Class: Pindo::Unity::UnityHelper

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.share_instanceObject



19
20
21
# File 'lib/pindo/module/unity/unity_helper.rb', line 19

def share_instance
  instance
end

Instance Method Details

#build_yoo_asset(unity_exe_full_path: nil, project_path: nil, platform: nil) ⇒ Hash

构建 YooAsset 资源包

Parameters:

  • unity_exe_full_path (String) (defaults to: nil)

    Unity 可执行文件路径

  • project_path (String) (defaults to: nil)

    Unity 项目路径

  • platform (String) (defaults to: nil)

    目标平台 (‘Android’, ‘iOS’ 等)

Returns:

  • (Hash)

    执行结果,如果跳过则返回 { skipped: true }



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/pindo/module/unity/unity_helper.rb', line 201

def build_yoo_asset(unity_exe_full_path:nil, project_path:nil, platform: nil)
  # 检查项目中是否存在 YooAsset
  unless yoo_asset_exists?(project_path)
    return { skipped: true, reason: 'YooAsset not found' }
  end

  puts "检测到 YooAsset,开始构建资源..."

  # 检查是否有Unity进程在运行
  UnityProcHelper.check_unity_processes(unity_exe_full_path: unity_exe_full_path, project_path: project_path)

  additional_args = {}
  additional_args[:platform] = platform if platform

  # 使用 Yoo.Editor.YooCommandHelper.BatchBuild 进行资源构建
  result = UnityCommandHelper.execute_unity_command(
    unity_exe_full_path,
    project_path,
    method: 'Yoo.Editor.YooCommandHelper.BatchBuild',
    additional_args: additional_args
  )

  if result[:success]
    puts "YooAsset 构建完成"
    puts "使用 Unity 版本: #{result[:unity_version]}"
    if !result[:stdout].empty?
      puts "Unity 输出:"
      puts result[:stdout]
    end
  else
    puts "YooAsset 构建失败!"
    raise Informative, "YooAsset 资源构建失败!请确保 YooAsset 配置正确!"
  end

  result
end

#check_goodunitybuild_version(unity_exe_full_path, project_path) ⇒ Object

检查 GoodUnityBuild 版本是否满足要求



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/pindo/module/unity/unity_helper.rb', line 49

def check_goodunitybuild_version(unity_exe_full_path, project_path)
  Funlog.instance.fancyinfo_start("检查 GoodUnityBuild 版本")

  # 检查是否有Unity进程在运行
  UnityProcHelper.check_unity_processes(unity_exe_full_path: unity_exe_full_path, project_path: project_path)

  # 执行 Unity 命令获取库版本
  result = UnityCommandHelper.execute_unity_command(
    unity_exe_full_path,
    project_path,
    method: 'GoodUnityBuild.BuildManager.PrintLibraryVersion',
    additional_args: {}
  )

  return unless result[:success]

  # 尝试多种模式匹配版本号
  patterns = [
    /GoodUnityBuild Version:\s*(\d+\.\d+\.\d+)/i,
    /\[GoodUnityBuild\]\s+Version\s+(\d+\.\d+\.\d+)/i
  ]

  library_version = nil
  patterns.each do |pattern|
    if match = result[:stdout].match(pattern)
      library_version = match[1]
      break
    end
  end

  # 从文件读取作为备用方案
  if library_version.nil?
    version_file = File.join(project_path, "Logs", "GoodUnityBuild_version.txt")
    if File.exist?(version_file)
      content = File.read(version_file).strip
      library_version = content[/(\d+\.\d+\.\d+)/, 1]
    end
  end

  return unless library_version

  required_version = Pindoconfig.instance.pindo_user_config_json["goodunitybuild_version"]

  Funlog.info("当前使用的 GoodUnityBuild 版本: #{library_version}")
  Funlog.info("要求的 GoodUnityBuild 最底版本: #{required_version}")

  return unless required_version

  if version_less_than?(library_version, required_version)
    raise Informative, "      GoodUnityBuild \u7248\u672C\u8FC7\u4F4E\uFF01\n      \u5F53\u524D\u7248\u672C: \#{library_version}\n      \u8981\u6C42\u7248\u672C: \#{required_version}\n      \u8BF7\u66F4\u65B0 GoodUnityBuild \u5E93\u5230 \#{required_version} \u6216\u66F4\u9AD8\u7248\u672C\n    MSG\n  end\n\n  Funlog.instance.fancyinfo_success(\"GoodUnityBuild \u7248\u672C\u68C0\u67E5\u901A\u8FC7\")\nrescue Informative\n  raise\nrescue => e\n  Funlog.warning(\"GoodUnityBuild \u5E93\u7248\u672C\u68C0\u67E5\u5931\u8D25: \#{e.message}\")\nend\n"

