Module: EasyCI::UnityExeHelper

Defined in:
lib/easyci/unity_exe_helper.rb

Overview

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

Class Method Summary collapse

Class Method Details

.find_latest_unity(verbose = false) ⇒ String

查找最新版本的Unity

Parameters:

  • verbose (Boolean) (defaults to: false)

    是否显示详细信息

Returns:

  • (String)

    Unity可执行文件路径



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
# File 'lib/easyci/unity_exe_helper.rb', line 65

def self.find_latest_unity(verbose = false)
  # 检查macOS路径
  if RUBY_PLATFORM =~ /darwin/
    hub_path = "/Applications/Unity/Hub/Editor"
    if Dir.exist?(hub_path)
      # 获取所有版本并按版本号排序
      versions = Dir.entries(hub_path).select { |e| e =~ /[\d\.]+/ }.sort_by do |v|
        v.split('.').map(&:to_i)
      end
      
      if versions.any?
        latest_version = versions.last
        mac_path = "#{hub_path}/#{latest_version}/Unity.app/Contents/MacOS/Unity"
        if File.exist?(mac_path)
          puts "使用最新Unity版本 #{latest_version}: #{mac_path}" if verbose
          return mac_path
        end
      end
    end
    
    # 查找传统路径
    traditional_path = "/Applications/Unity/Unity.app/Contents/MacOS/Unity"
    if File.exist?(traditional_path)
      puts "使用传统路径Unity: #{traditional_path}" if verbose
      return traditional_path
    end
  # 检查Windows路径
  elsif RUBY_PLATFORM =~ /mswin|mingw|cygwin/
    hub_path = "C:/Program Files/Unity/Hub/Editor"
    if Dir.exist?(hub_path)
      versions = Dir.entries(hub_path).select { |e| e =~ /[\d\.]+/ }.sort_by do |v|
        v.split('.').map(&:to_i)
      end
      
      if versions.any?
        latest_version = versions.last
        win_path = "#{hub_path}/#{latest_version}/Editor/Unity.exe"
        if File.exist?(win_path)
          puts "使用最新Unity版本 #{latest_version}: #{win_path}" if verbose
          return win_path
        end
      end
    end
    
    # 查找传统路径
    traditional_path = "C:/Program Files/Unity/Editor/Unity.exe"
    if File.exist?(traditional_path)
      puts "使用传统路径Unity: #{traditional_path}" if verbose
      return traditional_path
    end
  # 检查Linux路径
  elsif RUBY_PLATFORM =~ /linux/
    hub_path = "/opt/unity/hub/editor"
    if Dir.exist?(hub_path)
      versions = Dir.entries(hub_path).select { |e| e =~ /[\d\.]+/ }.sort_by do |v|
        v.split('.').map(&:to_i)
      end
      
      if versions.any?
        latest_version = versions.last
        linux_path = "#{hub_path}/#{latest_version}/Editor/Unity"
        if File.exist?(linux_path)
          puts "使用最新Unity版本 #{latest_version}: #{linux_path}" if verbose
          return linux_path
        end
      end
    end
  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可执行文件路径



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/easyci/unity_exe_helper.rb', line 8

def self.find_unity_executable(project_path = nil, verbose = false)
  unity_path = nil
  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
  
  # 如果找到项目版本,尝试定位对应的Unity
  if project_version
    # 检查macOS路径
    if RUBY_PLATFORM =~ /darwin/
      mac_path = "/Applications/Unity/Hub/Editor/#{project_version}/Unity.app/Contents/MacOS/Unity"
      if File.exist?(mac_path)
        unity_path = mac_path
        puts "找到匹配版本的Unity: #{unity_path}" if verbose
      end
    # 检查Windows路径
    elsif RUBY_PLATFORM =~ /mswin|mingw|cygwin/
      win_path = "C:/Program Files/Unity/Hub/Editor/#{project_version}/Editor/Unity.exe"
      if File.exist?(win_path)
        unity_path = win_path
        puts "找到匹配版本的Unity: #{unity_path}" if verbose
      end
    # 检查Linux路径
    elsif RUBY_PLATFORM =~ /linux/
      linux_path = "/opt/unity/hub/editor/#{project_version}/Editor/Unity"
      if File.exist?(linux_path)
        unity_path = linux_path
        puts "找到匹配版本的Unity: #{unity_path}" if verbose
      end
    end
  end
  
  # 如果没有找到匹配版本,尝试查找最新版本
  if unity_path.nil?
    puts "没有找到匹配的Unity版本,尝试查找最新版本" if verbose
    unity_path = find_latest_unity(verbose)
  end
  
  # 如果仍然找不到,抛出错误
  raise "无法找到Unity可执行文件,请确保Unity已正确安装" if unity_path.nil?
  
  unity_path
end

.get_unity_version(unity_path) ⇒ String

获取Unity版本信息

Parameters:

  • unity_path (String)

    Unity可执行文件路径

Returns:

  • (String)

    Unity版本信息



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/easyci/unity_exe_helper.rb', line 148

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)

    是否有效



140
141
142
143
# File 'lib/easyci/unity_exe_helper.rb', line 140

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