SceneController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. //传入父物体,返回体碰撞盒大小
  235. public static Vector2 GetGameObjectSize(GameObject parentObj)
  236. {
  237. Vector2 size = Vector2.one;
  238. for (int i = 0; i < parentObj.transform.childCount; i++)
  239. {
  240. GameObject childGameObj = parentObj.transform.GetChild(i).gameObject;
  241. BoxCollider2D boxCollider2D = childGameObj.GetComponent<BoxCollider2D>();
  242. if (boxCollider2D != null)
  243. {
  244. size = GetGameObjectBoundsSize(parentObj);
  245. if (parentObj.name == "Role") boxCollider2D.size = size;
  246. boxCollider2D.offset = -childGameObj.transform.localPosition;
  247. //* childGameObj.transform.localScale
  248. size = size * parentObj.transform.localScale * 100;
  249. return size;
  250. }
  251. }
  252. return Vector2.zero;
  253. }
  254. private static Vector2 GetGameObjectBoundsSize(GameObject parentObj)
  255. {
  256. float right = int.MinValue;
  257. float left = int.MaxValue;
  258. float top = int.MinValue;
  259. float bottom = int.MaxValue;
  260. for (int i = 0; i < parentObj.transform.childCount; i++)
  261. {
  262. Transform transform = parentObj.transform.GetChild(i);
  263. SpriteRenderer sr = transform.GetComponent<SpriteRenderer>();
  264. if (sr != null)
  265. {
  266. Vector2 pos = transform.localPosition;
  267. Vector2 size = sr.bounds.size / parentObj.transform.localScale.x;
  268. Debug.Log("size:" + size + " pos:" + pos);
  269. right = Math.Max(size.x / 2 + pos.x, right);
  270. left = Math.Min(pos.x - size.x / 2, left);
  271. top = Math.Max(size.y / 2 + pos.y, top);
  272. bottom = Math.Min(pos.y - size.y / 2, bottom);
  273. }
  274. }
  275. Vector2 bounds = new Vector2(right - left, top - bottom);
  276. Debug.Log("size:" + bounds);
  277. return bounds;
  278. }
  279. //设置物体中心点
  280. public static void SetGameObjectCenter(GameObject parentObj)
  281. {
  282. Transform parent = parentObj.transform; // 2.选中技算
  283. Vector3 postion = parent.position;
  284. Quaternion rotation = parent.rotation;
  285. Vector3 scale = parent.localScale;
  286. parent.position = Vector3.zero;
  287. parent.rotation = Quaternion.Euler(Vector3.zero);
  288. parent.localScale = Vector3.one;
  289. Vector3 center = Vector3.zero;
  290. Renderer[] renders = parent.GetComponentsInChildren<Renderer>();
  291. int index = 0;
  292. foreach (Transform t in parent)
  293. {
  294. string[] strs = t.name.Split('_');
  295. if (strs.Length > 1 && strs[1] == "eff") continue;//不计算特效大小
  296. Renderer render = t.GetComponent<Renderer>();
  297. if (render)
  298. {
  299. index++;
  300. center += render.bounds.center;
  301. }
  302. }
  303. center /= index;
  304. Bounds bounds = new Bounds(center, Vector3.zero);
  305. foreach (Transform t in parent)
  306. {
  307. string[] strs = t.name.Split('_');
  308. if (strs.Length > 1 && strs[1] == "eff") continue;
  309. Renderer render = t.GetComponent<Renderer>();
  310. if (render) bounds.Encapsulate(render.bounds);
  311. }
  312. parent.position = postion;
  313. parent.rotation = rotation;
  314. parent.localScale = scale;
  315. foreach (Transform t in parent)
  316. {
  317. string[] strs = t.parent.name.Split('_');
  318. if (strs.Length > 1 && strs[1] == "eff") continue;
  319. t.position = t.position - bounds.center;
  320. }
  321. parent.position = bounds.center + parent.position;
  322. }
  323. public static GameObject GetFirstHitObj(RaycastHit2D[] hit2Ds)
  324. {
  325. int layer = int.MinValue;
  326. GameObject gameObject = null;
  327. for (int i = 0; i < hit2Ds.Length; i++)
  328. {
  329. SpriteRenderer spr = hit2Ds[i].collider.gameObject.GetComponent<SpriteRenderer>();
  330. if (spr && spr.sortingOrder > layer)
  331. {
  332. gameObject = hit2Ds[i].collider.gameObject;
  333. layer = spr.sortingOrder;
  334. }
  335. }
  336. return gameObject;
  337. }
  338. public static void AddObjectToView(GameObject _gameObject, GoWrapper _wrapper, GGraph holder, string res, out GameObject gameObject, out GoWrapper wrapper, float scale = 100)
  339. {
  340. if (_gameObject != null)
  341. {
  342. GameObject.DestroyImmediate(_gameObject);
  343. }
  344. _gameObject = DressUpUtil.CreateAnimationObj(res);
  345. _gameObject.transform.localScale = new Vector3(scale, scale, scale);
  346. if (_wrapper == null)
  347. {
  348. _wrapper = new GoWrapper(_gameObject);
  349. holder.SetNativeObject(_wrapper);
  350. }
  351. else
  352. {
  353. GameObject.Destroy(_wrapper.wrapTarget);
  354. _wrapper.wrapTarget = _gameObject;//替换资源
  355. }
  356. wrapper = _wrapper;
  357. gameObject = _gameObject;
  358. }
  359. public static void DestroyObjectFromView(GameObject _gameObject, GoWrapper goWrapper)
  360. {
  361. if (_gameObject != null)
  362. {
  363. GameObject.DestroyImmediate(_gameObject);
  364. }
  365. if (goWrapper != null)
  366. {
  367. goWrapper.Dispose();
  368. }
  369. }
  370. }
  371. }