UnityHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /***
  2. *
  3. * Title: "SUIFW" UI框架项目
  4. * 主题: Unity帮助类
  5. * Description:
  6. * 功能: 提供程序用户常用功能集。
  7. * Date: 2017
  8. * Version: 0.1版本
  9. * Modify Recoder:
  10. *
  11. *
  12. */
  13. using UnityEngine;
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. namespace SUIFW
  18. {
  19. public class UnityHelper : MonoBehaviour
  20. {
  21. //是否是第一次加载游戏,默认是
  22. public static bool isFirstLoad = true;
  23. /// <summary>
  24. /// 获取指定范围内随机数
  25. /// </summary>
  26. /// <param name="num1"></param>
  27. /// <param name="num2"></param>
  28. /// <returns></returns>
  29. public static int GetRandom(int num1, int num2)
  30. {
  31. if (num1 < num2)
  32. {
  33. return UnityEngine.Random.Range(num1, num2+1);
  34. }
  35. else
  36. {
  37. return UnityEngine.Random.Range(num2, num1+1);
  38. }
  39. }
  40. /// <summary>
  41. /// 清理内存(一般在切换场景的时候调用)
  42. /// </summary>
  43. public static void ClearMemory()
  44. {
  45. Resources.UnloadUnusedAssets();
  46. GC.Collect();
  47. }
  48. /// <summary>
  49. /// 查找子对象
  50. /// </summary>
  51. /// <param name="goParent">父对象</param>
  52. /// <param name="childName">子对象名称</param>
  53. /// <returns></returns>
  54. public static Transform FindTheChild(GameObject goParent, string childName)
  55. {
  56. Transform searchTrans = goParent.transform.Find(childName);
  57. if (searchTrans == null)
  58. {
  59. foreach (Transform trans in goParent.transform)
  60. {
  61. searchTrans = FindTheChild(trans.gameObject, childName);
  62. if (searchTrans != null)
  63. {
  64. return searchTrans;
  65. }
  66. }
  67. }
  68. return searchTrans;
  69. }
  70. /// <summary>
  71. /// 获取子物体的脚本
  72. /// </summary>
  73. /// <typeparam name="T">泛型</typeparam>
  74. /// <param name="goParent">父对象</param>
  75. /// <param name="childName">子对象名称</param>
  76. /// <returns></returns>
  77. public static T GetTheChildComponent<T>(GameObject goParent, string childName) where T : Component
  78. {
  79. Transform searchTrans = FindTheChild(goParent, childName);
  80. if (searchTrans != null)
  81. {
  82. return searchTrans.gameObject.GetComponent<T>();
  83. }
  84. else
  85. {
  86. return null;
  87. }
  88. }
  89. /// <summary>
  90. /// 给子物体添加脚本
  91. /// </summary>
  92. /// <typeparam name="T">泛型</typeparam>
  93. /// <param name="goParent">父对象</param>
  94. /// <param name="childName">子对象名称</param>
  95. /// <returns></returns>
  96. public static T AddTheChildComponent<T>(GameObject goParent, string childName) where T : Component
  97. {
  98. Transform searchTrans = FindTheChild(goParent, childName);
  99. if (searchTrans != null)
  100. {
  101. T[] theComponentsArr = searchTrans.GetComponents<T>();
  102. for (int i = 0; i < theComponentsArr.Length; i++)
  103. {
  104. if (theComponentsArr[i] != null)
  105. {
  106. Destroy(theComponentsArr[i]);
  107. }
  108. }
  109. return searchTrans.gameObject.AddComponent<T>();
  110. }
  111. else
  112. {
  113. return null;
  114. }
  115. }
  116. /// <summary>
  117. /// 给子物体添加父对象
  118. /// </summary>
  119. /// <param name="parentTrs">父对象的方位</param>
  120. /// <param name="childTrs">子对象的方位</param>
  121. public static void AddChildToParent(Transform parentTrs, Transform childTrs)
  122. {
  123. //childTrs.parent = parentTrs; //Original Method
  124. childTrs.SetParent(parentTrs,false);
  125. childTrs.localPosition = Vector3.zero;
  126. childTrs.localScale = Vector3.one;
  127. childTrs.localEulerAngles = Vector3.zero;
  128. }
  129. //加载场景的开关 ???
  130. public static void OpenLoadSceneHelper()
  131. {
  132. GameObject uiRoot = GameObject.FindGameObjectWithTag("CanvasRoot");
  133. if (uiRoot != null)
  134. {
  135. GameObject helpGo = FindTheChild(uiRoot, "LoadSceneHelper").gameObject;
  136. if (helpGo.activeInHierarchy == false)
  137. {
  138. helpGo.SetActive(true);
  139. }
  140. }
  141. }
  142. }//Class_end
  143. }