SceneController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using UnityEditor;
  5. using FairyGUI;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. namespace GFGGame
  9. {
  10. public class SceneController
  11. {
  12. public static void UpdateLoginScene(GameObject sceneObj)
  13. {
  14. //背景
  15. Transform tf = sceneObj.transform.Find("Bg");
  16. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  17. var resPath = ResPathUtil.GetDressUpPath("jhsy_bg", "jpg");
  18. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  19. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  20. spr.sprite = sp;
  21. //角色
  22. DressUpUtil.UpdateBody("ui_loginrole", sceneObj, true, null);
  23. }
  24. public static void UpdateMainScene(GameObject sceneObj)
  25. {
  26. //背景
  27. Transform tf = sceneObj.transform.Find("Bg");
  28. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  29. var resPath = ResPathUtil.GetDressUpPath("zjm_bg1", "jpg");
  30. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  31. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  32. spr.sprite = sp;
  33. //角色
  34. CustomSuitData suitSavedData = CustomSuitDataManager.GetCurrentSuitList();
  35. List<int> equipDatas = suitSavedData.equipDatas;
  36. UpdateRole(equipDatas, sceneObj, false, null, true, null, false);
  37. if (suitSavedData.pic && suitSavedData.suitId > 0)
  38. {
  39. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitSavedData.suitId);
  40. DressUpUtil.UpdateBody(suitCfg.picRes, sceneObj, !string.IsNullOrEmpty(suitCfg.aniRes), suitCfg.effRes);
  41. }
  42. else
  43. {
  44. DressUpUtil.UpdateBody(null, sceneObj);
  45. }
  46. }
  47. public static void UpdateRole(int[] equipDatas, GameObject sceneObj, bool needSetMask = false, int[] exceptTypes = null, bool showAni = true, GameObject parentObj = null, bool reset = true)
  48. {
  49. if (reset)
  50. {
  51. Reset(sceneObj);
  52. }
  53. int count = equipDatas.Length;
  54. for (int i = 0; i < count; i++)
  55. {
  56. int id = (int)equipDatas[i];
  57. if (exceptTypes != null)
  58. {
  59. int subType = ItemUtilCS.GetItemSubType(id);
  60. if (Array.IndexOf(exceptTypes, subType) >= 0)
  61. {
  62. continue;
  63. }
  64. }
  65. DressUpUtil.AddItem(id, sceneObj, needSetMask, showAni, parentObj);
  66. }
  67. DressUpUtil.UpdateBody(null, sceneObj, false, null, needSetMask, parentObj);
  68. }
  69. public static void UpdateRole(List<int> equipDatas, GameObject sceneObj, bool needSetMask = false, int[] exceptTypes = null, bool showAni = true, GameObject parentObj = null, bool reset = true)
  70. {
  71. UpdateRole(equipDatas.ToArray(), sceneObj, needSetMask, exceptTypes, showAni, parentObj, reset);
  72. }
  73. public static void UpdateDialogBg(string value, GameObject sceneObj)
  74. {
  75. Transform tf = sceneObj.transform.Find("Bg");
  76. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  77. if (value == "0")
  78. {
  79. spr.sprite = null;
  80. }
  81. else
  82. {
  83. var resPath = ResPathUtil.GetDressUpPath(value, "jpg");
  84. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  85. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  86. spr.sprite = sp;
  87. }
  88. }
  89. public static void UpdateDialogPic(string value, GameObject sceneObj)
  90. {
  91. Transform tf = sceneObj.transform.Find("Pic");
  92. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  93. if (value == "0")
  94. {
  95. spr.sprite = null;
  96. }
  97. else
  98. {
  99. void UpdateDialogPicAlpha(object param)
  100. {
  101. if (spr != null)
  102. {
  103. Color c = spr.color;
  104. if (spr.enabled && c.a < 1f)
  105. {
  106. c.a += 0.05f;
  107. spr.color = c;
  108. }
  109. else
  110. {
  111. FairyGUI.Timers.inst.Remove(UpdateDialogPicAlpha);
  112. }
  113. }
  114. else
  115. {
  116. FairyGUI.Timers.inst.Remove(UpdateDialogPicAlpha);
  117. }
  118. }
  119. var resPath = ResPathUtil.GetNpcPicSPath(value);
  120. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  121. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  122. spr.sprite = sp;
  123. Color c = spr.color;
  124. c.a = 0f;
  125. spr.color = c;
  126. FairyGUI.Timers.inst.AddUpdate(UpdateDialogPicAlpha);
  127. }
  128. }
  129. public static void UpdateFightTarget(string value, GameObject sceneObj)
  130. {
  131. Transform tf = sceneObj.transform.Find("Npc");
  132. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  133. var resPath = ResPathUtil.GetNpcPicFPath(value);
  134. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  135. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  136. spr.sprite = sp;
  137. }
  138. private static void Reset(GameObject sceneObj)
  139. {
  140. //背景层次
  141. Transform bg = sceneObj.transform.Find("Bg");
  142. if (bg != null)
  143. {
  144. SpriteRenderer spr = bg.GetComponent<SpriteRenderer>();
  145. if (spr != null)
  146. {
  147. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING);
  148. spr.sortingOrder = typeCfg.defaultLayer;
  149. }
  150. }
  151. //角色清理
  152. Transform role = sceneObj.transform.Find("Role");
  153. if (role != null)
  154. {
  155. int childCount = role.childCount;
  156. for (int i = childCount - 1; i >= 0; --i)
  157. {
  158. Transform child = role.GetChild(i);
  159. if (child.gameObject.name != "Body")
  160. {
  161. GameObject.DestroyImmediate(child.gameObject);
  162. }
  163. }
  164. }
  165. }
  166. //拍照角色
  167. public static void UpdatePhotographBody(List<int> equipDatas, GameObject sceneObj, GameObject parentObj)
  168. {
  169. SceneController.UpdateRole(equipDatas, sceneObj, false, null, false, parentObj);
  170. if (EquipDataCache.cacher.IsSuitPic && EquipDataCache.cacher.suitId > 0)
  171. {
  172. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(EquipDataCache.cacher.suitId);
  173. DressUpUtil.UpdateBody(suitCfg.picRes, sceneObj, false, null, false, parentObj);
  174. }
  175. GameObject gameObject = parentObj.transform.Find("Body").gameObject;
  176. SceneController.SetBoxCollider2DToGameObject(gameObject);
  177. }
  178. //拍照场景添加单个道具
  179. public static void AddItemToScene(GameObject sceneObj, GameObject parentGameObj, int itemId, int resLayer)
  180. {
  181. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  182. parentGameObj.transform.SetParent(sceneObj.transform.Find("Scene"), false);
  183. DressUpUtil.AddItem(itemId, sceneObj, false, false, parentGameObj, resLayer);
  184. // if (parentGameObj.transform.childCount > 1)
  185. // {
  186. // if (resLayer == 1)
  187. // {
  188. // GameObject.DestroyImmediate(parentGameObj.transform.GetChild(1).gameObject);
  189. // }
  190. // else if (resLayer == 2)
  191. // {
  192. // GameObject.DestroyImmediate(parentGameObj.transform.GetChild(0).gameObject);
  193. // }
  194. // else if (resLayer == 3)
  195. // {
  196. // }
  197. // }
  198. SceneController.SetBoxCollider2DToGameObject(parentGameObj.transform.GetChild(0).gameObject);
  199. }
  200. //向Transform添加SpriteRenderer并设置资源
  201. public static void SetSpriteRendererToTransform(Transform tf, string resPath)
  202. {
  203. tf.position = Vector3.zero;
  204. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  205. if (spr == null)
  206. {
  207. tf.gameObject.AddComponent<SpriteRenderer>();
  208. spr = tf.GetComponent<SpriteRenderer>();
  209. }
  210. DressUpUtil.ChangeAssetReleaser(tf.gameObject, resPath);
  211. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  212. spr.sprite = sp;
  213. spr.size = spr.sprite.bounds.size;//将节点设置为原图大小
  214. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING);
  215. }
  216. //向GameObject添加BoxCollider2D
  217. public static void SetBoxCollider2DToGameObject(GameObject gameObject)
  218. {
  219. BoxCollider2D polygonCollider2D = gameObject.GetComponent<BoxCollider2D>();
  220. if (polygonCollider2D != null)
  221. {
  222. GameObject.Destroy(polygonCollider2D);
  223. }
  224. polygonCollider2D = gameObject.AddComponent<BoxCollider2D>();
  225. polygonCollider2D.isTrigger = true;
  226. }
  227. //传入父物体,返回体碰撞盒大小
  228. public static Vector2 GetGameObjectSize(GameObject parentObj)
  229. {
  230. Vector2 size = Vector2.one;
  231. for (int i = 0; i < parentObj.transform.childCount; i++)
  232. {
  233. GameObject childGameObj = parentObj.transform.GetChild(i).gameObject;
  234. BoxCollider2D boxCollider2D = childGameObj.GetComponent<BoxCollider2D>();
  235. if (boxCollider2D != null)
  236. {
  237. size = GetGameObjectBoundsSize(parentObj);
  238. if (parentObj.name == "Role") boxCollider2D.size = size;
  239. boxCollider2D.offset = -childGameObj.transform.localPosition;
  240. //* childGameObj.transform.localScale
  241. size = size * parentObj.transform.localScale * 100;
  242. return size;
  243. }
  244. }
  245. return Vector2.zero;
  246. }
  247. private static Vector2 GetGameObjectBoundsSize(GameObject parentObj)
  248. {
  249. float right = int.MinValue;
  250. float left = int.MaxValue;
  251. float top = int.MinValue;
  252. float bottom = int.MaxValue;
  253. for (int i = 0; i < parentObj.transform.childCount; i++)
  254. {
  255. Transform transform = parentObj.transform.GetChild(i);
  256. SpriteRenderer sr = transform.GetComponent<SpriteRenderer>();
  257. if (sr != null)
  258. {
  259. Vector2 pos = transform.localPosition;
  260. Vector2 size = sr.bounds.size / parentObj.transform.localScale.x;
  261. Debug.Log("size:" + size + " pos:" + pos);
  262. right = Math.Max(size.x / 2 + pos.x, right);
  263. left = Math.Min(pos.x - size.x / 2, left);
  264. top = Math.Max(size.y / 2 + pos.y, top);
  265. bottom = Math.Min(pos.y - size.y / 2, bottom);
  266. }
  267. }
  268. Vector2 bounds = new Vector2(right - left, top - bottom);
  269. Debug.Log("size:" + bounds);
  270. return bounds;
  271. }
  272. //设置物体中心点
  273. public static void SetGameObjectCenter(GameObject parentObj)
  274. {
  275. Transform parent = parentObj.transform; // 2.选中技算
  276. Vector3 postion = parent.position;
  277. Quaternion rotation = parent.rotation;
  278. Vector3 scale = parent.localScale;
  279. parent.position = Vector3.zero;
  280. parent.rotation = Quaternion.Euler(Vector3.zero);
  281. parent.localScale = Vector3.one;
  282. Vector3 center = Vector3.zero;
  283. Renderer[] renders = parent.GetComponentsInChildren<Renderer>();
  284. int index = 0;
  285. foreach (Transform t in parent)
  286. {
  287. string[] strs = t.name.Split('_');
  288. if (strs.Length > 1 && strs[1] == "eff") continue;//不计算特效大小
  289. Renderer render = t.GetComponent<Renderer>();
  290. if (render)
  291. {
  292. index++;
  293. center += render.bounds.center;
  294. }
  295. }
  296. center /= index;
  297. Bounds bounds = new Bounds(center, Vector3.zero);
  298. foreach (Transform t in parent)
  299. {
  300. string[] strs = t.name.Split('_');
  301. if (strs.Length > 1 && strs[1] == "eff") continue;
  302. Renderer render = t.GetComponent<Renderer>();
  303. if (render) bounds.Encapsulate(render.bounds);
  304. }
  305. parent.position = postion;
  306. parent.rotation = rotation;
  307. parent.localScale = scale;
  308. foreach (Transform t in parent)
  309. {
  310. string[] strs = t.parent.name.Split('_');
  311. if (strs.Length > 1 && strs[1] == "eff") continue;
  312. t.position = t.position - bounds.center;
  313. }
  314. parent.position = bounds.center + parent.position;
  315. }
  316. public static GameObject GetFirstHitObj(RaycastHit2D[] hit2Ds)
  317. {
  318. int layer = int.MinValue;
  319. GameObject gameObject = null;
  320. for (int i = 0; i < hit2Ds.Length; i++)
  321. {
  322. SpriteRenderer spr = hit2Ds[i].collider.gameObject.GetComponent<SpriteRenderer>();
  323. if (spr && spr.sortingOrder > layer)
  324. {
  325. gameObject = hit2Ds[i].collider.gameObject;
  326. layer = spr.sortingOrder;
  327. }
  328. }
  329. return gameObject;
  330. }
  331. public static void AddObjectToView(GameObject _gameObject, GoWrapper _wrapper, GGraph holder, string res, out GameObject gameObject, out GoWrapper wrapper, float scale = 100)
  332. {
  333. if (_gameObject != null)
  334. {
  335. SpriteRenderer spr = _gameObject.GetComponent<SpriteRenderer>();
  336. if (spr != null)
  337. {
  338. DressUpUtil.TryClearSpriteObj(_gameObject, spr.name);
  339. _gameObject = null;
  340. }
  341. }
  342. _gameObject = DressUpUtil.CreateAnimationObj(res);
  343. _gameObject.transform.localScale = new Vector3(scale, scale, scale);
  344. if (_wrapper == null)
  345. {
  346. _wrapper = new GoWrapper(_gameObject);
  347. holder.SetNativeObject(_wrapper);
  348. }
  349. else
  350. {
  351. GameObject.Destroy(_wrapper.wrapTarget);
  352. _wrapper.wrapTarget = _gameObject;//替换资源
  353. }
  354. wrapper = _wrapper;
  355. gameObject = _gameObject;
  356. }
  357. public static void DestroyObjectFromView(GameObject _gameObject, GoWrapper goWrapper)
  358. {
  359. if (_gameObject != null)
  360. {
  361. SpriteRenderer spr = _gameObject.GetComponent<SpriteRenderer>();
  362. if (spr != null)
  363. {
  364. DressUpUtil.TryClearSpriteObj(_gameObject, spr.name);
  365. _gameObject = null;
  366. }
  367. }
  368. if (goWrapper != null)
  369. {
  370. goWrapper.Dispose();
  371. }
  372. }
  373. }
  374. }