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"
]
PINDO_UNITY_VERSION =
"2021.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.share_instanceObject



23
24
25
# File 'lib/pindo/module/build/unityhelper.rb', line 23

def share_instance
  instance
end

Instance Method Details

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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/pindo/module/build/unityhelper.rb', line 133

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pindo/module/build/unityhelper.rb', line 100

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(unity_major_version: nil, force_change_version: false) ⇒ Object



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
# File 'lib/pindo/module/build/unityhelper.rb', line 28

def find_unity_path(unity_major_version:nil, force_change_version:false)

  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

  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.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



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

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
      unless version.start_with?(PINDO_UNITY_VERSION)
        raise "Project Unity version (#{version}) does not match required version (#{PINDO_UNITY_VERSION}.x)"
      end
      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



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

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