• Unity_AssetBundle 动态资源加载


    //Editor:编辑器模式 将脚本放置在此文件夹下 编辑器模式下运行

    //1.制作Prefabs       选中场景中的物体  

     1    //创建成prefab的文件夹
     2         string prefabPath = "Assets/_CreatedPrefabs/";
     3 
     4         //验证该文件夹是否存在 
     5         if (AssetDatabase.IsValidFolder(prefabPath) == false)
     6         {
     7             AssetDatabase.CreateFolder("Assets", "_CreatedPrefabs");
     8         }
     9 
    10         //场景中选中的物体
    11         Transform[] selectedTransform = Selection.transforms;
    12 
    13         //遍历选中的物体   
    14         foreach (Transform tran in selectedTransform)
    15         {
    16             //   遍历子物体    创建成prefab
    17             for (int i = 0; i < tran.childCount; i++)
    18             {
    19 
    20                 Transform childTran = tran.GetChild(i);
    21                 GameObject childgo = childTran.gameObject;
    22                 string path = prefabPath +childTran.name+".prefab";
    23                 PrefabUtility.CreatePrefab(path, childgo);
    24 
    25                 AssetBundleBuild build = new AssetBundleBuild();
    26                 build.assetBundleName = childgo.name;          //设置AssetBundle名称
    27                 build.assetNames = new string[] { path};        //绑定资源
    28 
    29                 builds.Add(build);
    30             }
    31         }

    //2.通过生成的Prefab来制作AssetBundle

    1         AssetBundleBuild[] targetBuilds = new AssetBundleBuild[builds.Count];
    2 
    3         for (int i = 0; i < targetBuilds.Length;i++ )
    4         {
    5             targetBuilds[i] = builds[i];
    6         }
    7 
    8             BuildPipeline.BuildAssetBundles(assetBundlePath, targetBuilds);    //打包到指定路径assetBundlePath

    //BuildPipeline.BuildAssetBundles("打包出来路径","目标AssetBundleBuild[]");

     //在指定文件夹下可以找到打包好的资源文件: 

    //上传gem1 mod_basemeta1 到Tomcat服务器下载

    //在unity中设置ProjectSetting :

    //在编辑器下可以下载

     1     IEnumerator LoadAsset(string assetName)
     2     {
     3         using (WWW www = new WWW(m_pathURL + assetName))
     4         {
     5             yield return www;
     6 
     7             if (www.error != null)
     8             {
     9                 throw new Exception("www download exception:"+www.error);
    10             }
    11 
    12             AssetBundle ab = www.assetBundle;
    13 
    14             string[] assetNames = ab.GetAllAssetNames();
    15 
    16             Debug.Log("--assetNames[0]--"+assetNames[0]);
    17 
    18             //存在于缓存中  
    19             AssetBundleRequest request = ab.LoadAssetAsync<GameObject>(assetNames[0]);
    20 
    21             yield return request;
    22 
    23             GameObject obj = request.asset as GameObject;
    24 
    25             GameObject clone = Instantiate(obj);
    26 
    27             clone.transform.position = new Vector3(0,0,0);
    28 
    29         }
    30     }

    //调用协同程序来进行下载

    //另外text文件测试:

     1     IEnumerator LoadText(string textName)
     2     {
     3         using (WWW www = new WWW(m_pathURL+textName))
     4         {
     5 
     6             yield return www;
     7 
     8             if (www.error!=null)
     9             {
    10                 throw new Exception(www.error);
    11             }
    12 
    13             Debug.Log("---www.bytesDownloaded-"+www.bytesDownloaded);
    14 
    15             byte[] bytes = www.bytes;
    16 
    17             string str = System.Text.Encoding.ASCII.GetString(bytes);
    18 
    19             Debug.Log(str);
    20 
    21         }
    22     }

  • 相关阅读:
    Docker 基础 : 数据管理
    linux sudo 命令
    TeamCity : .NET Core 插件
    C# 文件下载之断点续传
    TeamCity : 配置 Build 过程
    TeamCity : Build 版本控制系统配置
    Git : SSH 协议服务器
    TeamCity : Build 基本配置
    C# 文件下载 : WinINet
    [翻译] TSMessages
  • 原文地址:https://www.cnblogs.com/siyi/p/5885425.html
Copyright © 2020-2023  润新知