Unity的AssetDatabase路徑格式

來源:互聯網
上載者:User

標籤:列印   work   har   base   規範   line   strong   分隔字元   param   

開發環境

windows 7

Unity 5.3 及更高版本

 

前言

使用AssetDatabase.Load或AnimatorController.CreateAnimatorControllerAtPath等Unity內建Editor API進行檔案操作時,經常碰到載入資源為null,或報路徑不存在!

經過斷點調試,發現絕大部分錯誤都是因為路徑的分隔字元存在兩種:"/"和"\"。

我們使用 System.IO.Path 這個API得到的路徑,其實也是以"\"分隔路徑的。

我們在windows下開啟資源管理員,某個目錄或檔案的路徑為:e:\Code\GameFramework\ 或 \\192.168.80.100\XXX\

 

但是使用Unity的API,列印Application.dataPath 時,列印出:E:/xxx/client/trunk/Project/Assets,所以可知,它的路徑和windows是反的,所以當我們使用的路徑不符合Unity的規範時,經常會報資源載入失敗。

比如某個FBX的路徑為:Assets/Art/Characters/Wing/fbx_3005/[email protected] ,而如果你的輸入的路徑或拼接的路徑不符合規範,那麼極有可能會負載檔案失敗。

 正常化路徑

提供一個方法,把路徑格式成Unity可讀取的路徑格式:

/// <summary>    /// 格式化路徑成Asset的標準格式    /// </summary>    /// <param name="filePath"></param>    /// <returns></returns>    public static string FormatAssetPath(string filePath)    {        var newFilePath1 = filePath.Replace("\\", "/");        var newFilePath2 = newFilePath1.Replace("//", "/").Trim();        newFilePath2 = newFilePath2.Replace("///", "/").Trim();        newFilePath2 = newFilePath2.Replace("\\\\", "/").Trim();        return newFilePath2;    }

Unity的AssetDatabase路徑格式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.