Tools(四)——FileU3DBuild

来源:互联网 发布:淘宝离开时自动回复 编辑:程序博客网 时间:2024/06/17 19:08
目录为: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");        }    }}
原创粉丝点击