unity自动设置Assetbundle包名

来源:互联网 发布:矩阵的雅可比矩阵 编辑:程序博客网 时间:2024/06/05 10:54

因为是针对自己项目写的,所以有些地方需要读者自己修改

上代码



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


public class CreateAllPrefab : MonoBehaviour {


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
    [MenuItem("AssetBundle/Create All Prefab")]
    static void CreateAllPrefabr()
    {
        if (EditorUtility.DisplayDialog("包名", "将会把Resources/AB/FBX目录下的fbx文件制成prefab并添加ab包名,包名即是名字是否确认?", "确定"))
        {
            DirectoryInfo raw = new DirectoryInfo(Application.dataPath + "/Resources/AB/FBX/");
            //清除多余的ab名
            ClearAssetBundlesName();
            //为其返回子目录文件夹名称的数组的路径
            foreach (DirectoryInfo dictorys in raw.GetDirectories())
            {
                string name = @dictorys.ToString().Replace(raw.ToString().Replace("/", @"\"), "");
                //检查是否存在prefab
                if (!File.Exists(dictorys.ToString() + @"\" + name + ".prefab"))
                {
                    BuildPrefab(dictorys, name);
                }
                DirectoryInfo dif = new DirectoryInfo(dictorys + @"\");
                FileInfo[] fileinfo = dif.GetFiles();
                foreach (FileInfo finf in fileinfo)
                {
                    if (finf.Extension.Equals(".prefab"))
                    {
                        //ab包名
                        string AbName = name.Substring(0, name.IndexOf("_")) + "/" + name;
                        //文件路径
                        string _assetPath = "Assets" + finf.ToString().Replace(@"\", "/").Substring(Application.dataPath.Length);
                        AssetImporter assetImporter = AssetImporter.GetAtPath(_assetPath);
                        if (assetImporter != null)
                        {
                            assetImporter.assetBundleName = AbName;
                        }
                        else
                        {
                            Debug.LogError(AbName + ".prefab 不存在,请确认是否存在该预制");
                        }
                        break;
                    }
                }
            }




            Debug.Log("设置完成");
        }
    }
    /// <summary>
    /// 制作预制并且添加换装需要的资源
    /// </summary>
    /// <param name="dictorys"></param>
    /// <param name="name"></param>
    static void BuildPrefab(DirectoryInfo dictorys, string name)
    {
        //文件路径
        string path = dictorys.ToString().Replace(@"\", "/").Substring(Application.dataPath.Length).Replace("/Resources/", "");
        GameObject go = Resources.Load(path + "/" + name) as GameObject;
        if (go != null)
        {
            go.name = name;
        }
        else
        {
            Debug.Log("资源名和文件夹名不对应,加载失败");
            return;
        }
        Texture[] m_texture = Resources.LoadAll<Texture>(path);
        List<Material> m_Material = new List<Material>();




        //生成材质球
        for (int j = 0; j < m_texture.Length - 1; j++)
        {
            Material mater = new Material(Shader.Find("Standard"));
            mater.mainTexture = m_texture[j];
            m_Material.Add(mater);
            AssetDatabase.CreateAsset(mater, "Assets/Resources/" + path.ToString() + "/Materials/" + m_texture[j].name + ".mat");
        }




        //添加材质球
        SkinnedMeshRenderer[] SMR = go.GetComponentsInChildren<SkinnedMeshRenderer>();
        for (int i = 0; i < SMR.Length; i++)
        {
            if (SMR[i].transform.name == name)
            {
                Mesh mesh = SMR[i].sharedMesh;
                Material[] arrmate = m_Material.ToArray();
                Transform[] Bones = SMR[i].bones;
                SMR[i].sharedMesh = mesh;
                SMR[i].materials = arrmate;
                SMR[i].bones = Bones;
            }
        }




        //生成prefab的路径
        string PrefabPath = "Assets/Resources/" + path.ToString() + "/" + name + ".prefab";
        PrefabUtility.CreatePrefab(PrefabPath, go);
        Debug.Log("创建了文件:" + name);
    }


    /// <summary>  
    /// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包  
    /// </summary>  
    static void ClearAssetBundlesName()
    {
        int length = AssetDatabase.GetAllAssetBundleNames().Length;
        string[] oldAssetBundleNames = new string[length];
        for (int i = 0; i < length; i++)
        {
            oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
        }
        for (int j = 0; j < oldAssetBundleNames.Length; j++)
        {
            AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true);
        }
        length = AssetDatabase.GetAllAssetBundleNames().Length;
    }
}

原创粉丝点击