CommonUtil.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public static class CommonUtil
  5. {
  6. /// <summary>
  7. /// 写成静态,可以给不同的地方进行调用 [调用完之后请调用 CleanPreloadContain 清空处理]
  8. /// </summary>
  9. public static List<int> s_Effects = new List<int>();
  10. public static List<int> s_Secondactors = new List<int>();
  11. public static List<int> s_Buffs = new List<int>();
  12. public static List<string> s_Sounds = new List<string>();
  13. public static List<string> s_Prefabs = new List<string>();
  14. public static List<string> s_FbxAnims = new List<string>();
  15. public static Shader shaderTexture = Shader.Find("Mobile/Unlit (Supports Lightmap)");
  16. public static Shader shaderProjector = Shader.Find("Projector/Multiply");
  17. public static Shader shaderDoubleRim = Shader.Find("GOD/DoubleRim");
  18. public static Shader shaderDoubleRimRed = Shader.Find("GOD/DoubleRimRed");
  19. public static Shader shaderDoubleRimRedEx = Shader.Find("GOD/DoubleRimRedEx");
  20. public static Shader shagerCustomFog = Shader.Find("GOD/CustomFog");
  21. public static string s_AssetsMappingPkgName = "assetsmapping.ab";
  22. public static void CleanPreloadContain()
  23. {
  24. s_Effects.Clear();
  25. s_Secondactors.Clear();
  26. s_Buffs.Clear();
  27. s_Sounds.Clear();
  28. s_Prefabs.Clear();
  29. s_FbxAnims.Clear();
  30. }
  31. public static void RefreshModel(Transform trans, bool useSpecShader = false)
  32. {
  33. Shader shader;
  34. if (trans == null)
  35. return;
  36. for (int i = 0; i < trans.childCount; ++i)
  37. {
  38. RefreshModel(trans.GetChild(i), useSpecShader);
  39. }
  40. Renderer r = trans.GetComponent<Renderer>();
  41. if (r != null)
  42. {
  43. foreach (Material m in r.materials)
  44. {
  45. if (m != null)
  46. {
  47. if (useSpecShader)
  48. {
  49. DebugHelper.LogWarning("[ refreshModel ]r:{0}, m:{1}, old shader:{2}, shader:{3}.", r, m, m.shader, shaderTexture);
  50. }
  51. shader = useSpecShader ? shaderTexture : Shader.Find(m.shader.name);
  52. if (shader)
  53. m.shader = shader;
  54. }
  55. }
  56. }
  57. }
  58. public static void SetGameObjectLayer(GameObject go, string layerName)
  59. {
  60. if (go == null)
  61. return;
  62. LayerMask mask = LayerMask.NameToLayer(layerName);
  63. SetGameObjectLayer(go.transform, mask);
  64. }
  65. public static void SetGameObjectLayerFrom(GameObject go, string fromLayer, string toLayer)
  66. {
  67. if (go == null)
  68. return;
  69. LayerMask fromMask = LayerMask.NameToLayer(fromLayer);
  70. LayerMask toMask = LayerMask.NameToLayer(toLayer);
  71. SetGameObjectLayerFrom(go.transform, fromMask, toMask);
  72. }
  73. public static void SetGameObjectLayer(GameObject go, int layer)
  74. {
  75. if (go == null)
  76. return;
  77. for (int i = 0; i < go.transform.childCount; ++i)
  78. {
  79. SetGameObjectLayer(go.transform.GetChild(i), layer);
  80. }
  81. go.layer = layer;
  82. }
  83. public static void SetGameObjectLayerExcept(Transform t, int layer, int exceptLayerMask)
  84. {
  85. if (t == null)
  86. return;
  87. if (exceptLayerMask != 0)
  88. {
  89. int checkMask = 1 << t.gameObject.layer;
  90. if ((checkMask & exceptLayerMask) != 0)
  91. return;
  92. }
  93. for (int i = 0; i < t.childCount; ++i)
  94. SetGameObjectLayerExcept(t.GetChild(i), layer, exceptLayerMask);
  95. t.gameObject.layer = layer;
  96. }
  97. public static void SetGameObjectLayerFrom(GameObject go, int fromLayer, int toLayer)
  98. {
  99. if (go == null)
  100. return;
  101. for (int i = 0; i < go.transform.childCount; ++i)
  102. SetGameObjectLayerFrom(go.transform.GetChild(i), fromLayer, toLayer);
  103. if (go.layer == fromLayer)
  104. go.layer = toLayer;
  105. }
  106. static void SetGameObjectLayer(Transform trans, LayerMask mask)
  107. {
  108. if (trans == null)
  109. return;
  110. for (int i = 0; i < trans.childCount; ++i)
  111. {
  112. SetGameObjectLayer(trans.GetChild(i), mask);
  113. }
  114. trans.gameObject.layer = mask;
  115. }
  116. static void SetGameObjectLayerFrom(Transform trans, LayerMask fromLayer, LayerMask toLayer)
  117. {
  118. if (trans == null)
  119. return;
  120. for (int i = 0; i < trans.childCount; ++i)
  121. {
  122. SetGameObjectLayerFrom(trans.GetChild(i), fromLayer, toLayer);
  123. }
  124. if (trans.gameObject.layer == fromLayer)
  125. trans.gameObject.layer = toLayer;
  126. }
  127. static public GameObject AddChild(GameObject parent, GameObject prefab, bool resetPosition = false, bool instantiate = true)
  128. {
  129. GameObject go = instantiate ? (GameObject.Instantiate(prefab) as GameObject) : prefab;
  130. #if UNITY_EDITOR
  131. UnityEditor.Undo.RegisterCreatedObjectUndo(go, "Create Object");
  132. #endif
  133. if (go != null && parent != null)
  134. {
  135. Transform t = go.transform;
  136. t.SetParent(parent.transform);
  137. if (resetPosition)
  138. {
  139. t.localPosition = Vector3.zero;
  140. t.localRotation = Quaternion.identity;
  141. }
  142. t.localScale = Vector3.one;
  143. go.layer = parent.layer;
  144. }
  145. return go;
  146. }
  147. public static IEnumerator WaitForUnscaleTime(float time)
  148. {
  149. float waitTime = 0f;
  150. while (waitTime < time)
  151. {
  152. yield return 1;
  153. waitTime += Time.unscaledDeltaTime;
  154. }
  155. }
  156. public static Vector3 ConvertPosToUIPos(Vector3 worldPos)
  157. {
  158. if (Camera.main == null) return worldPos;
  159. Vector3 pos1 = Camera.main.WorldToScreenPoint(worldPos);
  160. return CameraMgr.Instance.UICamera.ScreenToWorldPoint(pos1);
  161. }
  162. #region Sound
  163. static public bool GetActive(Behaviour mb)
  164. {
  165. return mb && mb.enabled && mb.gameObject.activeInHierarchy;
  166. }
  167. /// <summary>
  168. /// Play the specified audio clip.
  169. /// </summary>
  170. static private AudioSource PlaySound(AudioClip clip) { return PlaySound(clip, 1f, 1f); }
  171. /// <summary>
  172. /// Play the specified audio clip with the specified volume.
  173. /// </summary>
  174. static private AudioSource PlaySound(AudioClip clip, float volume) { return PlaySound(clip, volume, 1f); }
  175. /// <summary>
  176. /// Play the specified audio clip with the specified volume and pitch.
  177. /// </summary>
  178. static AudioListener mListener = null;
  179. static private AudioSource PlaySound(AudioClip clip, float volume, float pitch)
  180. {
  181. //volume *= soundVolume;
  182. if (clip != null && volume > 0.01f)
  183. {
  184. if (mListener == null || !GetActive(mListener))
  185. {
  186. AudioListener[] listeners = GameObject.FindObjectsOfType(typeof(AudioListener)) as AudioListener[];
  187. if (listeners != null)
  188. {
  189. for (int i = 0; i < listeners.Length; ++i)
  190. {
  191. if (GetActive(listeners[i]))
  192. {
  193. mListener = listeners[i];
  194. break;
  195. }
  196. }
  197. }
  198. if (mListener == null)
  199. {
  200. Camera cam = Camera.main;
  201. if (cam == null) cam = GameObject.FindObjectOfType(typeof(Camera)) as Camera;
  202. if (cam != null) mListener = cam.gameObject.AddComponent<AudioListener>();
  203. }
  204. }
  205. if (mListener != null && mListener.enabled && GetActive(mListener))
  206. {
  207. AudioSource source = mListener.GetComponent<AudioSource>();
  208. if (source == null) source = mListener.gameObject.AddComponent<AudioSource>();
  209. source.pitch = pitch;
  210. source.PlayOneShot(clip, volume);
  211. return source;
  212. }
  213. }
  214. return null;
  215. }
  216. public static void SortList<T>(System.Collections.Generic.List<T> list, System.Comparison<T> comparer)
  217. {
  218. for (int i = 1; i < list.Count; i++)
  219. {
  220. for (int j = 0; j < i; j++)
  221. {
  222. if (comparer(list[j], list[i]) > 0)
  223. {
  224. list.Insert(j, list[i]);
  225. list.RemoveAt(i + 1);
  226. break;
  227. }
  228. }
  229. }
  230. }
  231. public static string GetProfessionName(int type)
  232. {
  233. ProfessionType ptype = (ProfessionType)type;
  234. switch(ptype)
  235. {
  236. case ProfessionType.Pro_Type_0:
  237. return "初心者";
  238. case ProfessionType.Pro_Type_Sword:
  239. return "剑士";
  240. case ProfessionType.Pro_Type_Hunter:
  241. return "猎人";
  242. case ProfessionType.Pro_Type_Magic:
  243. return "法师";
  244. case ProfessionType.Pro_Type_Priest:
  245. return "手持";
  246. case ProfessionType.Pro_Type_Thief:
  247. return "盗贼";
  248. }
  249. return "";
  250. }
  251. public static long CalcNpcUniqueId(int mapId, int levelId, int pos, int baseId)
  252. {
  253. return mapId * 10000000 + levelId * 100000 + baseId * 10 + pos;
  254. }
  255. public static long CalcPointUniqueId(int mapId,int pointCnt,int pos)
  256. {
  257. return mapId * 1000000 + pointCnt * 10000 + pos;
  258. }
  259. public static long CalcSummonNpcUniqueId(int mapId,int num)
  260. {
  261. return mapId * 100000 + num;
  262. }
  263. #endregion
  264. }