Guest User

Untitled

a guest
Apr 27th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. public static class GameObjectUtil
  2. {
  3.  
  4. #if UNITY_EDITOR
  5.  
  6. public static GameObject FindGameObject(this GameObject obj, string name, bool includeInactive = true, bool showError = true)
  7. {
  8. Transform[] trs = obj.GetComponentsInChildren<Transform>(true);
  9. foreach (Transform t in trs)
  10. {
  11. if (t.name == name)
  12. {
  13. return t.gameObject;
  14. }
  15. }
  16.  
  17. if (showError)
  18. Debug.LogErrorFormat("ERROR: Cannot find {0}(type {1}) for {2}, please check!", name, obj.GetType(), obj);
  19.  
  20. return null;
  21. }
  22.  
  23. public static bool BindComponent<T>(this GameObject obj, out T component,
  24. string name, bool includeInactive = true, bool showError = true) where T : Component
  25. {
  26. component = null;
  27.  
  28. T temp = null;
  29.  
  30. Transform[] trs = obj.GetComponentsInChildren<Transform>(true);
  31. foreach (Transform t in trs)
  32. {
  33. if (t.name == name)
  34. {
  35. var c = t.GetComponent<T>();
  36. if (c != null)
  37. {
  38. if (temp != null)
  39. {
  40. if (showError)
  41. Debug.LogErrorFormat("ERROR: There is more than 1 Component name of {0}, Please check it", name);
  42.  
  43. return false;
  44. }
  45.  
  46. temp = c;
  47. }
  48. else
  49. {
  50. if (showError)
  51. Debug.LogErrorFormat("ERROR: Cannot find {0} component in {1} Obj", typeof(T), name);
  52. }
  53. }
  54. }
  55.  
  56. if (temp != null)
  57. {
  58. component = temp;
  59. return true;
  60. }
  61.  
  62. if (showError)
  63. Debug.LogErrorFormat("ERROR: Cannot find {0}(type {1}) for {2}, please check!", name, obj.GetType(), obj);
  64.  
  65. return false;
  66. }
  67.  
  68. #endif
  69.  
  70. }
Add Comment
Please, Sign In to add comment