AssetBundle打包资源管理

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
public class CreatAssetBundle
{
    [MenuItem("Itools/Build AssetsBundles")]
    static void BuildAllAssetsBundles()
    {
        string OutPath = Application.dataPath + "/AssetBundle1";
        BuildPipeline.BuildAssetBundles(OutPath,0,EditorUserBuildSettings.activeBuildTarget);
        AssetDatabase.Refresh();//刷新
    }
    [MenuItem("Itools/MarkAssetBundle")]
    public static void MarkAssetBundle()
    {
        AssetDatabase.RemoveUnusedAssetBundleNames();//移除没用的tag
        //G:/AssetBundle/New Unity Project/Assets/Art/Scences
        string path = Application.dataPath + "/Art/Scences";
        //绑定制定的目录
        DirectoryInfo dir = new DirectoryInfo(path);
        //获取目录中的文件
        FileSystemInfo[] fileInfo = dir.GetFileSystemInfos();
        //循环文件下所有内容
        for (int i = 0; i < fileInfo.Length; i++)
        {
            FileSystemInfo temFile = fileInfo[i];
            if (temFile is DirectoryInfo)//判断是不是文件夹(场景)
            {
                //G:/AssetBundle/New Unity Project/Assets/Art/Scences/ScenesOne
                string temPath = path + "/" + temFile.Name;
                SceneOverView(temPath);
            }
        }
        AssetDatabase.Refresh();//刷新场景
        
    }
    //对场景中的文件进行处理
    public static void SceneOverView(string path)
    {
        string textName = "Record.txt";
        string tmpPath = path + textName;
        FileStream fs = new FileStream(tmpPath, FileMode.OpenOrCreate);
        StreamWriter sw = new StreamWriter(fs);

        //存储对应关系
        Dictionary<string, string> readDic = new Dictionary<string, string>();//文本记录 
        ChangeHead(path, readDic);

        foreach (string key in readDic.Keys)
        {
            sw.Write(key);
            sw.Write(" ");
            sw.Write("\n");
        }

        sw.Close();
        fs.Close();
    }
    //截取相对路径//D:ToLuFush/Assets/Art/Scenes/Scenesone

    //scenesOne/Load
    public static void ChangeHead(string fullPath, Dictionary<string, string> theWriter)
    {
        int temCount = fullPath.IndexOf("Assets");
        int temlength = fullPath.Length;

        //Assets/Art/Scenes/Scenesone
        string replacePath = fullPath.Substring(temCount, temlength-temCount);
        //根据路径绑定Scenesone
        DirectoryInfo dir = new DirectoryInfo(fullPath);
        if (dir != null)
        {
            ListFiles(dir, replacePath, theWriter);
        }
        else
        {
            Debug.Log("路径不存在");
        }
    }
    //遍历场景中每个功能文件夹
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ScenesOne"></param>
    /// <param name="相对路径 Assets/Art/Scenes/Scenesone"></param>
    /// <param name="theWriter"></param>
    public static void ListFiles(FileSystemInfo info, string replacePath, Dictionary<string, string> theWriter)
    {
        if (!info.Exists)
        { 
            Debug.Log("文件不存在");
            return;
        }
        DirectoryInfo dir = info as DirectoryInfo;
        //取得ScenesOne中的预设信息的组合放数组中
        FileSystemInfo[] files = dir.GetFileSystemInfos();

        //循环文件夹中所有东西
        for (int i = 0; i < files.Length; i++)
        {
            FileInfo file = files[i] as FileInfo;
            if (file != null)
            {
                ChangeMark(file, replacePath, theWriter);
            }
            else
            {
                ListFiles(files[i], replacePath, theWriter);
            }

        }
    } 
    //改变物体的tag
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ScenesOne中的文件夹"></param>
    /// <param name="相对路径  Assets/Art/Scenes/Scenesone"></param>
    /// <param name="theWriter"></param>
    public static void ChangeMark(FileInfo temFile, string replacePath, Dictionary<string, string> theWriter)
    {
        if(temFile.Extension==".meta")//文件夹为空不执行,
        {
            return;
        }
        //执行说明是有实体
        string markStr=GetBundlePath(temFile,replacePath);
        //结果得到SceneOne/Load,
        ChangeAssetMark(temFile, markStr, theWriter);
    }
    //计算mart标记值是多少
    /// <summary>
    /// 
    /// </summary>
    /// <param name="Load中的预设 Cube.prefab"></param>
    /// <param name="Assets/Art/Scences/ScenesOne"></param>
    /// <returns></returns>
    public static string GetBundlePath(FileInfo file, string replacePath)
    {
        
        //G:\AssetBundle\New Unity Project\Assets\Art\Scences\ScenesOne\Load\Cube.prefab
        string temPath = file.FullName; 
        
        //G:/AssetBundle/New Unity Project/    Assets/Art/Scences/ScenesOne/   Load/ Cube.prefab
        temPath = FixedWindowsPath(temPath);
        
        int assetCount = temPath.IndexOf(replacePath);

        assetCount += replacePath.Length + 1;

        int nameCount = temPath.LastIndexOf(file.Name);

        int temLength = nameCount - assetCount;
        int temCount = replacePath.LastIndexOf("/");


        //得到了SceneOne
        string sceneHead = replacePath.Substring(temCount + 1, replacePath.Length - temCount - 1);
        

        if (temLength > 1)//Load/TestThree
        {
            //Load/TestThree/TestThree.prefab
            string subString = temPath.Substring(assetCount, temPath.Length - assetCount);
            string[] result = subString.Split("/".ToCharArray());

            //SceneOne/Load
            return sceneHead + "/" + result[0];
        }
        else
        {
            //标记为场景文件SceneOne.
            return sceneHead;
        }
    }
    public static string FixedWindowsPath(string path)
    {
        path = path.Replace("\\", "/");
        return path;
    }
    
    
    //改变资源的AssetBundles
    public static void ChangeAssetMark(FileInfo temFile, string markStr, Dictionary<string, string> theWriter)
    {
        string fullPath = temFile.FullName;
        int assetCount = fullPath.IndexOf("Assets");

        //Assets\Art\Scences\ScenesTwo\load3\Cube.prefab
        string assetPath = fullPath.Substring(assetCount, fullPath.Length - assetCount);
       


        //最关键的一步在这里
        AssetImporter importer = AssetImporter.GetAtPath(assetPath);
        importer.assetBundleName = markStr;




        if (temFile.Extension == ".Unity")//场景文件
        {
            importer.assetBundleVariant = "u3d";
        }
        else
        {
            importer.assetBundleVariant = "ld";
        }
        
        //Load  --SceneOne/load
        string modleName="";
        string[] subMark=markStr.Split('/');
       
        if(subMark.Length>1)
        {
            modleName = subMark[1];
        }
        else//SceneOne  --  SceneOne
        {
            modleName = markStr;
        }

        string modlePath = markStr.ToLower() + "." + importer.assetBundleVariant;

        if (!theWriter.ContainsKey(modleName))
        {
            theWriter.Add(modleName, modlePath);
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_35647121/article/details/80907338