#config_build_mode(unity_exe_full_path: nil, project_path: nil, deploy_mode: 'dev') ⇒ Hash

配置 GoodUnityBuild 编译模式

Parameters:

  • unity_exe_full_path (String) (defaults to: nil)

    Unity 可执行文件路径

  • project_path (String) (defaults to: nil)

    Unity 项目路径

  • deploy_mode (String) (defaults to: 'dev')

    部署模式 (‘dev’, ‘adhoc’, ‘release’)

Returns:

  • (Hash)

    执行结果



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/pindo/module/unity/unity_helper.rb', line 274

def config_build_mode(unity_exe_full_path:nil, project_path:nil, deploy_mode: 'dev')
  # 验证 deploy_mode 参数
  valid_modes = ['dev', 'adhoc', 'release']
  unless valid_modes.include?(deploy_mode)
    raise Informative, "无效的编译模式: #{deploy_mode},必须是 #{valid_modes.join(', ')}"
  end

  # 检查是否有Unity进程在运行
  UnityProcHelper.check_unity_processes(unity_exe_full_path: unity_exe_full_path, project_path: project_path)

  puts "配置 GoodUnityBuild 编译模式为: #{deploy_mode}"

  # 使用 GoodUnityBuild.BuildManager.BatchConfig 配置编译模式
  result = UnityCommandHelper.execute_unity_command(
    unity_exe_full_path,
    project_path,
    method: 'GoodUnityBuild.BuildManager.BatchConfig',
    additional_args: { deployMode: deploy_mode }
  )

  if result[:success]
    puts "编译模式配置完成: #{deploy_mode}"
    puts "使用 Unity 版本: #{result[:unity_version]}"
    if !result[:stdout].empty?
      puts "Unity 输出:"
      puts result[:stdout]
    end
  else
    puts "编译模式配置失败!"
    raise Informative, "GoodUnityBuild 编译模式配置失败!请确保 GoodUnityBuild 正确安装!"
  end

  result
end

#export_project(unity_exe_full_path: nil, project_path: nil, platform: nil, isLibrary: false, indexNo: nil, deployMode: nil) ⇒ Object



113
114
115
116
117
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
# File 'lib/pindo/module/unity/unity_helper.rb', line 113

def export_project(unity_exe_full_path:nil, project_path:nil, platform: nil, isLibrary: false, indexNo: nil, deployMode: nil)

  # 检查是否有Unity进程在运行,传入Unity路径和项目路径以精确匹配
  UnityProcHelper.check_unity_processes(unity_exe_full_path: unity_exe_full_path, project_path: project_path)

  additional_args = {}
  additional_args[:platform] = platform if platform
  additional_args[:buildtype] = 'library' if isLibrary
  additional_args[:indexno] = indexNo if indexNo
  additional_args[:deploymode] = deployMode || 'dev' if deployMode || true  # 默认为 dev

  # 使用GoodUnityBuild.BuildManager.BatchBuild进行导出
  result = UnityCommandHelper.execute_unity_command(
    unity_exe_full_path,
    project_path,
    method: 'GoodUnityBuild.BuildManager.BatchBuild',
    additional_args: additional_args
  )

  if result[:success]
    puts "Unity build completed successfully"
    puts "Using Unity version: #{result[:unity_version]}"
    if !result[:stdout].empty?
      puts "Unity输出:"
      puts result[:stdout]
    end
  else
    
    puts "Unity构建失败! "

    raise Informative, "GoodUnityBuild 导出时候失败!请确保GoodUnityBuild 能正常导出!!!"
  end

  result
