Unity Resources资源打包以及加载(一)

发表于2017-01-19
评论0 5.4k浏览
一、 功能说明
实现Resources资源打包、加载

二、资源打包功能如何实现
  第1步,在Unity编辑器中创建一个空的场景。


  第2步在project文件管理器中创建Editor文件夹,并在之下创建一个CreateAssetBundle.cs脚本。CreateAssetBundle.cs用来打包文件


  第3步,将要打包的项目的Resources资源整体拖拽到Project文件管理器中。


  第4步,确定要打包的资源格式。


  第5步,CreateAssetBundle.cs中规定了打包的文件格式以及生成版本号到version.txt文件中,可自定义添加。(第一次version为1只要以后version大于1不管文件有没有更改都下载。当然也可以规定某个模型更新 )格式规定代码如下所示 :


  在使用中,可能会有更多的格式。根据需要添加即可。

using UnityEngine;

using System.Collections;

using UnityEditor;

using System.IO;

using System.Collections.Generic;

public class CreateAssetBundle : Editor

{

 

    static void FindDependencies(string strPath , List strPathList)

    {

        if (strPathList.Contains(strPath))

        {

            return;

        }

        else

        {

            

            strPathList.Add(strPath);

            string[] strArrayDependencies = AssetDatabase.GetDependencies(strPathList[strPathList.Count - 1]);

 

            foreach (var m in strArrayDependencies)

            {

                FindDependencies(m, strPathList);

            }

            return;

        }

    }

    [MenuItem("Custom Editor/Create AssetBunldes Main")]

    static void CreateAssetBunldesMain()

    {

        WriteVersion();

        Caching.CleanCache();  

        List strPathList = new List();

        //获取在Project视图中选择的所有游戏对象

        Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

        //var game =   Selection.gameObjects;

        foreach (var n in SelectedAsset)

        {

            Debug.Log(AssetDatabase.GetAssetOrScenePath(n));

            FindDependencies(AssetDatabase.GetAssetOrScenePath(n),strPathList);

        }

 

        string targetPath = Application.dataPath + "/StreamingAssets";

        if (!Directory.Exists(targetPath))

        {

            Directory.CreateDirectory(targetPath);

        }

        AssetBundleBuild[] abBuilds = new AssetBundleBuild[6];

        List strPrefab = new List();

        List strModel = new List();

        List strImage = new List();

        List strShader = new List();

        List strOther = new List();

        List strMaterials = new List();

        foreach (var p in strPathList)

        {

            //strArraty[i] = AssetDatabase.GetAssetOrScenePath(SelectedAsset[i]);

            string strCase = "null";

            if (p.Contains("."))

            {

                strCase = p.Split('.')[1];

            }

            switch (strCase.ToLower())

            {

                case "prefab":

                case "cs":

                    strPrefab.Add(p);

                    break;

                case "fbx":

                    strModel.Add(p);

                    break;

                case "shader":

                    strShader.Add(p);

                    break;

                case "mat":

                    strMaterials.Add(p);

                    break;

                case "png":

                case "jpg":

                case "tga":

                case "psd":

                    strImage.Add(p);

                    break;

                default:

                    strOther.Add(p);

                    break;

            }

        }

        abBuilds[0].assetNames = strPrefab.ToArray();

        abBuilds[0].assetBundleName = "assetBundlePrefab";

        abBuilds[1].assetNames = strModel.ToArray();

        abBuilds[1].assetBundleName = "assetBundleModel";

        abBuilds[2].assetNames = strImage.ToArray();

        abBuilds[2].assetBundleName = "assetBundleImage";

        abBuilds[3].assetNames = strShader.ToArray();

        abBuilds[3].assetBundleName = "assetBundleShader";

        abBuilds[4].assetNames = strMaterials.ToArray();

        abBuilds[4].assetBundleName = "assetBundleMaterials";

        abBuilds[5].assetNames = strOther.ToArray();

        abBuilds[5].assetBundleName = "assetBundleOther";

 

        BuildPipeline.BuildAssetBundles(targetPath, abBuilds, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android);

        ////刷新编辑器

        AssetDatabase.Refresh();

    }

    static void WriteVersion()

    {

        StreamWriter sw = new StreamWriter(Application.streamingAssetsPath+"/version.txt");

        string w = "1";

        sw.Write(w);

        sw.Close();

    }


如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引