SceneController.cs 15 KB

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