SceneController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. SceneController.SetBoxCollider2DToGameObject(parentGameObj.transform.GetChild(0).gameObject);
  182. }
  183. //向Transform添加SpriteRenderer并设置资源
  184. public static void SetSpriteRendererToTransform(Transform tf, string resPath)
  185. {
  186. tf.position = Vector3.zero;
  187. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  188. if (spr == null)
  189. {
  190. tf.gameObject.AddComponent<SpriteRenderer>();
  191. spr = tf.GetComponent<SpriteRenderer>();
  192. }
  193. DressUpUtil.ChangeAssetReleaser(tf.gameObject, resPath);
  194. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  195. spr.sprite = sp;
  196. spr.size = spr.sprite.bounds.size;//将节点设置为原图大小
  197. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING);
  198. }
  199. //向GameObject添加BoxCollider2D
  200. public static void SetBoxCollider2DToGameObject(GameObject gameObject)
  201. {
  202. BoxCollider2D polygonCollider2D = gameObject.GetComponent<BoxCollider2D>();
  203. if (polygonCollider2D != null)
  204. {
  205. GameObject.Destroy(polygonCollider2D);
  206. }
  207. polygonCollider2D = gameObject.AddComponent<BoxCollider2D>();
  208. polygonCollider2D.isTrigger = true;
  209. }
  210. //传入父物体,返回体碰撞盒大小
  211. public static Vector2 GetGameObjectSize(GameObject parentObj)
  212. {
  213. Vector2 size = Vector2.one;
  214. for (int i = 0; i < parentObj.transform.childCount; i++)
  215. {
  216. GameObject childGameObj = parentObj.transform.GetChild(i).gameObject;
  217. BoxCollider2D boxCollider2D = childGameObj.GetComponent<BoxCollider2D>();
  218. if (boxCollider2D != null)
  219. {
  220. size = GetGameObjectBoundsSize(parentObj);
  221. if (parentObj.name == "Role") boxCollider2D.size = size;
  222. boxCollider2D.offset = -childGameObj.transform.localPosition;
  223. //* childGameObj.transform.localScale
  224. size = size * parentObj.transform.localScale * 100;
  225. return size;
  226. }
  227. }
  228. return Vector2.zero;
  229. }
  230. private static Vector2 GetGameObjectBoundsSize(GameObject parentObj)
  231. {
  232. float right = int.MinValue;
  233. float left = int.MaxValue;
  234. float top = int.MinValue;
  235. float bottom = int.MaxValue;
  236. for (int i = 0; i < parentObj.transform.childCount; i++)
  237. {
  238. Transform transform = parentObj.transform.GetChild(i);
  239. SpriteRenderer sr = transform.GetComponent<SpriteRenderer>();
  240. if (sr != null)
  241. {
  242. Vector2 pos = transform.localPosition;
  243. Vector2 size = sr.bounds.size / parentObj.transform.localScale.x;
  244. Debug.Log("size:" + size + " pos:" + pos);
  245. right = Math.Max(size.x / 2 + pos.x, right);
  246. left = Math.Min(pos.x - size.x / 2, left);
  247. top = Math.Max(size.y / 2 + pos.y, top);
  248. bottom = Math.Min(pos.y - size.y / 2, bottom);
  249. }
  250. }
  251. Vector2 bounds = new Vector2(right - left, top - bottom);
  252. Debug.Log("size:" + bounds);
  253. return bounds;
  254. }
  255. //设置物体中心点
  256. public static void SetGameObjectCenter(GameObject parentObj)
  257. {
  258. Transform parent = parentObj.transform; // 2.选中技算
  259. Vector3 postion = parent.position;
  260. Quaternion rotation = parent.rotation;
  261. Vector3 scale = parent.localScale;
  262. parent.position = Vector3.zero;
  263. parent.rotation = Quaternion.Euler(Vector3.zero);
  264. parent.localScale = Vector3.one;
  265. Vector3 center = Vector3.zero;
  266. Renderer[] renders = parent.GetComponentsInChildren<Renderer>();
  267. int index = 0;
  268. foreach (Transform t in parent)
  269. {
  270. string[] strs = t.name.Split('_');
  271. if (strs.Length > 1 && strs[1] == "eff") continue;//不计算特效大小
  272. Renderer render = t.GetComponent<Renderer>();
  273. if (render)
  274. {
  275. index++;
  276. center += render.bounds.center;
  277. }
  278. }
  279. center /= index;
  280. Bounds bounds = new Bounds(center, Vector3.zero);
  281. foreach (Transform t in parent)
  282. {
  283. string[] strs = t.name.Split('_');
  284. if (strs.Length > 1 && strs[1] == "eff") continue;
  285. Renderer render = t.GetComponent<Renderer>();
  286. if (render) bounds.Encapsulate(render.bounds);
  287. Debug.Log("zoya_111" + bounds);
  288. }
  289. parent.position = postion;
  290. parent.rotation = rotation;
  291. parent.localScale = scale;
  292. foreach (Transform t in parent)
  293. {
  294. string[] strs = t.parent.name.Split('_');
  295. if (strs.Length > 1 && strs[1] == "eff") continue;
  296. t.position = t.position - bounds.center;
  297. }
  298. parent.position = bounds.center + parent.position;
  299. }
  300. public static GameObject GetFirstHitObj(RaycastHit2D[] hit2Ds)
  301. {
  302. int layer = int.MinValue;
  303. GameObject gameObject = null;
  304. for (int i = 0; i < hit2Ds.Length; i++)
  305. {
  306. SpriteRenderer spr = hit2Ds[i].collider.gameObject.GetComponent<SpriteRenderer>();
  307. if (spr && spr.sortingOrder > layer)
  308. {
  309. gameObject = hit2Ds[i].collider.gameObject;
  310. layer = spr.sortingOrder;
  311. }
  312. }
  313. return gameObject;
  314. }
  315. public static void AddObjectToView(GameObject _gameObject, GoWrapper _wrapper, GGraph holder, string res, out GameObject gameObject, out GoWrapper wrapper, float scale = 100)
  316. {
  317. if (_gameObject != null)
  318. {
  319. GameObject.Destroy(_gameObject);
  320. _gameObject = null;
  321. }
  322. _gameObject = DressUpUtil.AddAnimationObj(res);
  323. _gameObject.transform.localScale = new Vector3(scale, scale, scale);
  324. if (_wrapper == null)
  325. {
  326. _wrapper = new GoWrapper(_gameObject);
  327. holder.SetNativeObject(_wrapper);
  328. }
  329. else
  330. {
  331. _wrapper.wrapTarget = _gameObject;//替换资源
  332. }
  333. wrapper = _wrapper;
  334. gameObject = _gameObject;
  335. }
  336. public static void DestroyObjectFromView(GameObject _gameObject)
  337. {
  338. if (_gameObject != null)
  339. {
  340. GameObject.Destroy(_gameObject);
  341. _gameObject = null;
  342. }
  343. }
  344. }
  345. }