溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Unity不同平臺生成中預(yù)處理的注意點(diǎn)

發(fā)布時(shí)間:2020-08-10 18:36:04 來源:網(wǎng)絡(luò) 閱讀:362 作者:蓬萊仙羽 欄目:游戲開發(fā)

Unity3D的項(xiàng)目,這周吃虧在宏上了。大背景是項(xiàng)目需要在Unity中用Hudson自動(dòng)生成不同平臺的版本。

程序設(shè)計(jì)語言的預(yù)處理的概念:在編譯之前進(jìn)行的處理。

#if UNITY_WEBPLAYER
            BuildTarget target = BuildTarget.WebPlayer;
#elif UNITY_STANDALONE_WIN && UNITY_EDITOR
            BuildTarget target = BuildTarget.StandaloneWindows;
#elif UNITY_ANDROID
            BuildTarget target = BuildTarget.Android;
#else
            BuildTarget target = BuildTarget.iPhone;
#endif



#if UNITY_WEBPLAYER
    public const string AssetRootPath = AutomaticBuild.WebPlatFormDataPath + "/";
#elif UNITY_STANDALONE_WIN && UNITY_EDITOR
    public const string AssetRootPath = AutomaticBuild.WinPlatFormDataPath + "/";
#elif UNITY_ANDROID
    public const string AssetRootPath = AutomaticBuild.AndRoidPlatFormDataPath + "/";
#else
    public const string AssetRootPath = AutomaticBuild.IOSPlatFormDataPath + "/";
#endif

如上面兩段代碼,打開Unity項(xiàng)目(例如PC & Mac Standalone保存的)之后,再打開項(xiàng)目的Script(這里用VS2008+VA),會發(fā)現(xiàn)上述加粗行高亮。即Target和AssetRootPath在編譯前已然確定,且之后不能對其做出變更。

當(dāng)采用Unity支持的命令編譯時(shí)C:\program files\Unity\Editor>Unity.exe -quit -batchmode -executeMethod MyEditorScript.MyMethod

此時(shí)MyMethod可能用了如下代碼,

BuildPipeline.BuildPlayer( levels, "WebPlayerBuild",                     BuildTarget.WebPlayer, BuildOptions.None); 
Target和AssetRootPath并沒有賦予應(yīng)有的Web相應(yīng)值,會造成生成的Unity3D文件能生成但不對,執(zhí)行BuildPlayer時(shí)會報(bào)Runtime Error錯(cuò)。


不禁讓我想起Effective C++里的第2個(gè)條款:盡量以const, enum, inline替換 #define。果然金科玉律……

我的解決方式如下:

1.在MyMethod中先調(diào)用

SwitchActiveBuildTarget (target : BuildTarget)函數(shù)。

    private static string AssetRootPath = null;

    public static string GetAssetRootPath()
    {
        if (AssetRootPath != null)
            return AssetRootPath;


        if (EditorUserBuildSettings.activeBuildTarget==BuildTarget.WebPlayer)
        {
            AssetRootPath = "WebData/LatestData/";
        }
        else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
        {
            AssetRootPath = "AndroidData/LatestData/";
        }
        else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows)
        {
            AssetRootPath = "WinData/LatestData/";
        }
        else
        {
            AssetRootPath = "IOSData/LatestData/";
        }


        return AssetRootPath;
    }

    public static BuildTarget GetBuildTarget()
    {
        return EditorUserBuildSettings.activeBuildTarget;
    }


由于需求的小變更,小小地重構(gòu)了上次的代碼:

        ClearISingleFileSeries();
        ClearDirectorySeries();


    /// <summary>
    /// 刪除單個(gè)文件的數(shù)組
    /// </summary>
    static void ClearISingleFileSeries()
    {
        string[] SingleFileSeries = { Application.dataPath + "/Plugins/I18N.dll", Application.dataPath + "/Plugins/I18N.CJK.dll", Application.dataPath + "/Plugins/I18N.West.dll" };
        ClearFiles(SingleFileSeries);
    }


    /// <summary>
    /// 刪除filesPath數(shù)組內(nèi)指向的文件
    /// </summary>
    ///  <param name="filesPath"></param>
    static void ClearFiles(string[] filesPath)
    {
        foreach (string singleFilePath in filesPath)
        {
            if (File.Exists(singleFilePath))
            {
                try
                {
                    File.Delete(singleFilePath);
                }
                catch (System.Exception ex)
                {
                    //catch ex
                }
            }
        }
        
    }


    /// <summary>
    /// 刪除目前做Web版本會出現(xiàn)內(nèi)存問題的Audio資源
    /// </summary>
    static void ClearDirectorySeries()
    {
        string[] audioPath = { Application.dataPath + "/Game/Audio/Resources", Application.dataPath + "/Game/Audio/SFX", Application.dataPath + "/Game/MyGUI" };
        foreach (string audioDirectory in audioPath)
        {
            if (Directory.Exists(audioDirectory))  //保護(hù),避免文件目錄不存在跳異常
                ClearFilesAndDirectory(audioDirectory);
        }
    }

        
    /// <summary>
    /// 刪除dataPath文件目錄下的所有子文件及子文件夾
    /// </summary>
    ///  <param name="DirectoryPath"></param>
    static void ClearFilesAndDirectory(string DirectoryPath)
    {
        DirectoryInfo dir = new DirectoryInfo(DirectoryPath);


        //文件
        foreach (FileInfo fChild in dir.GetFiles("*"))
        {
            if (fChild.Attributes != FileAttributes.Normal)
                fChild.Attributes = FileAttributes.Normal;
            fChild.Delete();
        }


        //文件夾
        foreach (DirectoryInfo dChild in dir.GetDirectories("*"))
        {
            if (dChild.Attributes != FileAttributes.Normal)
                dChild.Attributes = FileAttributes.Normal;
            ClearFilesAndDirectory(dChild.FullName);
            dChild.Delete();
        }

    }


轉(zhuǎn)載自:http://blog.csdn.net/pandawuwyj/article/details/7959335

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI