Tools(四)——FileU3DBuild

目录为:Assets/Scripts/Tools/Editor,
FileU3DBuild.cs
这文件其实也只是单个文件打包成assetbundle而已.
using UnityEngine;
using UnityEditor;
using System.IO;


public class FileU3dBuild  {

    //只能选择文件,不能选择文件夹
    [@MenuItem("BoLong/Build  AssetBundles/From Files")]
    static void ExportAssetBundles(){
        //相对于项目文件夹的目录
        //例如:Assets/Scripts/TestScript.cs
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);

        //path1:Assets/Scripts
        string path1 = Path.GetDirectoryName(path);



        BuildAssetBundleOptions option = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets;//资源包编译选项

        if(path1.Length != 0)
        {
            path1 = path1.Replace("Assets/","");

            string [] fileEntries1 = Directory.GetFiles(Application.dataPath + "/" +path1);
            foreach(string fileName in fileEntries1)
            {
                //排除meta文件
                if (fileName.IndexOf(".meta") == -1)
                {
                    string filePath =  fileName.Replace("\\","/");

                    int index = filePath.LastIndexOf("/");
                    //   /TestScript.cs
                    filePath = filePath.Substring(index);

                    string localPath = "Assets/" + path1;
                    if(index > 0 )
                    {
                        //文件相对于项目文件夹的目录
                        localPath += filePath;
                    }

                    Object t = AssetDatabase.LoadMainAssetAtPath(localPath);
                    if(t != null)
                    {
                        string bundlePath = "Assets/" + path1 + "/" + t.name + ".unity3d";
                        //这在5.x里好像已经不能用了
                        BuildPipeline.BuildAssetBundle(t,null,bundlePath,option);
                    }
                }
            }
        }
        else 
        {
            Debug.LogError("path null");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值