SceneController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using UnityEditor;
  5. using FairyGUI;
  6. namespace GFGGame
  7. {
  8. public class SceneController
  9. {
  10. public static void UpdateLoginScene(GameObject sceneObj)
  11. {
  12. //背景
  13. Transform tf = sceneObj.transform.Find("Bg");
  14. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  15. var resPath = ResPathUtil.GetDressUpPath("jhsy_bg", "jpg");
  16. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  17. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  18. spr.sprite = sp;
  19. //角色
  20. DressUpUtil.UpdateBody("ui_loginrole", sceneObj, true, null);
  21. }
  22. public static void UpdateMainScene(GameObject sceneObj)
  23. {
  24. //背景
  25. Transform tf = sceneObj.transform.Find("Bg");
  26. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  27. var resPath = ResPathUtil.GetDressUpPath("zjm_bg1", "jpg");
  28. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  29. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  30. spr.sprite = sp;
  31. //角色
  32. CustomSuitData suitSavedData = CustomSuitDataManager.GetCurrentSuitList();
  33. int[] equipDatas = suitSavedData.equipDatas;
  34. UpdateRole(equipDatas, sceneObj, false);
  35. if (suitSavedData.pic > 0 && suitSavedData.suitId > 0)
  36. {
  37. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitSavedData.suitId);
  38. DressUpUtil.UpdateBody(suitCfg.picRes, sceneObj, !string.IsNullOrEmpty(suitCfg.aniRes), suitCfg.effRes);
  39. }
  40. else
  41. {
  42. DressUpUtil.UpdateBody(null, sceneObj);
  43. }
  44. }
  45. public static void UpdateRole(int[] equipDatas, GameObject sceneObj, bool needSetMask = false, int[] exceptTypes = null, bool showAni = true, GameObject parentObj = null)
  46. {
  47. Reset(sceneObj);
  48. int count = equipDatas.Length;
  49. for (int i = 0; i < count; i++)
  50. {
  51. int id = (int)equipDatas[i];
  52. if (exceptTypes != null)
  53. {
  54. int subType = ItemUtil.GetItemSubType(id);
  55. if (Array.IndexOf(exceptTypes, subType) >= 0)
  56. {
  57. continue;
  58. }
  59. }
  60. DressUpUtil.AddItem(id, sceneObj, needSetMask, showAni, parentObj);
  61. }
  62. DressUpUtil.UpdateBody(null, sceneObj, false, null, needSetMask, parentObj);
  63. }
  64. public static void UpdateDialogBg(string value, GameObject sceneObj)
  65. {
  66. Transform tf = sceneObj.transform.Find("Bg");
  67. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  68. if (value == "0")
  69. {
  70. spr.sprite = null;
  71. }
  72. else
  73. {
  74. var resPath = ResPathUtil.GetDressUpPath(value, "jpg");
  75. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  76. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  77. spr.sprite = sp;
  78. }
  79. }
  80. public static void UpdateDialogPic(string value, GameObject sceneObj)
  81. {
  82. Transform tf = sceneObj.transform.Find("Pic");
  83. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  84. if (value == "0")
  85. {
  86. spr.sprite = null;
  87. }
  88. else
  89. {
  90. void UpdateDialogPicAlpha(object param)
  91. {
  92. Color c = spr.color;
  93. if (spr.enabled && c.a < 1f)
  94. {
  95. c.a += 0.05f;
  96. spr.color = c;
  97. }
  98. else
  99. {
  100. FairyGUI.Timers.inst.Remove(UpdateDialogPicAlpha);
  101. }
  102. }
  103. var resPath = ResPathUtil.GetNpcPicSPath(value);
  104. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  105. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  106. spr.sprite = sp;
  107. Color c = spr.color;
  108. c.a = 0f;
  109. spr.color = c;
  110. FairyGUI.Timers.inst.AddUpdate(UpdateDialogPicAlpha);
  111. }
  112. }
  113. public static void UpdateFightTarget(string value, GameObject sceneObj)
  114. {
  115. Transform tf = sceneObj.transform.Find("Npc");
  116. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  117. var resPath = ResPathUtil.GetNpcPicFPath(value);
  118. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  119. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  120. spr.sprite = sp;
  121. }
  122. private static void Reset(GameObject sceneObj)
  123. {
  124. //背景层次
  125. Transform bg = sceneObj.transform.Find("Bg");
  126. if (bg != null)
  127. {
  128. SpriteRenderer spr = bg.GetComponent<SpriteRenderer>();
  129. if (spr != null)
  130. {
  131. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING);
  132. spr.sortingOrder = typeCfg.defaultLayer;
  133. }
  134. }
  135. //角色清理
  136. Transform role = sceneObj.transform.Find("Role");
  137. if (role != null)
  138. {
  139. int childCount = role.childCount;
  140. for (int i = childCount - 1; i >= 0; --i)
  141. {
  142. Transform child = role.GetChild(i);
  143. if (child.gameObject.name != "Body")
  144. {
  145. GameObject.DestroyImmediate(child.gameObject);
  146. }
  147. }
  148. }
  149. }
  150. public static void UpdatePhotographBody(int[] equipDatas, GameObject sceneObj, bool needSetMask = false, int[] exceptTypes = null, bool showAni = true, GameObject parentObj = null)
  151. {
  152. SceneController.UpdateRole(equipDatas, sceneObj, false, null, false, parentObj);
  153. if (EquipDataCache.cacher.IsSuitPic && EquipDataCache.cacher.suitId > 0)
  154. {
  155. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(EquipDataCache.cacher.suitId);
  156. DressUpUtil.UpdateBody(suitCfg.picRes, sceneObj, false, null, false, parentObj);
  157. }
  158. GameObject gameObject = sceneObj.transform.Find("Scene/Role/Body").gameObject;
  159. SceneController.PhotographAddCollider(gameObject);
  160. }
  161. public static void UpdatePhotographBgOrNpcOrBorder(GameObject sceneObj, int itemId, Transform tf, string resPath)
  162. {
  163. // Transform tf = sceneObj.transform.Find(objName);
  164. tf.position = Vector3.zero;
  165. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  166. if (spr == null)
  167. {
  168. tf.gameObject.AddComponent<SpriteRenderer>();
  169. spr = tf.GetComponent<SpriteRenderer>();
  170. }
  171. DressUpUtil.ChangeAssetReleaser(tf.gameObject, resPath);
  172. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  173. spr.sprite = sp;
  174. spr.size = spr.sprite.bounds.size;//将节点设置为原图大小
  175. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING);
  176. }
  177. //添加碰撞盒
  178. public static void PhotographAddCollider(GameObject gameObject)
  179. {
  180. BoxCollider2D polygonCollider2D = gameObject.GetComponent<BoxCollider2D>();
  181. if (polygonCollider2D != null)
  182. {
  183. GameObject.Destroy(polygonCollider2D);
  184. }
  185. polygonCollider2D = gameObject.AddComponent<BoxCollider2D>();
  186. polygonCollider2D.isTrigger = true;
  187. }
  188. //场景添加单个道具
  189. public static void AddItemToScene(GameObject sceneObj, GameObject parentGameObj, int itemId, int layerId)
  190. {
  191. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  192. // //普通层
  193. // if (itemCfg.resLayer1 > 0)
  194. // {
  195. // var parentGameObj = new GameObject(string.Format("{0}_{1}_{2}", itemId, index, itemCfg.resLayer1));
  196. parentGameObj.transform.SetParent(sceneObj.transform.Find("Scene"), false);
  197. DressUpUtil.AddItem(itemId, sceneObj, false, false, parentGameObj, layerId);
  198. SceneController.PhotographAddCollider(parentGameObj.transform.GetChild(0).gameObject);
  199. // }
  200. // //特殊层
  201. // if (itemCfg.resLayer2 > 0)
  202. // {
  203. // var parentGameObj = new GameObject(string.Format("{0}_{1}_{2}", itemId, index, itemCfg.resLayer2));
  204. // parentGameObj.transform.SetParent(sceneObj.transform.Find("Scene"), false);
  205. // DressUpUtil.AddItem(itemId, sceneObj, false, false, parentGameObj, itemCfg.resLayer2);
  206. // SceneController.PhotographAddCollider(parentGameObj.transform.GetChild(0).gameObject);
  207. // }
  208. }
  209. //传入父物体,根据所有子物体大小计算父物体边框大小
  210. public static Vector2 GetGameObjectSize(GameObject parentObj)
  211. {
  212. Vector2 size = Vector2.one;
  213. for (int i = 0; i < parentObj.transform.childCount; i++)
  214. {
  215. GameObject childGameObj = parentObj.transform.GetChild(i).gameObject;
  216. BoxCollider2D boxCollider2D = childGameObj.GetComponent<BoxCollider2D>();
  217. if (boxCollider2D != null)
  218. {
  219. return boxCollider2D.size * childGameObj.transform.localScale * parentObj.transform.localScale * 100;
  220. }
  221. }
  222. return Vector2.zero;
  223. }
  224. //设置物体中心点
  225. public static void SetGameObjectCenter(GameObject parentObj)
  226. {
  227. Transform parent = parentObj.transform;
  228. Vector3 postion = parent.position;
  229. Quaternion rotation = parent.rotation;
  230. Vector3 scale = parent.localScale;
  231. parent.position = Vector3.zero;
  232. parent.rotation = Quaternion.Euler(Vector3.zero);
  233. parent.localScale = Vector3.one;
  234. Vector3 center = Vector3.zero;
  235. Renderer[] renders = parent.GetComponentsInChildren<Renderer>();
  236. foreach (Renderer child in renders)
  237. {
  238. center += child.bounds.center;
  239. }
  240. center /= parent.GetComponentsInChildren<Transform>().Length;
  241. Bounds bounds = new Bounds(center, Vector3.zero);
  242. foreach (Renderer child in renders)
  243. {
  244. bounds.Encapsulate(child.bounds);
  245. }
  246. parent.position = postion;
  247. parent.rotation = rotation;
  248. parent.localScale = scale;
  249. foreach (Transform t in parent)
  250. {
  251. t.position = t.position - bounds.center;
  252. }
  253. parent.transform.position = bounds.center + parent.position;
  254. }
  255. //输入变换前后的向量,返回旋转角度
  256. public static float GetRotationAngle(Vector3 lastRotation, Vector3 curRotation)
  257. {
  258. float angle = Vector3.Angle(lastRotation, curRotation); //求出两向量之间的夹角
  259. Vector3 normal = Vector3.Cross(lastRotation, curRotation);//叉乘求出法线向量
  260. angle *= Mathf.Sign(Vector3.Dot(normal, Vector3.forward)); //Mathf.Sign()求符号,Vector3.Dot()求方向,求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
  261. return angle;
  262. }
  263. public static GameObject GetFirstHitObj(RaycastHit2D[] hit2Ds)
  264. {
  265. int layer = int.MinValue;
  266. GameObject gameObject = null;
  267. for (int i = 0; i < hit2Ds.Length; i++)
  268. {
  269. SpriteRenderer spr = hit2Ds[i].collider.gameObject.GetComponent<SpriteRenderer>();
  270. if (spr && spr.sortingOrder > layer)
  271. {
  272. gameObject = hit2Ds[i].collider.gameObject;
  273. layer = spr.sortingOrder;
  274. }
  275. }
  276. return gameObject;
  277. }
  278. public static void AddObjectToView(GameObject _gameObject, GoWrapper _wrapper, GGraph holder, string res, out GameObject gameObject, out GoWrapper wrapper, float scale = 100)
  279. {
  280. if (_gameObject != null)
  281. {
  282. GameObject.Destroy(_gameObject);
  283. _gameObject = null;
  284. }
  285. _gameObject = DressUpUtil.AddAnimationObj(res);
  286. _gameObject.transform.localScale = new Vector3(scale, scale, scale);
  287. if (_wrapper == null)
  288. {
  289. _wrapper = new GoWrapper(_gameObject);
  290. holder.SetNativeObject(_wrapper);
  291. }
  292. else
  293. {
  294. _wrapper.wrapTarget = _gameObject;//替换资源
  295. }
  296. wrapper = _wrapper;
  297. gameObject = _gameObject;
  298. }
  299. }
  300. }