Module: EasyCI::UnityExeHelper

Defined in:
lib/easyci/unity_exe_helper.rb

Overview

UnityExeHelper模块,用于查找和验证Unity可执行文件路径

Constant Summary collapse

UNITY_MAC_PATHS =

Unity可执行文件的可能路径 (使用通配符)

[
  "/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/*/Editor/Unity.exe"
]
UNITY_LINUX_PATHS =
[
  "/opt/Unity/Editor/Unity",
  "/opt/unity/hub/editor/*/Editor/Unity"
]

Class Method Summary collapse

Class Method Details

.extract_version_from_path(path) ⇒ String?

从路径中提取Unity版本号

Parameters:

  • path (String)

    Unity可执行文件路径

Returns:

  • (String, nil)

    提取的版本号,如果无法提取则返回nil



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/easyci/unity_exe_helper.rb', line 23

def self.extract_version_from_path(path)
  # macOS路径格式: /Applications/Unity/Hub/Editor/2021.3.45f1/Unity.app/Contents/MacOS/Unity
  # macOS路径格式(变体): /Applications/Unity/Hub/Editor/2021.3.45f1c1/Unity.app/Contents/MacOS/Unity
  # Windows路径格式: C:/Program Files/Unity/Hub/Editor/2021.3.45f1/Editor/Unity.exe
  # Windows路径格式(变体): C:/Program Files/Unity/Hub/Editor/2021.3.45f1c1/Editor/Unity.exe

  # 尝试匹配 macOS 路径格式
  if match = path.match(/Editor\/([\d.]+[a-zA-Z]\d+(?:c\d+)?)\//)
    return match[1]
  end

  # 尝试匹配 Windows 路径格式
  if match = path.match(/([\d.]+[a-zA-Z]\d+(?:c\d+)?)\/Editor\//)
    return match[1]
  end

  nil
end

.find_unity_executable(project_path = nil, verbose = false) ⇒ String

查找Unity可执行文件路径

Parameters:

  • project_path (String) (defaults to: nil)

    Unity项目路径

  • verbose (Boolean) (defaults to: false)

    是否显示详细信息

Returns:

  • (String)

    Unity可执行文件路径



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
94
95
96
97
98
99
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
# File 'lib/easyci/unity_exe_helper.rb', line 46

def self.find_unity_executable(project_path = nil, verbose = false)
  project_version = nil
  
  # 如果提供了项目路径,尝试从ProjectVersion.txt中读取版本
  if project_path && !project_path.empty?
    version_file = File.join(project_path, 'ProjectSettings', 'ProjectVersion.txt')
    if File.exist?(version_file)
      content = File.read(version_file)
      if content =~ /m_EditorVersion:\s*([\d\.]+[a-zA-Z\d]*)/
        project_version = $1
        puts "项目Unity版本: #{project_version}" if verbose
      end
    end
  end
  
  if project_version.nil?
    puts "未找到项目Unity版本信息" if verbose
  end

  # 如果找到了项目版本,提取主要版本号(例如:2021.3)
  unity_major_version = nil
  if project_version
    unity_major_version = project_version.split('.')[0..1].join('.')
    puts "项目Unity主要版本: #{unity_major_version}" if verbose
  end
  
  # 获取所有可能的Unity版本及路径
  unity_versions = []
  
  # 获取当前平台的Unity路径集合
  paths = case RUBY_PLATFORM
          when /darwin/
            UNITY_MAC_PATHS
          when /mswin|mingw|cygwin/
            UNITY_WINDOWS_PATHS
          when /linux/
            UNITY_LINUX_PATHS
          else
            []
          end
  
  # 查找所有可用的Unity版本
  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
          }
          puts "找到Unity版本: #{version} 路径: #{expanded_path}" if verbose
        end
      end
    elsif File.exist?(path)
      version = extract_version_from_path(path)
      if version
        major_version = version.split('.')[0..1].join('.')
        unity_versions << {
          path: path,
          version: version,
          major_version: major_version
        }
        puts "找到Unity版本: #{version} 路径: #{path}" if verbose
      end
    end
  end
  
  if unity_versions.empty?
    puts "未找到任何Unity版本" if verbose
    raise "未找到任何Unity版本,请确保Unity已正确安装"
  end
  
  # 如果找到项目版本,尝试精确匹配
  if project_version
    # 1. 精确匹配版本号
    exact_matches = unity_versions.select { |v| v[:version] == project_version }
    if !exact_matches.empty?
      unity_path = exact_matches.first[:path]
      puts "找到精确匹配的Unity版本 #{project_version}: #{unity_path}" if verbose
      return unity_path
    end
    
    # 2. 如果没有精确匹配,尝试按主要版本号匹配(如2021.3.x)
    if unity_major_version
      major_matches = unity_versions.select { |v| v[:major_version] == unity_major_version }
      
      if !major_matches.empty?
        # 按版本号排序
        sorted_versions = major_matches.sort_by do |v|
          # 解析版本号为数组,如 "2021.3.45f1" => [2021, 3, 45, 1]
          if v[:version] =~ /(\d+)\.(\d+)\.(\d+)f(\d+)/
            [$1.to_i, $2.to_i, $3.to_i, $4.to_i]
          else
            [0, 0, 0, 0]
          end
        end
        
        # 标准版本(如2021.3.45f1)优先于特殊版本(如2021.3.45f1c1)
        standard_versions = sorted_versions.select { |v| v[:version] =~ /^[\d\.]+f\d+$/ }
        
        if !standard_versions.empty?
          unity_path = standard_versions.last[:path]  # 取最新的标准版本
          puts "找到主要版本号匹配的标准Unity版本 #{standard_versions.last[:version]}: #{unity_path}" if verbose
          return unity_path
        else
          unity_path = sorted_versions.last[:path]  # 取最新版本
          puts "找到主要版本号匹配的Unity版本 #{sorted_versions.last[:version]}: #{unity_path}" if verbose
          return unity_path
        end
      end
    end
  end
  
  # 3. 如果没有找到匹配的版本,使用最新版本
  puts "未找到匹配项目版本的Unity,将使用最新可用版本" if verbose
  
  # 按版本号排序
  sorted_all_versions = unity_versions.sort_by do |v|
    if v[:version] =~ /(\d+)\.(\d+)\.(\d+)f(\d+)/
      [$1.to_i, $2.to_i, $3.to_i, $4.to_i]
    else
      [0, 0, 0, 0]
    end
  end
  
  # 标准版本优先
  standard_versions = sorted_all_versions.select { |v| v[:version] =~ /^[\d\.]+f\d+$/ }
  
  if !standard_versions.empty?
    unity_path = standard_versions.last[:path]  # 取最新的标准版本
    puts "使用最新标准Unity版本 #{standard_versions.last[:version]}: #{unity_path}" if verbose
    return unity_path
  else
    unity_path = sorted_all_versions.last[:path]  # 取最新版本
    puts "使用最新可用Unity版本 #{sorted_all_versions.last[:version]}: #{unity_path}" if verbose
    return unity_path
  end
end

.get_unity_version(unity_path) ⇒ String

获取Unity版本信息

Parameters:

  • unity_path (String)

    Unity可执行文件路径

Returns:

  • (String)

    Unity版本信息



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/easyci/unity_exe_helper.rb', line 199

def self.get_unity_version(unity_path)
  return nil unless valid_unity_path?(unity_path)
  
  begin
    # 使用-version参数获取版本信息
    version_output = `"#{unity_path}" -version 2>&1`
    if version_output =~ /([\d\.]+[a-zA-Z\d]*)/
      return $1
    end
  rescue => e
    puts "获取Unity版本信息失败: #{e.message}"
  end
  
  nil
end

.valid_unity_path?(unity_path) ⇒ Boolean

验证Unity可执行文件路径是否有效

Parameters:

  • unity_path (String)

    Unity可执行文件路径

Returns:

  • (Boolean)

    是否有效



191
192
193
194
# File 'lib/easyci/unity_exe_helper.rb', line 191

def self.valid_unity_path?(unity_path)
  return false if unity_path.nil? || unity_path.empty?
  File.exist?(unity_path) && File.executable?(unity_path)
end