yyl
2025-05-19 b118ece3db250a5a257b60713da92234d8d5a57a
1
2
3
4
5
6
7
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
using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
 
/// <summary>
/// 版本检查类,用于获取当前应用版本信息
/// </summary>
public class VersionCheck
{
    /// <summary>
    /// 获取本地版本信息
    /// 适用于安卓、iOS、WebGL和PC平台
    /// </summary>
    /// <returns>版本字符串,格式为 x.y.z</returns>
    public static async UniTask<string> GetLocalVersionAsync()
    {
        try
        {
            // 首先尝试从Resources加载版本文件
            TextAsset versionAsset = Resources.Load<TextAsset>("version");
            
            if (versionAsset != null)
            {
                string versionText = versionAsset.text.Trim();
                Debug.Log($"从Resources加载版本信息: {versionText}");
                return versionText;
            }
            
            // 如果Resources中没有版本文件,则使用应用程序版本
            string appVersion = Application.version;
            Debug.Log($"使用应用程序版本: {appVersion}");
            
            // 确保版本号格式正确
            if (string.IsNullOrEmpty(appVersion))
            {
                Debug.LogWarning("应用程序版本为空,使用默认版本 1.0.0");
                appVersion = "1.0.0";
            }
            
            return appVersion;
        }
        catch (Exception e)
        {
            Debug.LogError($"获取本地版本信息失败: {e.Message}");
            return "1.0.0"; // 返回默认版本
        }
    }
}