AssetBundles相关的坑

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








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值