class U3d::Asset

U3d::Asset provides you with a way to easily manipulate an search for assets in Unity Projects

Attributes

guid[R]
meta[R]
meta_path[R]
path[R]

Public Class Methods

glob(pattern, unity_project_path = Dir.pwd) click to toggle source
# File lib/u3d/asset.rb, line 29
def glob(pattern, unity_project_path = Dir.pwd)
  unity_project = U3d::UnityProject.new(unity_project_path)
  Dir.glob(pattern).reject { |path| File.extname(path) == '.meta' || !File.file?(path) }.map { |path| Asset.new(path, unity_project) }
end
new(path, unity_project = nil) click to toggle source
# File lib/u3d/asset.rb, line 37
def initialize(path, unity_project = nil)
  raise ArgumentError, "No file at #{path}" unless File.exist?(path)
  @path = path
  @meta_path = path + ".meta"
  @meta = YAML.safe_load(File.read(@meta_path))
  @guid = @meta['guid']
  @unity_project = unity_project
end

Public Instance Methods

eql?(other) click to toggle source
# File lib/u3d/asset.rb, line 64
def eql?(other)
  return false unless other.is_a? Asset
  other.guid == @guid
end
extension() click to toggle source
# File lib/u3d/asset.rb, line 60
def extension
  File.extname(@path)
end
guid_references() click to toggle source
# File lib/u3d/asset.rb, line 46
def guid_references
  @guid_references ||= U3dCore::CommandExecutor.execute(
    command: "grep -rl #{@guid} #{grep_reference_root}",
    print_command: false
  ).split("\n").reject { |f| f == @meta_path }.map { |path| Asset.new(path) }
end
hash() click to toggle source
# File lib/u3d/asset.rb, line 69
def hash
  @guid.to_i
end
inspect() click to toggle source
# File lib/u3d/asset.rb, line 77
def inspect
  to_s
end
name_references() click to toggle source
# File lib/u3d/asset.rb, line 53
def name_references
  @name_references ||= U3dCore::CommandExecutor.execute(
    command: "grep -rl #{File.basename(@path, extension)} #{grep_reference_root} --include=*.cs",
    print_command: false
  ).split("\n").map { |path| Asset.new(path) }
end
to_s() click to toggle source
# File lib/u3d/asset.rb, line 73
def to_s
  "#{@guid}:#{@path}"
end

Private Instance Methods

grep_reference_root() click to toggle source
# File lib/u3d/asset.rb, line 83
def grep_reference_root
  @unity_project && @unity_project.exist? ? 'Assets/' : '.'
end