SceneController.cs 13 KB

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