当前位置:   article > 正文

AssetsBuddle_loadasset("assetbundlemanifes

loadasset("assetbundlemanifest") 为空

部分内容引自SIKI学院
CreateAssetBundles:

    [ MenuItem ( "AssetBundles/Build AssetBundles" )]
    static void BuildAllAssetBundles ()
            {
                         string dir = "AssetBundles" ;
     
        if ( Directory . Exists (dir)== false )
        {
            Directory . CreateDirectory (dir);
        }
  BuildPipeline . BuildAssetBundles (dir, BuildAssetBundleOptions . UncompressedAssetBundle , BuildTarget . StandaloneWindows64 );
    }

或者:
[MenuItem("TobyStarkTools/AssetBundle/PackingAB")]
    static void BuildAssetBundle()
    {
        //打包到Asset目录外面
        string path = Application.dataPath + "/../www/AssetBundls";
        if (!System.IO.Directory.Exists(path))
        {
            System.IO.Directory.CreateDirectory(path);

        }

        if (BuildPipeline.BuildAssetBundles(path,
            BuildAssetBundleOptions.None,
            BuildTarget.StandaloneWindows64))
        {
            Debug.Log("打包成功");
            
        }

清理:
            [ MenuItem ( "AssetBundles/clear AssetBundles" )]
             static void Clear (){
                         string dir = "AssetBundles" ;
                         if ( Directory . Exists (dir)== true )
                        {
                                     string path = Application . dataPath + "/../" + dir+ "/" ;
                                     Directory . Delete (path, true );
                        }
            }

或者:

[MenuItem("aaa/b")]
    static void de(){
        string path = Application.dataPath + "/../www/AssetBundls";
        System.IO.Directory.Delete (path,true);
        Debug.Log ("清除成功");
    }

     LoadAssets:

             加载一种资源
          string path = "AssetBundles/cubewall.ab" ;
         AssetBundle ab = AssetBundle.LoadFromFile(path);
         GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
         Instantiate(wallPrefab);
          yield return null ;


             加载所有资源
        AssetBundle ab2 = AssetBundle.LoadFromFile("AssetBundles/cubewall.ab");
        Object[] objs= ab2.LoadAllAssets();
        foreach(Object o in objs)
        {
            Instantiate(o);
         }


              第一种加载AB的方式 LoadFromMemoryAsync
        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        yield return request;
        AssetBundle ab = request.assetBundle;
        GameObject wallPrefab = ab.LoadAsset<GameObject>( "CubeWall" );
        Instantiate(wallPrefab);

                第二种加载AB的方式 LoadFromFile
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
        yield return request;
        AssetBundle ab = request.assetBundle;
        GameObject wallPrefab = ab.LoadAsset<GameObject>( "CubeWall" );
        Instantiate(wallPrefab);

               第三种加载AB的方式 WWW  
如果是本地服务器:

        while (Caching.ready == false )
        {
            yield return null ;
        }

        WWW www =
        WWW.LoadFromCacheOrDownload( @"file://F:\CodeFiles\unity\Framwork\AB\AssetBundleProject\AssetBundles\cubewall.ab" , 2 );
        yield return www;
        if ( string .IsNullOrEmpty(www.error)== false )
        {
            Debug.Log(www.error); yield break ;
         }
        AssetBundle ab = www.assetBundle;
        GameObject wallPrefab = ab.LoadAsset<GameObject>( "CubeWall" );
         Instantiate(wallPrefab);

                    第四种方式 使用UnityWebRequest
           //string uri = @"file://F:\CodeFiles\unity\Framwork\AB\AssetBundleProject\AssetBundles\cubewall.ab";
          string uri = @"http://localhost/AssetBundles/cubewall.ab" ;
         UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
          yield return request.Send();
          //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
         AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
         GameObject wallPrefab = ab.LoadAsset<GameObject>( "CubeWall" );
         Instantiate(wallPrefab);
         Instantiate(wallPrefab);

              //加载依赖文件
        AssetBundle manifestAB =  AssetBundle . LoadFromFile ( "AssetBundles/AssetBundles" );
         AssetBundle ab = AssetBundle . LoadFromFile ( "AssetBundles/cubewall.ab" );
        AssetBundleManifest manifest = manifestAB. LoadAsset < AssetBundleManifest >( "AssetBundleManifest" );
        string [] strs =  manifest. GetAllDependencies ( "cubewall.ab" );
        foreach ( string name in strs)
        {
            print (name);
            AssetBundle . LoadFromFile ( "AssetBundles/" + name);
        }
        GameObject wallPrefab = ab. LoadAsset < GameObject >( "CubeWall" );
         Instantiate (wallPrefab);
或者:

       //从AB中加载资源,需要实例化预设 显然不能只加载Prefab,因为他依赖了模型和材质,需要去解决他们的依赖关系
       private string m_ABPath = "";
       //依赖文件
       private AssetBundleManifest m_Manifest;
       void Start ()
      {
        m_ABPath = Application.dataPath + "/../www/AssetBundls/";
        AssetBundle mainfestBundle = null;
        //1.先加载依赖文件
        m_Manifest = LoadAsset<AssetBundleManifest>("AssetBundls","AssetBundleManifest",out mainfestBundle);
        string abName = "Download/Prefab/EnvPrefabs/a.assetbundle";
        //2.再加载依赖项  获取哪一个ab包的依赖项
        string[] arrDps = m_Manifest.GetAllDependencies(abName);
        AssetBundle[] arrDpsBundle = new AssetBundle[arrDps.Length];
        for (int i = 0; i < arrDps.Length; i++)
        {
            AssetBundle dpsBundle = null;
            //注意依赖项的 路径
             LoadAsset<Object>(arrDps[i], arrDps[i],out  dpsBundle);
            arrDpsBundle[i] = dpsBundle;
            Debug.Log("2加载依赖项->"+arrDps[i]);
        }
       
        //3.最后加载预设体 并clone
        AssetBundle jianzuBundle = null;
        GameObject objPrefab = LoadAsset<GameObject>(abName, "a",out  jianzuBundle);
        Instantiate(objPrefab);
        
        
        //-----------------------卸载资源镜像
        //卸载预设AB镜像
        jianzuBundle.Unload(true);
        //卸载依赖项的AB镜像
        for (int i = 0; i < arrDpsBundle.Length; i++)
        {
            arrDpsBundle[i].Unload(false);
        }
        //卸载依赖文件AB镜像
        mainfestBundle.Unload(true);
        
    }

    public T LoadAsset<T>(string abName,string SourceName)where  T:Object
    {
        AssetBundle ab= AssetBundle.LoadFromFile(string.Format("{0}/{1}", m_ABPath, abName));
        return ab.LoadAsset<T>(SourceName);
    }
    
    //方便卸载AB的镜像,所以需要调用者再次获取到这个AB
    public T LoadAsset<T>(string abName,string SourceName,out AssetBundle ab)where  T:Object
    {
         ab= AssetBundle.LoadFromFile(string.Format("{0}/{1}", m_ABPath, abName));
        return ab.LoadAsset<T>(SourceName);
    }


文件校验:


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/82266?site
推荐阅读
相关标签
  

闽ICP备14008679号