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)


37
38
39
40
41
42
43
44
# 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

Instance Attribute Details

#guidObject (readonly)

Returns the value of attribute guid.



35
36
37
# File 'lib/u3d/asset.rb', line 35

def guid
  @guid
end

#metaObject (readonly)

Returns the value of attribute meta.



35
36
37
# File 'lib/u3d/asset.rb', line 35

def meta
  @meta
end

#meta_pathObject (readonly)

Returns the value of attribute meta_path.



35
36
37
# File 'lib/u3d/asset.rb', line 35

def meta_path
  @meta_path
end

#pathObject (readonly)

Returns the value of attribute path.



35
36
37
# File 'lib/u3d/asset.rb', line 35

def path
  @path
end

Class Method Details

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



29
30
31
32
# 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

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  return false unless other.is_a? Asset
  other.guid == @guid
end

#extensionObject



60
61
62
# File 'lib/u3d/asset.rb', line 60

def extension
  File.extname(@path)
end

#guid_referencesObject



46
47
48
49
50
51
# 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

#hashObject



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

def hash
  @guid.to_i
end

#inspectObject



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

def inspect
  to_s
end

#name_referencesObject



53
54
55
56
57
58
# 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_sObject



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

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