SceneController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using UnityEditor;
  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. //背景
  24. Transform tf = sceneObj.transform.Find("Bg");
  25. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  26. var resPath = ResPathUtil.GetDressUpPath("zjm_bg1", "jpg");
  27. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  28. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  29. spr.sprite = sp;
  30. //角色
  31. CustomSuitData suitSavedData = CustomSuitDataManager.GetCurrentSuitList();
  32. int[] equipDatas = suitSavedData.equipDatas;
  33. UpdateRole(equipDatas, sceneObj, false);
  34. if (suitSavedData.pic > 0 && suitSavedData.suitId > 0)
  35. {
  36. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitSavedData.suitId);
  37. DressUpUtil.UpdateBody(suitCfg.picRes, sceneObj, !string.IsNullOrEmpty(suitCfg.aniRes), suitCfg.effRes);
  38. }
  39. else
  40. {
  41. DressUpUtil.UpdateBody(null, sceneObj);
  42. }
  43. }
  44. public static void UpdateRole(int[] equipDatas, GameObject sceneObj, bool needSetMask = false, int[] exceptTypes = null, bool showAni = true, GameObject parentObj = null)
  45. {
  46. Reset(sceneObj);
  47. int count = equipDatas.Length;
  48. for (int i = 0; i < count; i++)
  49. {
  50. int id = (int)equipDatas[i];
  51. if (exceptTypes != null)
  52. {
  53. int subType = ItemUtil.GetItemSubType(id);
  54. if (Array.IndexOf(exceptTypes, subType) >= 0)
  55. {
  56. continue;
  57. }
  58. }
  59. DressUpUtil.AddItem(id, sceneObj, needSetMask, showAni, parentObj);
  60. }
  61. DressUpUtil.UpdateBody(null, sceneObj, false, null, needSetMask, parentObj);
  62. }
  63. public static void UpdateDialogBg(string value, GameObject sceneObj)
  64. {
  65. Transform tf = sceneObj.transform.Find("Bg");
  66. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  67. if (value == "0")
  68. {
  69. spr.sprite = null;
  70. }
  71. else
  72. {
  73. var resPath = ResPathUtil.GetDressUpPath(value, "jpg");
  74. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  75. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  76. spr.sprite = sp;
  77. }
  78. }
  79. public static void UpdateDialogPic(string value, GameObject sceneObj)
  80. {
  81. Transform tf = sceneObj.transform.Find("Pic");
  82. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  83. if (value == "0")
  84. {
  85. spr.sprite = null;
  86. }
  87. else
  88. {
  89. void UpdateDialogPicAlpha(object param)
  90. {
  91. Color c = spr.color;
  92. if (spr.enabled && c.a < 1f)
  93. {
  94. c.a += 0.05f;
  95. spr.color = c;
  96. }
  97. else
  98. {
  99. FairyGUI.Timers.inst.Remove(UpdateDialogPicAlpha);
  100. }
  101. }
  102. var resPath = ResPathUtil.GetNpcPicSPath(value);
  103. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  104. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  105. spr.sprite = sp;
  106. Color c = spr.color;
  107. c.a = 0f;
  108. spr.color = c;
  109. FairyGUI.Timers.inst.AddUpdate(UpdateDialogPicAlpha);
  110. }
  111. }
  112. public static void UpdateFightTarget(string value, GameObject sceneObj)
  113. {
  114. Transform tf = sceneObj.transform.Find("Npc");
  115. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  116. var resPath = ResPathUtil.GetNpcPicFPath(value);
  117. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  118. DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
  119. spr.sprite = sp;
  120. }
  121. private static void Reset(GameObject sceneObj)
  122. {
  123. //背景层次
  124. Transform bg = sceneObj.transform.Find("Bg");
  125. if (bg != null)
  126. {
  127. SpriteRenderer spr = bg.GetComponent<SpriteRenderer>();
  128. if (spr != null)
  129. {
  130. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING);
  131. spr.sortingOrder = typeCfg.defaultLayer;
  132. }
  133. }
  134. //角色清理
  135. Transform role = sceneObj.transform.Find("Role");
  136. if (role != null)
  137. {
  138. int childCount = role.childCount;
  139. for (int i = childCount - 1; i >= 0; --i)
  140. {
  141. Transform child = role.GetChild(i);
  142. if (child.gameObject.name != "Body")
  143. {
  144. GameObject.DestroyImmediate(child.gameObject);
  145. }
  146. }
  147. }
  148. }
  149. public static void UpdatePhotographBgOrNpcOrBorder(GameObject sceneObj, EnumPhotographType type, int itemId)
  150. {
  151. string objName = "";
  152. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
  153. var resPath = ""; ResPathUtil.GetDressUpPath(itemCfg.res, ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType));
  154. if (type == EnumPhotographType.BG)
  155. {
  156. objName = "Bg/BgRes";
  157. resPath = ResPathUtil.GetDressUpPath(itemCfg.res, ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType));
  158. }
  159. else if (type == EnumPhotographType.NPC)
  160. {
  161. objName = "Scene/Npc/NpcRes";
  162. resPath = ResPathUtil.GetNpcPicFPath(itemCfg.res);
  163. }
  164. else if (type == EnumPhotographType.BORDER)
  165. {
  166. objName = "Border/BorderRes";
  167. resPath = ResPathUtil.GetDressUpPath(itemCfg.res, ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType));
  168. }
  169. Transform tf = sceneObj.transform.Find(objName);
  170. tf.position = Vector3.zero;
  171. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  172. DressUpUtil.ChangeAssetReleaser(tf.gameObject, resPath);
  173. Sprite sp = GFGAsset.Load<Sprite>(resPath);
  174. spr.sprite = sp;
  175. spr.size = spr.sprite.bounds.size;//将节点设置为原图大小
  176. if (type == EnumPhotographType.BG)
  177. {
  178. ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING);
  179. spr.sortingOrder = typeCfg.defaultLayer;
  180. return;
  181. }
  182. if (type == EnumPhotographType.BORDER) return;//边框不加碰撞盒
  183. PhotographAddCollider(tf.gameObject);
  184. }
  185. //添加碰撞盒
  186. public static void PhotographAddCollider(GameObject gameObject)
  187. {
  188. PolygonCollider2D polygonCollider2D = gameObject.GetComponent<PolygonCollider2D>();
  189. if (polygonCollider2D != null)
  190. {
  191. GameObject.Destroy(polygonCollider2D);
  192. }
  193. polygonCollider2D = gameObject.AddComponent<PolygonCollider2D>();
  194. polygonCollider2D.isTrigger = true;
  195. }
  196. //场景添加单个道具
  197. public static void AddScene(GameObject sceneObj, int itemId, int index)
  198. {
  199. var parentGameObj = new GameObject(string.Format("{0}_{1}", itemId, index));
  200. parentGameObj.transform.SetParent(sceneObj.transform.Find("Scene"), false);
  201. // SceneController.UpdateRole(itemId, _sceneObject, false, null, false, gameObj);
  202. DressUpUtil.AddItem(itemId, sceneObj, false, false, parentGameObj);
  203. for (int i = 0; i < parentGameObj.transform.childCount; i++)
  204. {
  205. GameObject childGameObj = parentGameObj.transform.GetChild(i).gameObject;
  206. SceneController.PhotographAddCollider(childGameObj);
  207. // GetGameObjectSize(childGameObj, parentGameObj);
  208. }
  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. Vector2 childSize = childGameObj.GetComponent<SpriteRenderer>().bounds.size;
  218. if (size.x < childSize.x)
  219. {
  220. size.x = childSize.x;
  221. }
  222. if (size.y < childSize.y)
  223. {
  224. size.y = childSize.y;
  225. }
  226. }
  227. return size * 100;
  228. }
  229. //设置物体中心点
  230. public static void SetGameObjectCenter(GameObject parentObj)
  231. {
  232. Transform parent = parentObj.transform;
  233. Vector3 postion = parent.position;
  234. Quaternion rotation = parent.rotation;
  235. Vector3 scale = parent.localScale;
  236. parent.position = Vector3.zero;
  237. parent.rotation = Quaternion.Euler(Vector3.zero);
  238. parent.localScale = Vector3.one;
  239. Vector3 center = Vector3.zero;
  240. Renderer[] renders = parent.GetComponentsInChildren<Renderer>();
  241. foreach (Renderer child in renders)
  242. {
  243. center += child.bounds.center;
  244. }
  245. center /= parent.GetComponentsInChildren<Transform>().Length;
  246. Bounds bounds = new Bounds(center, Vector3.zero);
  247. foreach (Renderer child in renders)
  248. {
  249. bounds.Encapsulate(child.bounds);
  250. }
  251. parent.position = postion;
  252. parent.rotation = rotation;
  253. parent.localScale = scale;
  254. foreach (Transform t in parent)
  255. {
  256. t.position = t.position - bounds.center;
  257. }
  258. parent.transform.position = bounds.center + parent.position;
  259. }
  260. //输入变换前后的向量,返回旋转角度
  261. public static float GetRotationAngle(Vector3 lastRotation, Vector3 curRotation)
  262. {
  263. float angle = Vector3.Angle(lastRotation, curRotation); //求出两向量之间的夹角
  264. Vector3 normal = Vector3.Cross(lastRotation, curRotation);//叉乘求出法线向量
  265. angle *= Mathf.Sign(Vector3.Dot(normal, Vector3.forward)); //Mathf.Sign()求符号,Vector3.Dot()求方向,求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
  266. return angle;
  267. }
  268. }
  269. }