SceneController.cs 16 KB

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