unity Package

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;

public class Package : Editor
{

    [MenuItem("Custom Editor/Create AssetBunldes Main")]
    static void CreateAssetBunldesMain()
    {
        //获取在Project视图中选择的所有游戏对象
        Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

        //遍历所有的游戏对象
        foreach (Object obj in SelectedAsset)
        {
            //本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
            //StreamingAssets是只读路径,不能写入
            //服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
            string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";
            if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies))
            {
                Debug.Log(obj.name + "资源打包成功");
            }
            else
            {
                Debug.Log(obj.name + "资源打包失败");
            }
        }
        //刷新编辑器
        AssetDatabase.Refresh();
    }

    [MenuItem("Custom Editor/Create Scene")]
    static void CreateSceneALL()
    {
        //清空一下缓存
        Caching.CleanCache();
        string Path = Application.dataPath + "/StreamingAssets/MyScene.unity3d";
        string[] levels = { "Assets/Scene/Level.unity" };
        //打包场景
        BuildPipeline.BuildPlayer(levels, Path, BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
        AssetDatabase.Refresh();
    }

    [MenuItem("Custom Editor/Create XML All")]
    static void CreateXmlAll()
    {
        //清空一下缓存
        Caching.CleanCache();
        //获取选择的目录  
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        // 
        ArrayList List = new ArrayList();
        GetAllXmlList(path, List,true);
        GetAllDirList(path, List);
        SetBuildAsset(path, List);
    }


    //递归函数
    public static void GetAllDirList(string strBaseDir , ArrayList List)
    {
        DirectoryInfo di = new DirectoryInfo(strBaseDir);
        DirectoryInfo[] diA = di.GetDirectories();
        for (int i = 0; i < diA.Length; i++)
        {
            string locPath = diA[i].FullName.Replace("\\", "/");
            int indexTemp = locPath.LastIndexOf("/");
            //Debug.Log(locPath);
            //locPath = locPath.Substring(indexTemp + 1);
            GetAllXmlList(locPath,List,false);
            //注意:递归了
            GetAllDirList(diA[i].FullName,List);
        }
    }

    //打包文件
    public static void GetAllXmlList(string path, ArrayList List , bool isOnec)
    {
        //打包文件
        if (path.Length != 0)
        {
            //取出根目录
            if (isOnec == true)
            {
                path = path.Replace("Assets/", "");
            }
            //得到选择下的全部目录   
            string[] fileEntries = null;
            if (isOnec == true)
            {          
               fileEntries = Directory.GetFiles(Application.dataPath + "/" + path, "*.xml");
            }
            else
            {
                fileEntries = Directory.GetFiles(path, "*.xml");
            }

            //遍历全部目录
            foreach (string fileName in fileEntries)
            {
                //把目录里的反斜杠变成斜杠
                string filePath = fileName.Replace("\\", "/");
                int index = filePath.LastIndexOf("/");
                //得到目录名称
                filePath = filePath.Substring(index);
                string localPath = null;
                //设置本地路径
                if (isOnec == true)
                {
                    localPath = "Assets/" + path + filePath;
                }
                else
                {
                    localPath = "Assets" + path + filePath;
                }

                //把目录里的反斜杠变成斜杠
                localPath = localPath.Replace("\\", "/");
                localPath = localPath.Replace(Application.dataPath, "");
                //Debug.Log("本地路径:" + localPath);
                //得到打包对象
                Object obj = AssetDatabase.LoadMainAssetAtPath(localPath);

                //如果打包对象不为空
                if (obj != null)
                {
                    //把对象名改成路径名
                    int num = localPath.IndexOf("/", 7);
                    string temp = localPath.Substring(num);
                    temp = temp.Substring(1, temp.Length - 1);
                    temp = temp.Replace("/", "###");
                    temp = temp.Replace(".", "$$$");
                    obj.name = temp;
                    //Debug.Log("打包改名成功:" + obj.name);
                    //加入到链表
                    List.Add(obj);

                    //Object[] tp = new Object[1];
                    //tp[0] = obj;
                    ////设置打包
                    //string targetPath = Application.dataPath + "/StreamingAssets/" + "config.unity3d" + ".assetbundle";

                    //BuildPipeline.BuildAssetBundle(null, tp, targetPath, BuildAssetBundleOptions.CompleteAssets);
                    //Debug.Log(tp[0].name);
                }
            }
        }
    }

    /// <summary>
    /// 设置打包目录
    /// </summary>
    /// <param name="path">打包名称</param>
    /// <param name="List">打包对象</param>
    public static void SetBuildAsset(string path, ArrayList List )
    {
        ////取出根目录
        path = path.Replace("Assets/", "");
        ////设置打包
        string targetPath = Application.dataPath + "/StreamingAssets/" + path + ".assetbundle";
        //Debug.Log("Building bundle at: " + targetPath);
        Object[] temp = new Object[List.Count];
        string[] arr = new string[List.Count];
        for (int i = 0; i < temp.Length; i++)
        {
            temp[i] = (Object)List[i];
            arr[i] = temp[i].name;
            Debug.Log("@@@@@@@@@@" + temp[i].name);
        }
        //从激活的选择编译资源文件  
        //BuildPipeline.BuildAssetBundle(null, temp, targetPath, BuildAssetBundleOptions.CompleteAssets);      
        BuildPipeline.BuildAssetBundleExplicitAssetNames(temp, arr, targetPath, BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows64);
        //刷新编辑器
        AssetDatabase.Refresh();


        targetPath = "file://" + targetPath;
        WWW tempwww = new WWW(targetPath);

        string tempstring = "GameAssetBundleInventory$$$xml";
        TextAsset textAsset = tempwww.assetBundle.Load(tempstring, typeof(TextAsset)) as TextAsset;
        if(textAsset != null)
        {
            Debug.Log(textAsset.ToString());
        }
    }



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一行注释也不写

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值