Unity不一樣平臺生成中預處理的注意點

http://blog.csdn.net/pandawuwyj/article/details/7959335html

Unity3D的項目,這周吃虧在宏上了。大背景是項目須要在Unity中用Hudson自動生成不一樣平臺的版本。數組

程序設計語言的預處理的概念:在編譯以前進行的處理。函數

#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;
#endifui



#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 + "/";
#endifspa

如上面兩段代碼,打開Unity項目(例如PC & Mac Standalone保存的)以後,再打開項目的Script(這裏用VS2008+VA),會發現上述加粗行高亮。即Target和AssetRootPath在編譯前已然肯定,且以後不能對其作出變動。.net

當採用Unity支持的命令編譯時C:\program files\Unity\Editor>Unity.exe -quit -batchmode -executeMethod MyEditorScript.MyMethod設計

此時MyMethod可能用了以下代碼,3d

 

BuildPipeline.BuildPlayer( levels, "WebPlayerBuild", 
                   BuildTarget.WebPlayer, BuildOptions.None); 

Target和AssetRootPath並無賦予應有的Web相應值,會形成生成的Unity3D文件能生成但不對,執行BuildPlayer時會報Runtime Error錯。code

 

 

不由讓我想起Effective C++裏的第2個條款:儘可能以const, enum, inline替換 #define。果真金科玉律……orm

個人解決方式以下:

1.在MyMethod中先調用

SwitchActiveBuildTarget (target : BuildTarget)函數。

 

    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;
    }

 

因爲需求的小變動,小小地重構了上次的代碼:

        ClearISingleFileSeries();         ClearDirectorySeries();     /// <summary>     /// 刪除單個文件的數組    /// </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數組內指向的文件    /// </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版本會出現內存問題的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))  //保護,避免文件目錄不存在跳異常                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();         }     } 

相關文章
相關標籤/搜索