SceneController.cs 15 KB

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