end

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

委托给 UnityEnvHelper 的类方法



25
26
27
28
29
30
# File 'lib/pindo/module/unity/unity_helper.rb', line 25

def find_unity_path(project_unity_version: nil, force_change_version: false)
  UnityEnvHelper.find_unity_path(
    project_unity_version: project_unity_version,
    force_change_version: force_change_version
  )
end

#force_update_libraries(unity_exe_full_path: nil, project_path: nil) ⇒ Hash

强制更新必备库(NugetForUnity)

Parameters:

  • unity_exe_full_path (String) (defaults to: nil)

    Unity 可执行文件路径

  • project_path (String) (defaults to: nil)

    Unity 项目路径

Returns:

  • (Hash)

    执行结果



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/pindo/module/unity/unity_helper.rb', line 242

def force_update_libraries(unity_exe_full_path:nil, project_path:nil)
  # 检查是否有Unity进程在运行
  UnityProcHelper.check_unity_processes(unity_exe_full_path: unity_exe_full_path, project_path: project_path)

  # 使用 NugetForUnity.Joy.JoyTools.CheckForceUpdate 强制更新库
  result = UnityCommandHelper.execute_unity_command(
    unity_exe_full_path,
    project_path,
    method: 'NugetForUnity.Joy.JoyTools.CheckForceUpdate',
    additional_args: {}
  )

  if result[:success]
    puts "必备库更新完成"
    puts "使用 Unity 版本: #{result[:unity_version]}"
    if !result[:stdout].empty?
      puts "Unity 输出:"
      puts result[:stdout]
    end
  else
    puts "必备库更新失败!"
    raise Informative, "NugetForUnity 库更新失败!请检查网络连接和配置!"
  end

  result
end

#get_unity_version(project_path) ⇒ Object



32
33
34
# File 'lib/pindo/module/unity/unity_helper.rb', line 32

def get_unity_version(project_path)
  UnityEnvHelper.get_unity_version(project_path)
end

#unity_project?(project_path) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/pindo/module/unity/unity_helper.rb', line 36

def unity_project?(project_path)
  UnityEnvHelper.unity_project?(project_path)
end

#version_less_than?(v1, v2) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/pindo/module/unity/unity_helper.rb', line 40

def version_less_than?(v1, v2)
  UnityEnvHelper.version_less_than?(v1, v2)
end

#yoo_asset_exists?(project_path) ⇒ Boolean

检查 YooAsset 是否存在YooAsset 可能以以下形式存在:

1. Assets/Packages/Joy.Tool.YooExt.* (

Parameters:

  • project_path (String)

    Unity 项目路径

Returns:

  • (Boolean)

    如果 YooAsset 存在返回 true



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/pindo/module/unity/unity_helper.rb', line 156

def yoo_asset_exists?(project_path)
  # 1. 检查 Assets/Packages 目录(手动导入方式)
  packages_path = File.join(project_path, "Assets", "Packages")
  if File.directory?(packages_path)
    yoo_pattern = File.join(packages_path, "Joy.Tool.YooExt.*")
    matching_dirs = Dir.glob(yoo_pattern)

    if matching_dirs.any?
      puts "找到 YooAsset (Assets): #{matching_dirs.first}"
      return true
    end
  end

  # 2. 检查 Library/PackageCache 目录(UPM 安装方式)
  package_cache_path = File.join(project_path, "Library", "PackageCache")
  if File.directory?(package_cache_path)
    # 检查多个可能的包名
    yoo_patterns = [
      "com.tuyoogame.yooasset@*",      # 官方 YooAsset 包
      "com.fununitylib.yooext@*"       # YooExt 辅助包
    ]

    yoo_patterns.each do |pattern|
      yoo_pattern = File.join(package_cache_path, pattern)
      matching_dirs = Dir.glob(yoo_pattern)

      if matching_dirs.any?
        puts "找到 YooAsset (UPM): #{matching_dirs.first}"
        return true
      end
    end
  end

  false
rescue => e
  # 如果检查失败,返回 false(保守处理)
  Funlog.warning("检查 YooAsset 是否存在时出错: #{e.message}")
  false
end