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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27032631/article/details/77676264
  #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

猜你喜欢

转载自blog.csdn.net/qq_27032631/article/details/77676264