guide:
Because it is inevitable to obtain sub-objects or collections of sub-objects many times in the project, write a separate class to do these operations. Then in the actual project, you only need to use transform or gameobject to call these methods to get the data quickly, and you don't need to write it in each individual class yourself.
code show as below:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using UnityEngine; 6 7 8 public static partial class ExtentionMethod 9 { 10 /// <summary> 11 /// 获取子对象变换集合 12 /// </summary> 13 /// <param name="obj"></param> 14 /// <returns></returns> 15 public static List<Transform> GetChildCollection(this Transform obj) 16 { 17 List<Transform> list = new List<Transform>(); 18 for (int i = 0; i < obj.childCount; i++) 19 { 20 list.Add(obj.GetChild(i)); 21 } 22 return list; 23 } 24 25 /// <summary> 26 /// 获取子对象集合 27 /// </summary> 28 /// <param name="obj"></param> 29 /// <returns></returns> 30 public static List<GameObject> GetChildCollection(this GameObject obj) 31 { 32 var list = obj.transform.GetChildCollection(); 33 return list.ConvertAll(T => T.gameObject); 34 } 35 36 public static Transform GetRootParent(this Transform obj) 37 { 38 Transform Root = obj.parent; 39 while (Root.parent != null ) 40 { 41 // Root = Root.root; // transform.root, the method can directly get the top parent node. 42 Root = Root.parent; 43 } 44 return Root; 45 } 46 47 /// <summary> 48 /// Add all the components on the source object to the target object 49 /// </summary> 50 / // <param name="origin"> Origin object </param> 51 /// <param name="target"> Target object </param> 52 public static void CopyComponent(GameObject origin, GameObject target) 53 { 54 var originComs = origin.GetComponents<Component>(); 55 foreach (var item in originComs) 56 { 57 target.AddComponent(item.GetType()); 58 } 59 } 60 61 /// <summary> 62 /// 改变游戏脚本 63 /// </summary> 64 /// <param name="origin"></param> 65 /// <param name="target"></param> 66 public static void ChangeScriptTo( this MonoBehaviour origin, MonoBehaviour target) 67 { 68 target.enabled = true ; 69 origin.enabled = false ; 70 } 71 72 73 /// <summary> 74 /// Search from the child objects of the current object and return a linked list of active game objects identified by tag. If not found, it will be Empty. 75 /// </summary> 76 /// <param name="obj">Object Transform </param> 77 /// <param name="tag"> tag </param> 78 /// <param name="transList"> result Transform collection </param> // recursively on a parent object Traverse, if the tag of a sub-object matches the given tag, store the sub-object in the linked list array 79 public static void FindGameObjectsWithTagRecursive( this Transform obj, string tag, ref List<Transform> transList) 80 { 81 foreach ( var item in obj.transform.GetChildCollection()) 82 { 83 // If the child object has child objects, recursively traverse the child objects of the child object 84 if (item.childCount > 0 ) 85 { 86 item.FindGameObjectsWithTagRecursive(tag, ref transList); 87 } 88 89 if (item .tag == tag) 90 { 91 transList.Add(item); 92 } 93 } 94 } 95 96 public static void FindGameObjectsWithTagRecursive( this GameObject obj, string tag, ref List<GameObject> objList) 97 { 98 List<Transform> list = new List<Transform>(); 99 obj.transform.FindGameObjectsWithTagRecursive(tag, ref list); 100 101 objList.AddRange(list.ConvertAll(T => T.gameObject)); 102 } 103 104 /// <summary> 105 /// 从父对象中查找组件 106 /// </summary> 107 /// <typeparam name="T">组件类型</typeparam> 108 /// <param name="com"> Object component </param> 109 /// <param name="parentLevel"> Look up the level, use 1 to represent the closest level to this object < /param> 110 /// <param name="searchDepth"> Search depth </param> 111 /// <returns> The search is successful and returns the corresponding component object, otherwise it returns null </returns> 112 public static T GetComponentInParent<T> ( this Component com, int parentLevel = 1 , int searchDepth = int .MaxValue) where T : Component 113 { 114 searchDepth--; 115 116 if (com != null && searchDepth > 0) 117 { 118 var component = com.transform.parent.GetComponent<T>(); 119 if (component != null) 120 { 121 parentLevel--; 122 if (parentLevel == 0) 123 { 124 return component; 125 } 126 } 127 128 return com.transform.parent.GetComponentInParent<T>(parentLevel, searchDepth); 129 } 130 131 return null; 132 } 133 }
Added: Three ways to call other script functions in Unity
The first type: the called script function is of static type, and the script name.function name() is used directly when calling. Very impractical~
The second: GameObject.Find("The name of the object where the script is located").SendMessage("Function name"); This method can call public and private type functions
The third type: GameObject.Find("The name of the object where the script is located").GetComponent<script name>().Function name(); This method can only call public type functions