Unity不同平臺生成中預(yù)處理的注意點-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)公司長期為上千多家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為科爾沁左翼企業(yè)提供專業(yè)的網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè),科爾沁左翼網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

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

#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項目(例如PC & Mac Standalone保存的)之后,再打開項目的Script(這里用VS2008+VA),會發(fā)現(xiàn)上述加粗行高亮。即Target和AssetRootPath在編譯前已然確定,且之后不能對其做出變更。

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

此時MyMethod可能用了如下代碼,

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

不禁讓我想起Effective C++里的第2個條款:盡量以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>
  /// 刪除單個文件的數(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))  //保護,避免文件目錄不存在跳異常
        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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站欄目:Unity不同平臺生成中預(yù)處理的注意點-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://bm7419.com/article26/cecccg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化Google、ChatGPT虛擬主機、移動網(wǎng)站建設(shè)企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)