欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AssetBundle加载的四种方式(补充 三,四)

程序员文章站 2022-05-15 18:41:41
...
  #region 第三种类型 
    /// <summary>
    /// 路径有本地和远程,本地可以加file:// 或者file:/// 路径要完整的本地路径,这个位置卡了很久,经过多次尝试得到最终正确的路径
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    /// 
    //在Start方法中调用 参数为自己本地最终正确路径  
    //StartCoroutine(LoadFromCacheOrDownload(@"file://F:\PrivateForWCW\Unity\UGUI\AssetBudleProject\AssetBundle\scene\cubewall.unity3d"));

   /// 服务器上  StartCoroutine(LoadFromCacheOrDownload(@"http://localhost//AssetBundless/scene/cubewall.unity3d"));
    IEnumerator LoadFromCacheOrDownload(string path)
    {
        while (!Caching.ready)
            yield return null;
        WWW www = WWW.LoadFromCacheOrDownload(path, 1);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield break;
        }
        AssetBundle AB = www.assetBundle;
        GameObject obj=AB.LoadAsset<GameObject>("cube");
        Instantiate(obj);
    }
    #endregion

    #region 第四种 UnityWebRequst  //这个UnityWebRequst相当与取代了WWW这个类,还进一步优化了
    //使用  UnityWebRequest 需要引入UnityEngine.Networking;
    IEnumerator LoadFormWebRequst()
    {
        string uri = @"http://localhost//AssetBundless/scene/cubewall.unity3d";
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
        yield return request.Send();
        //   AssetBundle AB = DownloadHandlerAssetBundle.GetContent(request); 第一种方式获取AssetBundle
        AssetBundle AB = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;// 第二种方式获取AssetBundle
        GameObject obj = AB.LoadAsset<GameObject>("cube");
        Instantiate(obj);
    }


    #endregion

 

相关标签: unity AssetBundle