SceneController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using UnityEditor;
  5. using FairyGUI;
  6. namespace GFGGame
  7. {
  8. public class SceneController
  9. {
  10. public static void UpdateLoginScene(GameObject sceneObj)
  11. {
  12. //背景
  13. Transform tf = sceneObj.transform.Find("Bg");
  14. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  15. var resPath = ResPathUtil.GetDressUpPath("jhsy_bg", "jpg");
  16. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  17. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  18. spr.sprite = sp;
  19. //角色
  20. DressUpUtil.UpdateBody("ui_loginrole", sceneObj, true, null);
  21. }
  22. public static void UpdateMainScene(GameObject sceneObj)
  23. {
  24. //背景
  25. Transform tf = sceneObj.transform.Find("Bg");
  26. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  27. var resPath = ResPathUtil.GetDressUpPath("zjm_bg1", "jpg");
  28. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  29. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  30. spr.sprite = sp;
  31. //角色
  32. CustomSuitData suitSavedData = CustomSuitDataManager.GetCurrentSuitList();
  33. int[] equipDatas = suitSavedData.equipDatas;
  34. UpdateRole(equipDatas, sceneObj, false);
  35. if (suitSavedData.pic > 0 && suitSavedData.suitId > 0)
  36. {
  37. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitSavedData.suitId);
  38. DressUpUtil.UpdateBody(suitCfg.picRes, sceneObj, !string.IsNullOrEmpty(suitCfg.aniRes), suitCfg.effRes);
  39. }
  40. else
  41. {
  42. DressUpUtil.UpdateBody(null, sceneObj);
  43. }
  44. }
  45. public static void UpdateRole(int[] equipDatas, GameObject sceneObj, bool needSetMask = false, int[] exceptTypes = null, bool showAni = true, GameObject parentObj = null)
  46. {
  47. Reset(sceneObj);
  48. int count = equipDatas.Length;
  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(int[] equipDatas, GameObject sceneObj, GameObject parentObj)
  159. {
  160. SceneController.UpdateRole(equipDatas, sceneObj, false, null, false, parentObj);
  161. if (EquipDataCache.cacher.IsSuitPic && 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("Body").gameObject;
  167. SceneController.SetBoxCollider2DToGameObject(gameObject);
  168. }
  169. //拍照场景添加单个道具
  170. public static void AddItemToScene(GameObject sceneObj, GameObject parentGameObj, int itemId, int layerId, 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, layerId, resLayer);
  175. SceneController.SetBoxCollider2DToGameObject(parentGameObj.transform.GetChild(0).gameObject);
  176. }
  177. //向Transform添加SpriteRenderer并设置资源
  178. public static void SetSpriteRendererToTransform(Transform tf, string resPath)
  179. {
  180. tf.position = Vector3.zero;
  181. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  182. if (spr == null)
  183. {
  184. tf.gameObject.AddComponent<SpriteRenderer>();
  185. spr = tf.GetComponent<SpriteRenderer>();
  186. }
  187. DressUpUtil.ChangeAssetReleaser(tf.gameObject, resPath);
  188. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  189. spr.sprite = sp;
  190. spr.size = spr.sprite.bounds.size;//将节点设置为原图大小
  191. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING);
  192. }
  193. //向GameObject添加BoxCollider2D
  194. public static void SetBoxCollider2DToGameObject(GameObject gameObject)
  195. {
  196. BoxCollider2D polygonCollider2D = gameObject.GetComponent<BoxCollider2D>();
  197. if (polygonCollider2D != null)
  198. {
  199. GameObject.Destroy(polygonCollider2D);
  200. }
  201. polygonCollider2D = gameObject.AddComponent<BoxCollider2D>();
  202. polygonCollider2D.isTrigger = true;
  203. }
  204. //传入父物体,返回体碰撞盒大小
  205. public static Vector2 GetGameObjectSize(GameObject parentObj)
  206. {
  207. Vector2 size = Vector2.one;
  208. for (int i = 0; i < parentObj.transform.childCount; i++)
  209. {
  210. GameObject childGameObj = parentObj.transform.GetChild(i).gameObject;
  211. BoxCollider2D boxCollider2D = childGameObj.GetComponent<BoxCollider2D>();
  212. if (boxCollider2D != null)
  213. {
  214. size = GetGameObjectSize1(parentObj);
  215. if (parentObj.name == "Role") boxCollider2D.size = size;
  216. boxCollider2D.offset = -childGameObj.transform.localPosition;
  217. //* childGameObj.transform.localScale
  218. size = size * parentObj.transform.localScale * 100;
  219. return size;
  220. }
  221. }
  222. return Vector2.zero;
  223. }
  224. private static Vector2 GetGameObjectSize1(GameObject parentObj)
  225. {
  226. float right = int.MinValue;
  227. float left = int.MaxValue;
  228. float top = int.MinValue;
  229. float bottom = int.MaxValue;
  230. for (int i = 0; i < parentObj.transform.childCount; i++)
  231. {
  232. Transform transform = parentObj.transform.GetChild(i);
  233. SpriteRenderer sr = transform.GetComponent<SpriteRenderer>();
  234. if (sr != null)
  235. {
  236. Vector2 pos = transform.localPosition;
  237. Vector2 size = sr.bounds.size / parentObj.transform.localScale.x;
  238. Debug.Log("size:" + size + " pos:" + pos);
  239. right = Math.Max(size.x / 2 + pos.x, right);
  240. left = Math.Min(pos.x - size.x / 2, left);
  241. top = Math.Max(size.y / 2 + pos.y, top);
  242. bottom = Math.Min(pos.y - size.y / 2, bottom);
  243. }
  244. }
  245. Vector2 bounds = new Vector2(right - left, top - bottom);
  246. Debug.Log("size:" + bounds);
  247. return bounds;
  248. }
  249. //设置物体中心点
  250. public static void SetGameObjectCenter(GameObject parentObj)
  251. {
  252. Transform parent = parentObj.transform; // 2.选中技算
  253. Vector3 postion = parent.position;
  254. Quaternion rotation = parent.rotation;
  255. Vector3 scale = parent.localScale;
  256. parent.position = Vector3.zero;
  257. parent.rotation = Quaternion.Euler(Vector3.zero);
  258. parent.localScale = Vector3.one;
  259. Vector3 center = Vector3.zero;
  260. Renderer[] renders = parent.GetComponentsInChildren<Renderer>();
  261. int index = 0;
  262. foreach (Transform t in parent)
  263. {
  264. string[] strs = t.name.Split('_');
  265. if (strs.Length > 1 && strs[1] == "eff") continue;//不计算特效大小
  266. Renderer render = t.GetComponent<Renderer>();
  267. if (render)
  268. {
  269. index++;
  270. center += render.bounds.center;
  271. }
  272. }
  273. center /= index;
  274. Bounds bounds = new Bounds(center, Vector3.zero);
  275. foreach (Transform t in parent)
  276. {
  277. string[] strs = t.name.Split('_');
  278. if (strs.Length > 1 && strs[1] == "eff") continue;
  279. Renderer render = t.GetComponent<Renderer>();
  280. if (render) bounds.Encapsulate(render.bounds);
  281. Debug.Log("zoya_111" + bounds);
  282. }
  283. parent.position = postion;
  284. parent.rotation = rotation;
  285. parent.localScale = scale;
  286. foreach (Transform t in parent)
  287. {
  288. string[] strs = t.parent.name.Split('_');
  289. if (strs.Length > 1 && strs[1] == "eff") continue;
  290. t.position = t.position - bounds.center;
  291. }
  292. parent.position = bounds.center + parent.position;
  293. }
  294. public static GameObject GetFirstHitObj(RaycastHit2D[] hit2Ds)
  295. {
  296. int layer = int.MinValue;
  297. GameObject gameObject = null;
  298. for (int i = 0; i < hit2Ds.Length; i++)
  299. {
  300. SpriteRenderer spr = hit2Ds[i].collider.gameObject.GetComponent<SpriteRenderer>();
  301. if (spr && spr.sortingOrder > layer)
  302. {
  303. gameObject = hit2Ds[i].collider.gameObject;
  304. layer = spr.sortingOrder;
  305. }
  306. }
  307. return gameObject;
  308. }
  309. public static void AddObjectToView(GameObject _gameObject, GoWrapper _wrapper, GGraph holder, string res, out GameObject gameObject, out GoWrapper wrapper, float scale = 100)
  310. {
  311. if (_gameObject != null)
  312. {
  313. GameObject.Destroy(_gameObject);
  314. _gameObject = null;
  315. }
  316. _gameObject = DressUpUtil.AddAnimationObj(res);
  317. _gameObject.transform.localScale = new Vector3(scale, scale, scale);
  318. if (_wrapper == null)
  319. {
  320. _wrapper = new GoWrapper(_gameObject);
  321. holder.SetNativeObject(_wrapper);
  322. }
  323. else
  324. {
  325. _wrapper.wrapTarget = _gameObject;//替换资源
  326. }
  327. wrapper = _wrapper;
  328. gameObject = _gameObject;
  329. }
  330. public static void DestroyObjectFromView(GameObject _gameObject)
  331. {
  332. if (_gameObject != null)
  333. {
  334. GameObject.Destroy(_gameObject);
  335. _gameObject = null;
  336. }
  337. }
  338. }
  339. }