PhotographView.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using FairyGUI;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UI.DressUp;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. namespace GFGGame
  9. {
  10. public class PhotographView : BaseView
  11. {
  12. private UI_PhotographUI _ui;
  13. private GameObject _scenePrefab;
  14. private GameObject _sceneObject;
  15. private DressUpObjDataCache equipDataCache;
  16. private List<int> _listData = null;//当前选择的资源数据
  17. private List<int> equipRoleData = new List<int>();//当前穿戴的角色数据
  18. // private List<int> equipSceneData = new List<int>();
  19. private Dictionary<int, List<int>> equipSceneData = new Dictionary<int, List<int>>();//当前穿戴的场景数据
  20. protected override void OnInit()
  21. {
  22. base.OnInit();
  23. packageName = UI_PhotographUI.PACKAGE_NAME;
  24. _ui = UI_PhotographUI.Create();
  25. viewCom = _ui.target;
  26. isfullScreen = true;
  27. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  28. _ui.m_ComSelect.m_list.itemRenderer = RenderListItem;
  29. _ui.m_ComSelect.m_list.onClickItem.Add(OnListItemClick);
  30. _ui.m_ComSelect.m_c1.onChanged.Add(OnContorllerChanged);
  31. _scenePrefab = GFGAsset.Load<GameObject>(ResPathUtil.GetPrefabPath("ScenePhotograph"));
  32. }
  33. protected override void OnShown()
  34. {
  35. base.OnShown();
  36. Input.multiTouchEnabled = true;
  37. if (_sceneObject == null)
  38. {
  39. _sceneObject = GameObject.Instantiate(_scenePrefab);
  40. EquipDataCache.cacher.setSceneObj(_sceneObject);
  41. }
  42. equipDataCache = EquipDataCache.cacher;
  43. _ui.m_ComSelect.m_c1.selectedIndex = 0;
  44. RefreshList(EnumPhotographType.BG);
  45. UpdateEquipData();
  46. UpdateBgOrNpcOrBorder(EnumPhotographType.BG, equipDataCache.bgId);
  47. UpdateBody();
  48. UpdateScene();
  49. Timers.inst.Add(0.001f, 0, OnTimerListener);
  50. }
  51. /************************************************************UI界面*********************************************************/
  52. private void OnContorllerChanged(EventContext context)
  53. {
  54. int index = _ui.m_ComSelect.m_c1.selectedIndex;
  55. RefreshList((EnumPhotographType)index);
  56. }
  57. private void RefreshList(EnumPhotographType index)
  58. {
  59. _ui.m_ComSelect.m_list.numItems = 0;
  60. switch (index)
  61. {
  62. case EnumPhotographType.BG:
  63. _listData = PhotographDataManager.Instance.listBgData;
  64. break;
  65. case EnumPhotographType.NPC:
  66. _listData = PhotographDataManager.Instance.listNpcData;
  67. break;
  68. case EnumPhotographType.SCENE:
  69. _listData = PhotographDataManager.Instance.listSceneData;
  70. break;
  71. case EnumPhotographType.BORDER:
  72. _listData = PhotographDataManager.Instance.listBorderData;
  73. break;
  74. case EnumPhotographType.EFFECT:
  75. _listData = PhotographDataManager.Instance.listEffectData;
  76. break;
  77. }
  78. _ui.m_ComSelect.m_list.numItems = _listData.Count;
  79. }
  80. private void RenderListItem(int index, GObject obj)
  81. {
  82. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_listData[index]);
  83. string resPath = ResPathUtil.GetPhotographFPath(itemCfg.res, ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType));
  84. UI_ListItem item = UI_ListItem.Proxy(obj);
  85. item.target.data = _listData[index];
  86. item.m_loaIcon.url = resPath;
  87. }
  88. private void OnListItemClick(EventContext context)
  89. {
  90. int itemID = (int)((context.data as GObject).data);
  91. EnumPhotographType type = (EnumPhotographType)_ui.m_ComSelect.m_c1.selectedIndex;
  92. switch (type)
  93. {
  94. case EnumPhotographType.BG:
  95. case EnumPhotographType.BORDER:
  96. case EnumPhotographType.NPC:
  97. UpdateBgOrNpcOrBorder(type, itemID);
  98. break;
  99. case EnumPhotographType.SCENE:
  100. if (!equipSceneData.ContainsKey(itemID))
  101. {
  102. equipSceneData.Add(itemID, new List<int>());
  103. }
  104. equipSceneData[itemID].Add(itemID);
  105. AddScene(itemID, equipSceneData[itemID].Count - 1);
  106. break;
  107. case EnumPhotographType.EFFECT:
  108. break;
  109. }
  110. }
  111. /************************************************************场景*********************************************************/
  112. private void OnTimerListener(object param)
  113. {
  114. // Debug.Log("点击鼠标:" + Input.GetMouseButtonDown(0));
  115. if (Input.GetMouseButtonDown(0))
  116. {
  117. if (Stage.isTouchOnUI) //点在了UI上
  118. {
  119. Debug.Log("点击UI");
  120. }
  121. else //没有点在UI上
  122. {
  123. Debug.Log("点击场景");
  124. }
  125. //从摄像机发出到点击坐标的射线
  126. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
  127. Vector2 pos = Input.mousePosition;
  128. if (hit.collider != null)
  129. {
  130. //划出射线,只有在scene视图中才能看到
  131. // Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
  132. GameObject gameObj = hit.collider.gameObject;
  133. Debug.Log("click object name is :" + gameObj.name);
  134. //当射线碰撞目标为boot类型的物品,执行拾取操作
  135. if (gameObj.tag == "boot")
  136. {
  137. Debug.Log("pickup!");
  138. }
  139. }
  140. }
  141. Debug.Log("触摸屏幕的手指个数:" + Input.touchCount);
  142. if (Input.touchCount <= 0) { return; }
  143. // for (var touch : Touch in Input.touches)
  144. // {
  145. if (Input.GetTouch(0).phase == TouchPhase.Began)
  146. {
  147. Debug.Log("触摸:" + "Began");
  148. }
  149. if (Input.GetTouch(0).phase == TouchPhase.Moved)
  150. {
  151. Debug.Log("触摸:" + "Moved");
  152. }
  153. if (Input.GetTouch(0).phase == TouchPhase.Stationary)
  154. {
  155. Debug.Log("触摸:" + "Stationary");
  156. }
  157. if (Input.GetTouch(0).phase == TouchPhase.Ended)
  158. {
  159. Debug.Log("触摸:" + "Ended");
  160. }
  161. }
  162. //背景
  163. private void UpdateBgOrNpcOrBorder(EnumPhotographType type, int itemId)
  164. {
  165. SceneController.UpdatePhotographBgOrNpcOrBorder(_sceneObject, type, itemId);
  166. }
  167. //主角
  168. private void UpdateBody()
  169. {
  170. // int[] equipDatas = equipDataCache.equipDatas;
  171. SceneController.UpdateRole(equipRoleData.ToArray(), _sceneObject, false, null, false);
  172. if (equipDataCache.IsSuitPic && equipDataCache.suitId > 0)
  173. {
  174. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(equipDataCache.suitId);
  175. DressUpUtil.UpdateBody(suitCfg.picRes, _sceneObject);
  176. }
  177. GameObject gameObject = _sceneObject.transform.Find("Role/Body").gameObject;
  178. SceneController.PhotographAddCollider(gameObject);
  179. }
  180. //场景道具
  181. private void UpdateScene()
  182. {
  183. ICollection keys = equipSceneData.Keys;
  184. foreach (int key in keys)
  185. {
  186. for (int i = 0; i < equipSceneData[key].Count; i++)
  187. {
  188. AddScene(key, i);
  189. }
  190. }
  191. }
  192. //场景添加道具
  193. private void AddScene(int itemId, int index)
  194. {
  195. var gameObj = new GameObject(string.Format("{0}_{1}", itemId, index));
  196. gameObj.transform.SetParent(_sceneObject.transform.Find("Scene"), false);
  197. // SceneController.UpdateRole(itemId, _sceneObject, false, null, false, gameObj);
  198. DressUpUtil.AddItem(itemId, _sceneObject, false, false, gameObj);
  199. for (int i = 0; i < gameObj.transform.childCount; i++)
  200. {
  201. SceneController.PhotographAddCollider(gameObj.transform.GetChild(i).gameObject);
  202. }
  203. }
  204. //滤镜效果
  205. private void UpdateEffect()
  206. {
  207. }
  208. private void UpdateEquipData()
  209. {
  210. for (int i = 0; i < equipDataCache.equipDatas.Length; i++)
  211. {
  212. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(equipDataCache.equipDatas[i]);
  213. if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING)
  214. {
  215. if (!equipSceneData.ContainsKey(itemCfg.id))
  216. {
  217. equipSceneData.Add(itemCfg.id, new List<int>());
  218. }
  219. equipSceneData[itemCfg.id].Add(itemCfg.id);
  220. }
  221. else
  222. {
  223. equipRoleData.Add(equipDataCache.equipDatas[i]);
  224. }
  225. }
  226. }
  227. private void OnClickBtnBack()
  228. {
  229. this.Hide();
  230. // ViewManager.Show(ViewName.DRESS_UP_VIEW);
  231. EventAgent.DispatchEvent(ConstMessage.CLOSE_PHOTOGRAPHVIEW);
  232. }
  233. protected override void OnHide()
  234. {
  235. base.OnHide();
  236. if (_sceneObject != null)
  237. {
  238. GameObject.Destroy(_sceneObject);
  239. _sceneObject = null;
  240. }
  241. equipDataCache = null;
  242. }
  243. public override void Dispose()
  244. {
  245. if (_scenePrefab != null)
  246. {
  247. GameObject.Destroy(_scenePrefab);
  248. _scenePrefab = null;
  249. }
  250. base.Dispose();
  251. }
  252. }
  253. }