1.加载资源的时候一定要是用相对路径,即前面加上“file://” 如下所示
string path ="file://"+ Application.dataPath + "/output/" + assetBundleName;
2.重新打包后去加载资源要把之前的缓存清除掉。如下:
void Awake()
{
Caching.CleanCache();
}
3.1中的path要放在方法中,Application.dataPath不能全局变量中使用
4.AssetBundles在安卓和苹果下的打包需要加参数BuildTarget.Android和BuildTarget.iOS。
5.根据不同平台打包的方便代码
BuildPipeline.BuildAssetBundles (outputPath, BuildAssetBundleOptions.None,
EditorUserBuildSettings.activeBuildTarget);
6.不知道怎么回事打包到安卓上总是读取不到,试了好多遍,也许就是路径的问题的,附上一个经过多次测试没有问题的代码。再次强调路径、路径、路径。重要的事情说三遍。
public class Build : MonoBehaviour {
[MenuItem("AB/CreatAssetBundles")]
public static void Creat()
{
Caching.CleanCache();
string path=Path.Combine(Application.streamingAssetsPath, "Android");
if(!File.Exists(path))
{
Directory.CreateDirectory(path);
}
BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.None, BuildTarget.Android);
AssetDatabase.Refresh();
}
}
public class Load : MonoBehaviour { string path; void Awake() { if(Application.platform==RuntimePlatform.Android) { path = "jar:file://" + Application.dataPath + "!/assets/Android/tex"; } else if(Application.platform==RuntimePlatform.WindowsEditor||Application.platform==RuntimePlatform.WindowsPlayer) { path = "file://" + Application.dataPath + "/StreamingAssets/Android/tex"; } } IEnumerator Start() { WWW www = WWW.LoadFromCacheOrDownload(path, 1); yield return www; AssetBundle ab = www.assetBundle; GetComponent<Image>().sprite = ab.LoadAsset<Sprite>("texture"); GameObject go = ab.LoadAsset<GameObject>("cube"); Instantiate(go); ab.Unload(false); } }