Class: Pindo::Client::UnityHelper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pindo/module/build/unityhelper.rb

Constant Summary collapse

UNITY_MAC_PATHS =
[
  "/Applications/Unity/Unity.app/Contents/MacOS/Unity",
  "/Applications/Unity/Hub/Editor/*/Unity.app/Contents/MacOS/Unity"
]
UNITY_WINDOWS_PATHS =
[
  "C:/Program Files/Unity/Editor/Unity.exe",
  "C:/Program Files/Unity/Hub/Editor/*/Unity.exe"
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.share_instanceObject



21
22
23
# File 'lib/pindo/module/build/unityhelper.rb', line 21

def share_instance
  instance
end

Instance Method Details

#build_project(unity_exe_full_path: nil, project_path: nil, platform: nil, isLibrary: false) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/pindo/module/build/unityhelper.rb', line 151

def build_project(unity_exe_full_path:nil, project_path:nil, platform: nil, isLibrary: false)

  additional_args = {}
  additional_args[:platform] = platform if platform
  additional_args[:buildtype] = 'library' if isLibrary
  result = execute_unity_command(unity_exe_full_path, project_path, additional_args)

  if result[:success]
    puts "Unity build completed successfully"
    puts "Using Unity version: #{result[:unity_version]}"
    puts result[:stdout]
  else
    puts "Unity build failed"
    puts "Unity version: #{result[:unity_version]}"
    puts result[:stderr]
    raise "Unity build failed with status: #{result[:exit_status]}"
  end

  result
end

#execute_unity_command(unity_exe_full_path, project_path, additional_args = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/pindo/module/build/unityhelper.rb', line 118

def execute_unity_command(unity_exe_full_path, project_path, additional_args = {})
  if unity_exe_full_path.nil?
    raise Informative, "Unity path not found!"
  end

  cmd_args = [
    unity_exe_full_path,
    "-batchmode",
    "-quit",
    "-projectPath",
    project_path.to_s,
    "-executeMethod",
    "GoodUnityBuild.BuildManager.BatchBuild"
  ]

  # Add any additional arguments
  additional_args.each do |key, value|
    cmd_args << "-#{key}"
    cmd_args << value.to_s if value
  end

  puts "Unity command: #{cmd_args.join(' ')}"
  stdout, stderr, status = Open3.capture3(*cmd_args)

  {
    success: status.success?,
    stdout: stdout,
    stderr: stderr,
    exit_status: status.exitstatus
  }
end

#find_unity_path(project_unity_version: nil, force_change_version: false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pindo/module/build/unityhelper.rb', line 26

def find_unity_path(project_unity_version:nil, force_change_version:false)

  if project_unity_version.nil? || project_unity_version.empty?
    raise "Project Unity version is nil or empty"
  end

  unity_major_version = project_unity_version.split('.')[0..1].join('.')
  paths = case RUBY_PLATFORM
          when /darwin/
            UNITY_MAC_PATHS
          when /mswin|mingw|windows/
            UNITY_WINDOWS_PATHS
          else
            raise "Unsupported platform: #{RUBY_PLATFORM}"
          end

  unity_versions = []

  paths.each do |path|
    if path.include?("*")
      Dir.glob(path).each do |expanded_path|
        version = extract_version_from_path(expanded_path)
        if version
          major_version = version.split('.')[0..1].join('.')
          unity_versions << {
            path: expanded_path,
            version: version,
            major_version: major_version
          }
        end
      end
    elsif File.exist?(path)
      version = extract_version_from_path(path)
      major_version = version.split('.')[0..1].join('.')
      if version
        unity_versions << {
          path: path,
          version: version,
          major_version: major_version
        }
      end
    end
  end

  if unity_versions.empty?
    raise Informative, "未找到任何Unity版本"
  end


  select_unity_versions = unity_versions.select { |v| v[:version] == project_unity_version } || []
  if !select_unity_versions.nil? && !select_unity_versions.empty? && select_unity_versions.length >= 1
    return select_unity_versions.first[:path]
  end

  unity_versions.sort_by! { |v| v[:major_version] }
  select_unity_versions = unity_versions.select { |v| v[:major_version] == unity_major_version } if unity_major_version
  if select_unity_versions.nil? || select_unity_versions.empty?
    if force_change_version
      return unity_versions.last[:path]
    else
      raise Informative, "未找到匹配的Unity版本"
      return nil
    end
  else
    return select_unity_versions.first[:path]
  end

end

#get_unity_version(project_path) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/pindo/module/build/unityhelper.rb', line 173

def get_unity_version(project_path)
  version_path = File.join(project_path, "ProjectSettings", "ProjectVersion.txt")
  if File.exist?(version_path)
    content = File.read(version_path)
    if content =~ /m_EditorVersion: (.*)/
      version = $1.strip
      version
    else
      raise "Could not parse Unity version from #{version_path}"
    end
  else
    raise "Project version file not found at #{version_path}"
  end
end

#unity_project?(project_path) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
194
195
196
197
198
199
# File 'lib/pindo/module/build/unityhelper.rb', line 189

def unity_project?(project_path)
  # 检查关键Unity工程文件和目录是否存在
  project_settings_path = File.join(project_path, "ProjectSettings")
  assets_path = File.join(project_path, "Assets")
  packages_path = File.join(project_path, "Packages")

  File.directory?(project_settings_path) &&
  File.directory?(assets_path) &&
  File.directory?(packages_path) &&
  File.exist?(File.join(project_settings_path, "ProjectSettings.asset"))
end