Tools(二)——BytesAdd

依然是Assets/Scripts/Tools/Editor目录下
BytesAdd.cs
这个文件的代码有错,估计没使用了,又没改吧。
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;

public class BytesAdd{

    ////这个函数实际上需要选择一个文件夹,
    //这函数就是打包选中的文件夹下的所有文件(包括meta)
    //但是里面的path很有问题
    //这函数不改了,也改不了
    [@MenuItem("BoLong/Build  AssetBundles/From bytes of Files")]
    static void ExportAssetBundles(){

        //这里返回的是相对项目文件夹的路径
        //例如:Assets/MyTextures/hello.png
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        BuildAssetBundleOptions option = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets;//资源包编译选项


        if(path.Length !=0 )
        {
            path = path.Replace("Assets/","");
            //获取目录下的所有文件,不递归
            string [] fileEntries = Directory.GetFiles(Application.dataPath + "/" + path);

            foreach(string fileName in fileEntries){
                string filePath = fileName;     
                int index = filePath.LastIndexOf("/");

                //这里filePath变成这样子了:
                //  /Materials\111.mat.bytes
                filePath = filePath.Substring(index);
                int indexp = path.LastIndexOf("/");
                //这里这path是外部变量,减着减着就没了,就会报错
                //如果只执行一次的话,就是获取path文件夹所有在目录
                path = path.Substring(0,indexp);
                //这又重新把 Assets/加上去,上面已经减去过了
                string localPath = "Assets/" + path;

                if(index > 0)
                {
                    localPath += filePath;
                }
                Object t =AssetDatabase.LoadMainAssetAtPath(localPath);

                if(t != null)
                { 
                    string bundlepath = "Assets/" + path + "/" +t.name + ".bytes";

                    BuildPipeline.BuildAssetBundle(t,null,bundlepath, option);
                }
            }    
        }
    }



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值