我的環境配置:Windows 10、Unity 5.6.0f3、Java JDK 1.8.0_121、Android Studio 2.2.3。
 
 
相信很多人都有遇到 Android 讀不到資源的問題過。
 
 
Android 這邊有兩個規定:
 
一、"檔案" 必須在 "res/raw" 目錄底下。(若沒這資料夾,請自行建立)
二、"檔案名稱" 絕對不能夠包含 "大寫字母"。
 
 
例如我想讀取一個 doorbell.mp3 的檔案,以下為正確的作法:
 
context.getResources().getIdentifier("doorbell", "raw", context.getPackageName());
 
 
 
以下是錯誤的作法:
 
// 檔案名稱不能包含大寫, 這是錯誤的
context.getResources().getIdentifier("Doorbell", "raw", context.getPackageName());

// 檔案名稱不能包含大寫, 這是錯誤的
context.getResources().getIdentifier("doorBell", "raw", context.getPackageName());

// 檔案沒有放在 res/raw 目錄下, 這是錯誤的
context.getResources().getIdentifier("doorbell", null, context.getPackageName());
 
 
在 Unity 中,這樣的錯誤甚至會造成編譯失敗:
 
CommandInvokationFailure: Failed to re-package resources.
E:\Android_SDK\build-tools\26.0.0\aapt.exe package --auto-add-overlay -v -f -m -J "gen" -M "AndroidManifest.xml" -S "res" -I "E:/Android_SDK\platforms\android-26\android.jar" -F bin/resources.ap_ --extra-packages tw.test.com -S "C:\..\..\Temp\StagingArea\android-libraries\unity-android-resources\res"
stderr[
invalid resource directory name: C:\Users\Rising-Win\Desktop\Unity\TestAndroid\Temp\StagingArea\android-libraries\unity-android-resources\res doorbell.mp3
]
stdout[
]
exit code: 1

image01.jpg

 
 
OBSOLETE - Providing Android resources in Assets/Plugins/Android/res is deprecated, please move your resources to an Android Library. See "Building Plugins for Android" section of the Manual.
UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()

image02.jpg

 
 
所以在 Unity 中,請確定你的 "檔案" 放置在 "Plugins/Android/res/raw" 目錄底下,並且 "檔名" 不包含大寫

image03.jpg

 
arrow
arrow
    文章標籤
    Unity Android res raw error
    全站熱搜

    岳 發表在 痞客邦 留言(0) 人氣()