SceneController.cs 15 KB

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