1. 程式人生 > >機器能做的事你就別湊熱鬧了╮(︶﹏︶")╭

機器能做的事你就別湊熱鬧了╮(︶﹏︶")╭

1.一樓測試…

   [MenuItem("CustomTools/Test___K %K")]
        public static void Test___K()
        {

        }
        [MenuItem("CustomTools/Test___L %L")]
        public static void Test___L()
        {

        }
        [MenuItem("CustomTools/Test___J %J")]
        public static void Test___J()
        {

        }

2.統計選中物體的子物體數量

 [MenuItem("CustomTools/CalculationChildCount")]
        public static void CalculationChildCount()
        {
            StatisticsChildCount scc = new StatisticsChildCount();
            scc.FindObjectChild(Selection.activeTransform);
            Debug.Log(scc.allObjCount);
        }
public class StatisticsChildCount
    {
        public void FindObjectChild(Transform t)
        {
            Transform[] trans = FindChild(t);
            if (trans == null)
                return;
            for (int i = 0; i < trans.Length; i++)
            {
                FindObjectChild(trans[i]);
            }

        }
        public
int allObjCount = 0; Transform[] FindChild(Transform t) { Transform[] trans = new Transform[t.childCount]; if (t.childCount > 0) { for (int i = 0; i < t.childCount; i++) { allObjCount++; trans[i] = t.GetChild(i); } return trans; } else return null; } }

3. 將選中的物體包含的所有的這個型別的物體的子物體命名為:父物體的名字+字首+子物體在當前父物體下的索引+1

   [MenuItem("CustomTools/ResetChildName")]
        public static void ResetChildName()
        {
            string prefix = "_Node:";
            Roads[] games = Selection.activeGameObject.GetComponentsInChildren<Roads>();
            for (int i = 0; i < games.Length; i++)
            {
                for (int j = 0; j < games[i].transform.childCount; j++)
                {
                    games[i].transform.GetChild(j).name = games[i].name + prefix + (j + 1);
                }
            }
            return;
        }

4. 子物體不變,父物體的位置和旋轉設定為子物體中間的位置

   [MenuItem("CustomTools/ChangeTransformParentLocation ")]
        public static void ChangeTransformParentLocation()
        {
            GameObject[] games = Selection.gameObjects;
            for (int i = 0; i < games.Length; i++)
            {
                Transform[] trans = new Transform[games[i].transform.childCount];
                Transform parent = games[i].transform;
                Vector3[] local = new Vector3[trans.Length];
                Quaternion[] lo = new Quaternion[trans.Length];
                for (int j = 0; j < games[i].transform.childCount; j++)
                {
                    local[j] = games[i].transform.GetChild(j).position;
                    lo[j] = games[i].transform.GetChild(j).rotation;
                    trans[j] = games[i].transform.GetChild(j);
                }
                int fa = (trans.Length / 2) + Random.Range(-5, 5);
                if (fa >= trans.Length || fa < 0)
                    fa = trans.Length / 2;
                parent.position = trans[fa].position;
                for (int j = 0; j < trans.Length; j++)
                {
                    trans[j].SetParent(parent);
                    trans[j].position = local[j];
                    trans[j].rotation = lo[j];
                }
            }
        }

5. 建立一個新的mesh給選中的物體,mesh為烘焙好的導航網格

  [MenuItem("CustomTools/DrawNavMesh")]
        private static void DrawNavMesh()
        {
            NavMeshTriangulation triangulation = NavMesh.CalculateTriangulation();
            Mesh mesh = new Mesh();
            mesh.vertices = triangulation.vertices;
            mesh.triangles = triangulation.indices;
            mesh.RecalculateBounds();
            Transform tran = Selection.activeTransform;
            MeshFilter filter = tran.GetComponent<MeshFilter>();
            if (filter != null)
            {
                filter.sharedMesh = mesh;
            }
        }

6. 查詢選中物體的所有子物體,使所有有該型別的子物體處於選中狀態

[MenuItem("CustomTools/SelctionObjs/Text")]
        private static void SelectionTexts()
        {
            Text[] allButton = Selection.activeGameObject.GetComponentsInChildren<Text>();
            GameObject[] allObj = new GameObject[allButton.Length];
            for (int i = 0; i < allObj.Length; i++)
            {
                allObj[i] = allButton[i].gameObject;
            }
            Selection.objects = allObj;
        }

PS:這裡型別只是一個篩選的條件

7. 複製選中物體的名字到剪下板

   [MenuItem("CustomTools/CopySelectionName %W")]
        public static void CopySelectionName()
        {
            Object[] objs = Selection.GetFiltered(typeof(Object), SelectionMode.Deep); //Assets中的檔案
            Transform[] trans = Selection.GetTransforms(SelectionMode.DeepAssets); //Scene中的檔案
            TextEditor te = new TextEditor();
            if (objs.Length > 0) //只能單選
            {
                PlayerPrefs.SetString("CopyName", objs[0].name); //儲存到本地
                te.text = objs[0].name;
                Debug.Log("--------------" + objs[0].name + "--------------");
            }
            else if (trans.Length > 0)
            {
                PlayerPrefs.SetString("CopyName", trans[0].name); //從本地取出
                te.text = trans[0].name;
                Debug.Log("--------------" + trans[0].name + "--------------");
            }
            else //不支援一些特殊的物件,比如5.0之後Hierarchy中的Scene的name
                Debug.Log("無法識別選中的目標!");
            te.SelectAll();
            te.Copy();
        } //複製選中物體的名字到剪下板

8. 使選中的物體隱藏或顯示

[MenuItem("CustomTools/SetObjectActive %Q")] //使選中的物體隱藏或顯示
        public static void SetActive()
        {
            Transform[] trans = Selection.GetTransforms(SelectionMode.DeepAssets);
            GameObject[] allGame = new GameObject[trans.Length];
            for (int i = 0; i < trans.Length; i++)
            {
                allGame[i] = trans[i].gameObject;
            }
            Undo.RecordObjects(allGame, "SetObjectActive");//註冊撤銷事件
            int activeCount = trans.Length; //獲取選中的物體數量
            for (int i = trans.Length - 1; i >= 0; i--) //對選中的目標進行篩選,隱藏的數量>=1&&不等於選中的總數量則全部開啟,否則關閉
            {
                if (trans[i].gameObject.activeInHierarchy) //得到隱藏的數量
                    activeCount--;
            }
            bool activeState = activeCount != 0;//列表中同時有顯示和隱藏,優先把所有的設定為顯示狀態
            for (int i = trans.Length - 1; i >= 0; i--) //篩選之後就是對選中的物體設定顯隱了
            {
                trans[i].gameObject.SetActive(activeState);
            }
        }

9. 複製選中的一個object的transform(只能單選)

  [MenuItem("CustomTools/CopyTransform #C")] // 複製選中的一個object的transform(只能單選)
        public static void CopyTransform()
        {
            Transform[] trans = Selection.GetTransforms(SelectionMode.DeepAssets);
            GameObject[] allGame = new GameObject[trans.Length];
            for (int i = 0; i < trans.Length; i++)
            {
                allGame[i] = trans[i].gameObject;
            }
            if (trans.Length != 1)
                return;

            PlayerPrefs.SetFloat("PositionX", trans[0].localPosition.x); //儲存在本地,防止被清零
            PlayerPrefs.SetFloat("PositionY", trans[0].localPosition.y); //儲存在本地,防止被清零
            PlayerPrefs.SetFloat("PositionZ", trans[0].localPosition.z); //儲存在本地,防止被清零
            PlayerPrefs.SetFloat("RotationX", trans[0].localRotation.x); //儲存在本地,防止被清零
            PlayerPrefs.SetFloat("RotationY", trans[0].localRotation.y); //儲存在本地,防止被清零
            PlayerPrefs.SetFloat("RotationZ", trans[0].localRotation.z); //儲存在本地,防止被清零
            PlayerPrefs.SetFloat("RotationW", trans[0].localRotation.w); //儲存在本地,防止被清零
            PlayerPrefs.SetFloat("LocalScaleX", trans[0].localScale.x); //儲存在本地,防止被清零
            PlayerPrefs.SetFloat("LocalScaleY", trans[0].localScale.y); //儲存在本地,防止被清零
            PlayerPrefs.SetFloat("LocalScaleZ", trans[0].localScale.z); //儲存在本地,防止被清零
        }

PS:至於為什麼只能單選。。。你懂的

10. 將複製的transform的屬性貼上到選中的物體上(只能單選)

        public static void PasteTransform()
        {
            Transform[] trans = Selection.GetTransforms(SelectionMode.DeepAssets);
            Undo.RecordObjects(trans, "PasteTransform");
            GameObject[] allGame = new GameObject[trans.Length];
            for (int i = 0; i < trans.Length; i++)
            {
                allGame[i] = trans[i].gameObject;
            }
            for (int i = 0; i < trans.Length; i++)
            {
                trans[i].localPosition = new Vector3(PlayerPrefs.GetFloat("PositionX"), PlayerPrefs.GetFloat("PositionY"),
                    PlayerPrefs.GetFloat("PositionZ"));
                trans[i].localRotation = new Quaternion(PlayerPrefs.GetFloat("RotationX"), PlayerPrefs.GetFloat("RotationY"),
                    PlayerPrefs.GetFloat("RotationZ"), PlayerPrefs.GetFloat("RotationW"));
                trans[i].localScale = new Vector3(PlayerPrefs.GetFloat("LocalScaleX"), PlayerPrefs.GetFloat("LocalScaleY"),
                    PlayerPrefs.GetFloat("LocalScaleZ"));
            }
        }

11. 在面板中找到一個物體並處於選中狀態(Hieraychy面板)

   [MenuItem("CustomTools/FindSelect %#X")]
        public static void FindSelect()
        {
             Selection.activeObject= GameObject.Find("Start");
        }

PS:層級很深的時候就會知道這是很有用的

12. 在面板中找到一個物體並處於選中狀態(Assets面板)

  Selection.activeObject = Resources.Load("New Folder/N/Q/W/E/R/Obbbbb");

PS:也就只有我才這麼用Resources⁽⁽ଘ( ˊᵕˋ )ଓ⁾⁾

13. 嗯。。。。稱其為三件套吧

   public static void Play()
        {
            EditorApplication.isPlaying = true;
        }

PS:之前一直想用這個功能就是找不到,現在不用了反而會用了
其他二件分別是:

 EditorApplication.isPaused = true;
   EditorApplication.isPlaying = false;

應該不用我翻譯了吧

14. 編輯器下使用協程

參考:

> http://www.mamicode.com/info-detail-515821.html

**這個是這之前用寫其他功能時用到的**
> https://user.qzone.qq.com/857686710/infocenter

    private class EditorCoroutine : IEnumerator
        {
            private Stack<IEnumerator> executionStack;

            public EditorCoroutine(IEnumerator iterator)
            {
                this.executionStack = new Stack<IEnumerator>();
                this.executionStack.Push(iterator);
            }

            public bool MoveNext()
            {
                IEnumerator i = this.executionStack.Peek();

                if (i.MoveNext())
                {
                    object result = i.Current;
                    if (result != null && result is IEnumerator)
                    {
                        this.executionStack.Push((IEnumerator)result);
                    }

                    return true;
                }
                else
                {
                    if (this.executionStack.Count > 1)
                    {
                        this.executionStack.Pop();
                        return true;
                    }
                }

                return false;
            }

            public void Reset()
            {
                throw new System.NotSupportedException("This Operation Is Not Supported.");
            }

            public object Current
            {
                get { return this.executionStack.Peek().Current; }
            }

            public bool Find(IEnumerator iterator)
            {
                return this.executionStack.Contains(iterator);
            }
        }

        private static List<EditorCoroutine> editorCoroutineList;
        private static List<IEnumerator> buffer;

        public static IEnumerator StartEditorCoroutine(IEnumerator iterator)
        {
            if (editorCoroutineList == null)
            {
                // test
                editorCoroutineList = new List<EditorCoroutine>();
            }
            if (buffer == null)
            {
                buffer = new List<IEnumerator>();
            }
            if (editorCoroutineList.Count == 0)
            {
                EditorApplication.update += Update;
            }

            // add iterator to buffer first
            buffer.Add(iterator);

            return iterator;
        }

        private static bool Find(IEnumerator iterator)
        {
            // If this iterator is already added
            // Then ignore it this time
            foreach (EditorCoroutine editorCoroutine in editorCoroutineList)
            {
                if (editorCoroutine.Find(iterator))
                {
                    return true;
                }
            }

            return false;
        }

        private static void Update()
        {
            // EditorCoroutine execution may append new iterators to buffer
            // Therefore we should run EditorCoroutine first
            editorCoroutineList.RemoveAll
            (
                coroutine => { return coroutine.MoveNext() == false; }
            );

            // If we have iterators in buffer
            if (buffer.Count > 0)
            {
                foreach (IEnumerator iterator in buffer)
                {
                    // If this iterators not exists
                    if (!Find(iterator))
                    {
                        // Added this as new EditorCoroutine
                        editorCoroutineList.Add(new EditorCoroutine(iterator));
                    }
                }

                // Clear buffer
                buffer.Clear();
            }

            // If we have no running EditorCoroutine
            // Stop calling update anymore
            if (editorCoroutineList.Count == 0)
            {
                EditorApplication.update -= Update;
            }
        }

PS:這個不是我寫的,偶然搜到的,另外好像只適用5之前,好久不用這個了

15. 給物體增加子物體。。。

  GameObject addObj;//要增加子物體的物件
        public int addObjectCount = 10; //要增加的數量
        public string prefix = "Shoot";
        public bool isAddObj = true;
        public string addObjectDividingLine = "-----我是分割線-----";
void AddChilds()
        {
            addObj = Selection.activeGameObject;
            Assert.AreNotEqual(addObj, null, "沒有找到目標");
            int objCount = addObj.transform.childCount;
            string p = prefix;
            for (int index = objCount; index < objCount + addObjectCount; index++)
            {
                GameObject clone = new GameObject();
                clone.name = prefix + index;
                clone.transform.parent = addObj.transform;
            }
        }

!!!!!!!!!!!!!!!!!!!!!!!!!!!分割!!!!!!!!!!!!!!!線!!!!!!!!!!!!!!!!!!

╮(︶﹏︶”)╭哦,日後再更新吧,有什麼想法或者建議可以一起交流