SceneController.cs 16 KB

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