Class: U3d::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/u3d/asset.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, unity_project = nil) ⇒ Asset

Returns a new instance of Asset.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
# File 'lib/u3d/asset.rb', line 39

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

Instance Attribute Details

#guidObject (readonly)

Returns the value of attribute guid.



37
38
39
# File 'lib/u3d/asset.rb', line 37

def guid
  @guid
end

#metaObject (readonly)

Returns the value of attribute meta.



37
38
39
# File 'lib/u3d/asset.rb', line 37

def meta
  @meta
end

#meta_pathObject (readonly)

Returns the value of attribute meta_path.



37
38
39
# File 'lib/u3d/asset.rb', line 37

def meta_path
  @meta_path
end

#pathObject (readonly)

Returns the value of attribute path.



37
38
39
# File 'lib/u3d/asset.rb', line 37

def path
  @path
end

Class Method Details

.glob(pattern, unity_project_path = Dir.pwd) ⇒ Object



31
32
33
34
# File 'lib/u3d/asset.rb', line 31

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

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/u3d/asset.rb', line 67

def eql?(other)
  return false unless other.is_a? Asset

  other.guid == @guid
end

#extensionObject



63
64
65
# File 'lib/u3d/asset.rb', line 63

def extension
  File.extname(@path)
end

#guid_referencesObject



49
50
51
52
53
54
# File 'lib/u3d/asset.rb', line 49

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

#hashObject



73
74
75
# File 'lib/u3d/asset.rb', line 73

def hash
  @guid.to_i
end

#inspectObject



81
82
83
# File 'lib/u3d/asset.rb', line 81

def inspect
  to_s
end

#name_referencesObject



56
57
58
59
60
61
# File 'lib/u3d/asset.rb', line 56

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_sObject



77
78
79
# File 'lib/u3d/asset.rb', line 77

def to_s
  "#{@guid}:#{@path}"
end