using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using LitJson; using System.Text.RegularExpressions; namespace DemoSpace { public class BuildAssetBundle { /// /// 打包生成所有的AssetBundles包 /// [MenuItem("AssetBundleTools/BuildAllAssestBundles _`")] public static void BulidAllAB() { string strABpath = Path.Combine(Application.streamingAssetsPath, "AssetBundle"); if (!Directory.Exists(strABpath)) Directory.CreateDirectory(strABpath); BuildPipeline.BuildAssetBundles(strABpath, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.WebGL); } } } public class ModelInfo { public int index = -1; public string ModelName = null;//属性需要初始化 public string ABName = null; public string ModelPath = null; public string ABPath = null; public string PrePath = null; } public class ModelNameInfo { public string ModelName = null; } public class ModelNameInfoRoot { public List ModelInfos = new List();//属性需要初始化 } public class ModelInfoRoot { public List ModelInfos = new List();//属性需要初始化 } public class MyEditor : Editor { [MenuItem("GameObject/MyTools/AddComponent", priority = 10)] private static void NewMenuItem() { int value = 0; if (Selection.gameObjects[0]) { foreach (MeshRenderer item in Selection.gameObjects[0].GetComponentsInChildren(true)) { //item.gameObject.AddComponent(); //item.gameObject.GetComponent().index = value; //item.gameObject.GetComponent()._OriginalColor = item.gameObject.GetComponent().sharedMaterial.color; //if (item.GetComponent()) //{ // item.gameObject.GetComponent().boolSpecialShader = item.GetComponent().boolSpecialShader; // item.gameObject.GetComponent().spanNo = item.GetComponent().spanNo; // item.gameObject.GetComponent().supportAttribute = item.GetComponent().SupportAttribute; // item.gameObject.GetComponent().attributeSelectLayers = item.GetComponent().AttributeSelectLayers; //} value++; } } } [MenuItem("GameObject/MyTools/CheckModelComponet", priority = 10)] public static void CheckModelComponet() { if (Selection.gameObjects[0]) { foreach (MeshRenderer item in Selection.gameObjects[0].GetComponentsInChildren(true)) { //if (item.GetComponent()) //{ // Debug.Log(item.name); //} } } } [MenuItem("GameObject/MyTools/RemoveConstruction", priority = 10)] public static void RemoveConstruction() { if (Selection.gameObjects[0]) { foreach (Transform item in Selection.gameObjects[0].GetComponentsInChildren(true)) { //if (item.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} } } } [MenuItem("GameObject/MyTools/RemoveAll", priority = 10)] public static void RemoveAll() { if (Selection.gameObjects[0]) { foreach (LODGroup item in Selection.gameObjects[0].GetComponentsInChildren(true)) { //if (item.gameObject.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.gameObject.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.gameObject.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.gameObject.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.gameObject.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.gameObject.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.gameObject.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.gameObject.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.gameObject.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.transform.Find("model")) //{ // DestroyImmediate(item.transform.Find("model").gameObject); //} //if (item.transform.Find("empty")) //{ // DestroyImmediate(item.transform.Find("empty").gameObject); //} //if (item.gameObject.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} //if (item.gameObject.GetComponent()) //{ // DestroyImmediate(item.gameObject.GetComponent()); //} } } } //[MenuItem("GameObject/MyTools/CheckModelTran", priority = 10)] //public static void CheckModelTran() //{ // if (Selection.gameObjects[0]) // { // foreach (MeshRenderer item in Selection.gameObjects[0].GetComponentsInChildren(true)) // { // if (item.transform.localPosition != Vector3.zero) // { // //DestroyImmediate(item.gameObject.GetComponent()); // } // } // } //} //[MenuItem("GameObject/MyTools/CreatePrefabs", priority = 10)] //public static void CreatePrefabs() //{ // Transform tParent = ((GameObject)Selection.activeObject).transform; // object tempPrefab; // int i = 0; // foreach (Transform t in tParent) // { // string path = "Assets/Resources/Prefab/01/prefab" + i; // if (!Directory.Exists(path)) // Directory.CreateDirectory(path); // string fullPath = string.Format("{0}/{1}.prefab", path, t.name); // bool savePrefabResult; // PrefabUtility.SaveAsPrefabAsset(t.gameObject, fullPath, out savePrefabResult); // //GameObject.DestroyImmediate(t.gameObject); // AssetDatabase.SaveAssets(); // AssetDatabase.Refresh(); // if (!savePrefabResult) // Debug.LogError("保存失败 : " + fullPath); // //tempPrefab = PrefabUtility.CreateEmptyPrefab("Assets/Prefab/prefab" + i + ".prefab"); // //tempPrefab = PrefabUtility.ReplacePrefab(t.gameObject, tempPrefab); // i++; // } // AssetDatabase.Refresh(); //} [MenuItem("GameObject/MyTools/SaveJsonData", false, 10)] static void SaveData() { //GameObject obj = new GameObject("ItemA");//创建新物体 //GameObjectUtility.SetParentAndAlign(obj, menuCommand.context as GameObject);//设置父节点为当前选中物体 //Undo.RegisterCreatedObjectUndo(obj, "Create" + obj.name);//注册到Undo系统,允许撤销 //Selection.activeObject = obj;//将新建物体设为当前选中物体 Transform tParent = ((GameObject)Selection.activeObject).transform; object tempPrefab; ModelInfoRoot modelInfoRoot = new ModelInfoRoot(); foreach (MeshRenderer item in tParent.GetComponentsInChildren(true)) { ModelInfo info = new ModelInfo(); info.ModelName = item.transform.name; info.ABName = item.transform.name; modelInfoRoot.ModelInfos.Add(info); } var vbasePath = Path.Combine(Application.streamingAssetsPath, "ObjData"); DirectoryInfo dir = new DirectoryInfo(vbasePath); if (!dir.Exists) dir.Create(); var vconfigPath = Path.Combine(vbasePath, "ModelInfo.json"); FileInfo fileInfo = new FileInfo(vconfigPath); if (fileInfo.Exists) fileInfo.Delete(); StreamWriter writer = fileInfo.CreateText(); string jsonStr = ParseJsonData(JsonMapper.ToJson(modelInfoRoot)); writer.Write(jsonStr); writer.Flush(); writer.Dispose(); writer.Close(); AssetDatabase.Refresh(); } public static string ParseJsonData(string json) { Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})"); string targetJson = reg.Replace(json, delegate (Match m) { return ((char)System.Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); }); return targetJson; } static List m_listPath = new List(); public static string GetPath(Transform target) { string path = string.Empty; Transform tran = target; while (tran.parent != null) { tran = tran.parent; if (path == string.Empty) path = tran.name; else path = tran.name + "/" + path; } return path; } public static string GetChildPath(Transform trans, string childName) { m_listPath.Clear(); FindChildGameObject(trans.gameObject, childName); string path = ""; for (int i = 1; i < m_listPath.Count; i++) { path += m_listPath[i]; if (i != m_listPath.Count - 1) path += "/"; } return path; } public static GameObject FindChildGameObject(GameObject parent, string childName) { m_listPath.Add(parent.name); if (parent.name == childName) { return parent; } if (parent.transform.childCount < 1) { return null; } GameObject obj = null; for (int i = 0; i < parent.transform.childCount; i++) { GameObject go = parent.transform.GetChild(i).gameObject; obj = FindChildGameObject(go, childName); if (obj != null) { break; } } return obj; } [MenuItem("GameObject/MyTools/CreatTrans", false, 10)] static void CreatTrans() { foreach (Transform item in Selection.activeTransform.GetComponentsInChildren(true)) { for (int i = -11; i < 17; i++) { GameObject g = new GameObject(); g.transform.parent = Selection.gameObjects[0].transform; g.name = (i * 4).ToString() + "_Up"; g.transform.localPosition = new Vector3(i * 4, -0.65f, 0.8f); g.transform.localEulerAngles = Vector3.zero; g.transform.localScale = Vector3.one; //g.AddComponent(); GameObject g1 = new GameObject(); g1.transform.parent = Selection.gameObjects[0].transform; g1.name = (i * 4).ToString() + "_Down"; g1.transform.localPosition = new Vector3(i * 4, -0.65f, -0.8f); g1.transform.localEulerAngles = Vector3.zero; g1.transform.localScale = Vector3.one; //g1.AddComponent(); } } } [MenuItem("GameObject/MyTools/CalObjCount", false, 10)] static void CalObjCount() { int value = 0; for (int i = 0; i < Selection.gameObjects[0].transform.childCount; i++) { //int x = Selection.gameObjects[0].transform.GetComponent().oprateObjs.Count; //value += x; } Debug.Log(value); } [MenuItem("GameObject/MyTools/ClearABNames", false, 10)] static void ClearABNames() { AssetDatabase.RemoveUnusedAssetBundleNames(); } //[MenuItem("GameObject/MyTools/AddLODComponent", false, 10)] //static void AddLODComponent() //{ // if (Selection.gameObjects[0]) // { // foreach (MeshRenderer item in Selection.gameObjects[0].GetComponentsInChildren(true)) // { // //item.isChildRender = true; // //Vector3 pos = item.transform.localPosition; // //Vector3 rot = item.transform.localEulerAngles; // //Vector3 sca = item.transform.localScale; // //GameObject temp1 = new GameObject(); // //temp1.name = item.name; // //temp1.transform.parent = item.transform.parent; // //temp1.transform.localPosition = Vector3.zero; // //temp1.transform.localEulerAngles = Vector3.zero; // //temp1.transform.localScale = Vector3.one; // //item.transform.parent = temp1.transform; // //item.name = "model"; // //item.transform.localPosition = pos; // //item.transform.localEulerAngles = rot; // //item.transform.localScale = sca; // //UnityEditorInternal.ComponentUtility.CopyComponent(item); // //UnityEditorInternal.ComponentUtility.PasteComponentAsNew(temp1); // //temp1.AddComponent(); // //GameObject temp2 = new GameObject(); // //temp2.name = "empty"; // //temp2.transform.parent = item.transform.parent; // //temp2.transform.localPosition = Vector3.zero; // //temp2.transform.localEulerAngles = Vector3.zero; // //temp2.transform.localScale = Vector3.one; // //MeshRenderer[] renders = new MeshRenderer[1]; // //renders[0] = item.gameObject.GetComponent(); // //LOD[] lods = new LOD[2]; // //lods[0] = new LOD(0.3f, renders); // //lods[1] = new LOD(0.1f, null); // //temp1.GetComponent().SetLODs(lods); // //temp1.GetComponent().RecalculateBounds(); // //DestroyImmediate(item.gameObject.GetComponent()); // } // } //} [MenuItem("GameObject/MyTools/SaveModelNameJson", false, 10)] static void SaveModelNameJson() { //GameObject obj = new GameObject("ItemA");//创建新物体 //GameObjectUtility.SetParentAndAlign(obj, menuCommand.context as GameObject);//设置父节点为当前选中物体 //Undo.RegisterCreatedObjectUndo(obj, "Create" + obj.name);//注册到Undo系统,允许撤销 //Selection.activeObject = obj;//将新建物体设为当前选中物体 Transform tParent = ((GameObject)Selection.activeObject).transform; object tempPrefab; int i = 0; ModelNameInfoRoot modelInfoRoot = new ModelNameInfoRoot(); foreach (MeshRenderer item in tParent.GetComponentsInChildren(true)) { ModelNameInfo info = new ModelNameInfo(); info.ModelName = item.transform.name; modelInfoRoot.ModelInfos.Add(info); AssetDatabase.SaveAssets(); i++; } Debug.Log(i); var vbasePath = Path.Combine(Application.streamingAssetsPath, "ObjData"); DirectoryInfo dir = new DirectoryInfo(vbasePath); if (!dir.Exists) dir.Create(); var vconfigPath = Path.Combine(vbasePath, "ModelInfo.json"); FileInfo fileInfo = new FileInfo(vconfigPath); if (fileInfo.Exists) fileInfo.Delete(); StreamWriter writer = fileInfo.CreateText(); string jsonStr = ParseJsonData(JsonMapper.ToJson(modelInfoRoot)); writer.Write(jsonStr); writer.Flush(); writer.Dispose(); writer.Close(); AssetDatabase.Refresh(); } [MenuItem("GameObject/MyTools/CreateModel", false, 10)] static void CreateModel() { foreach (MeshRenderer item in Selection.gameObjects[0].transform.GetComponentsInChildren(true)) { if (item.name == "编号牌") { string index = item.transform.parent.parent.name[2].ToString(); GameObject right = new GameObject(); GameObject wrong = new GameObject(); string[] names = new string[] { "敏行", "敏学", "敏知", "敏慧", "毓秀" }; List tempList = new List(); for (int i = 0; i < names.Length; i++) { GameObject temp = Instantiate(item.gameObject); tempList.Add(temp); } right.name = "正确"; right.transform.SetParent(item.transform); right.transform.localEulerAngles = Vector3.zero; right.transform.localPosition = Vector3.zero; right.transform.localScale = Vector3.one; wrong.name = "错误"; wrong.transform.SetParent(item.transform); wrong.transform.localEulerAngles = Vector3.zero; wrong.transform.localPosition = Vector3.zero; wrong.transform.localScale = Vector3.one; for (int i = 0; i < tempList.Count; i++) { tempList[i].transform.SetParent(right.transform); tempList[i].name = names[i]; tempList[i].transform.localEulerAngles = Vector3.zero; tempList[i].transform.localPosition = Vector3.zero; tempList[i].transform.localScale = Vector3.one; tempList[i].GetComponent().material = Findmat("Assets/Arts/Mat/电线杆/编号牌/" + names[i] + "/" + names[i] + "低压/" + index + ".mat"); } } } } public static Material Findmat(string path) { Material mat = (Material)AssetDatabase.LoadAssetAtPath(path, typeof(Material)); return mat; } }