public void InitFromGameObject(GameObject go, bool includeChildren = false) { if (go == null) { return; } if (!includeChildren) { textUI = go.GetComponent <Text>(); inputField = go.GetComponent <InputField>(); textMesh = go.GetComponent <TextMesh>(); #if UNITY_2018_1_OR_NEWER tmpro = go.GetComponent <TMPro.TMP_Text>(); #endif writerTextDestination = go.GetComponent <IWriterTextDestination>(); } else { textUI = go.GetComponentInChildren <Text>(); inputField = go.GetComponentInChildren <InputField>(); textMesh = go.GetComponentInChildren <TextMesh>(); #if UNITY_2018_1_OR_NEWER tmpro = go.GetComponentInChildren <TMPro.TMP_Text>(); #endif writerTextDestination = go.GetComponentInChildren <IWriterTextDestination>(); } // Try to find any component with a text property if (textUI == null && inputField == null && textMesh == null && writerTextDestination == null) { Component[] allcomponents = null; if (!includeChildren) { allcomponents = go.GetComponents <Component>(); } else { allcomponents = go.GetComponentsInChildren <Component>(); } for (int i = 0; i < allcomponents.Length; i++) { var c = allcomponents[i]; textProperty = c.GetType().GetProperty("text"); if (textProperty != null) { textComponent = c; break; } } } }
// 初始化 // 获取go上的 // Text、InputField、TextMesh、TMP_Text控件 // IWriterTextDestination控件 // 或其他,拥有text属性的控件 public void InitFromGameObject(GameObject go, bool includeChildren = false) { if (go == null) { return; } // 获取到go上的 // Text、InputField、TextMesh、TMP_Text控件 /// IWriterTextDestination控件 if (!includeChildren) { textUI = go.GetComponent <Text>(); inputField = go.GetComponent <InputField>(); textMesh = go.GetComponent <TextMesh>(); #if UNITY_2018_1_OR_NEWER tmpro = go.GetComponent <TMPro.TMP_Text>(); #endif writerTextDestination = go.GetComponent <IWriterTextDestination>(); } else { // 获取孩子上的所有, // 上述控件 textUI = go.GetComponentInChildren <Text>(); inputField = go.GetComponentInChildren <InputField>(); textMesh = go.GetComponentInChildren <TextMesh>(); #if UNITY_2018_1_OR_NEWER tmpro = go.GetComponentInChildren <TMPro.TMP_Text>(); #endif writerTextDestination = go.GetComponentInChildren <IWriterTextDestination>(); } // Try to find any component with a text property // 如果上述控件 // 都没有获取到 if (textUI == null && inputField == null && textMesh == null && writerTextDestination == null) { // 获下方通用的 // Component控件 Component[] allcomponents = null; if (!includeChildren) { allcomponents = go.GetComponents <Component>(); } else { allcomponents = go.GetComponentsInChildren <Component>(); } for (int i = 0; i < allcomponents.Length; i++) { // 判断,是否有 // text属性 var c = allcomponents[i]; textProperty = c.GetType().GetProperty("text"); if (textProperty != null) { textComponent = c; break; } } } }