PhotographView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 Dictionary<int, List<int>> _equipSceneData = new Dictionary<int, List<int>>();//当前穿戴的场景数据
  19. private Vector3 distance = Vector3.zero;
  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_btnBg.onClick.Add(OnClickBtnBg);
  28. _ui.m_btnChoose.onClick.Add(OnClickBtnChoose);
  29. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  30. _ui.m_ComSelect.m_list.itemRenderer = RenderListItem;
  31. _ui.m_ComSelect.m_list.onClickItem.Add(OnListItemClick);
  32. _ui.m_ComSelect.m_c1.onChanged.Add(OnContorllerChanged);
  33. _scenePrefab = GFGAsset.Load<GameObject>(ResPathUtil.GetPrefabPath("ScenePhotograph"));
  34. }
  35. protected override void OnShown()
  36. {
  37. base.OnShown();
  38. Input.multiTouchEnabled = true;
  39. equipDataCache = EquipDataCache.cacher;
  40. if (_sceneObject == null)
  41. {
  42. _sceneObject = GameObject.Instantiate(_scenePrefab);
  43. EquipDataCache.cacher.setSceneObj(_sceneObject);
  44. }
  45. _ui.m_ComSelect.m_c1.selectedIndex = 0;
  46. OnClickBtnChoose();
  47. RefreshList(EnumPhotographType.BG);
  48. UpdateEquipData();
  49. UpdateBgOrNpcOrBorder(EnumPhotographType.BG, equipDataCache.bgId);
  50. UpdateBody();
  51. UpdateScene();
  52. // Timers.inst.Add(0.001f, 0, OnTimerListener);
  53. }
  54. /************************************************************UI界面*********************************************************/
  55. private void OnContorllerChanged(EventContext context)
  56. {
  57. int index = _ui.m_ComSelect.m_c1.selectedIndex;
  58. RefreshList((EnumPhotographType)index);
  59. }
  60. private void RefreshList(EnumPhotographType index)
  61. {
  62. _ui.m_ComSelect.m_list.numItems = 0;
  63. switch (index)
  64. {
  65. case EnumPhotographType.BG:
  66. _listData = PhotographDataManager.Instance.listBgData;
  67. break;
  68. case EnumPhotographType.NPC:
  69. _listData = PhotographDataManager.Instance.listNpcData;
  70. break;
  71. case EnumPhotographType.SCENE:
  72. _listData = PhotographDataManager.Instance.listSceneData;
  73. break;
  74. case EnumPhotographType.BORDER:
  75. _listData = PhotographDataManager.Instance.listBorderData;
  76. break;
  77. case EnumPhotographType.EFFECT:
  78. _listData = PhotographDataManager.Instance.listEffectData;
  79. break;
  80. }
  81. _ui.m_ComSelect.m_list.numItems = _listData.Count;
  82. }
  83. private void RenderListItem(int index, GObject obj)
  84. {
  85. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_listData[index]);
  86. string resPath = ResPathUtil.GetPhotographFPath(itemCfg.res, ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType));
  87. UI_ListItem item = UI_ListItem.Proxy(obj);
  88. item.target.data = _listData[index];
  89. item.m_loaIcon.url = resPath;
  90. }
  91. private void OnListItemClick(EventContext context)
  92. {
  93. int itemID = (int)((context.data as GObject).data);
  94. EnumPhotographType type = (EnumPhotographType)_ui.m_ComSelect.m_c1.selectedIndex;
  95. switch (type)
  96. {
  97. case EnumPhotographType.BG:
  98. case EnumPhotographType.BORDER:
  99. case EnumPhotographType.NPC:
  100. UpdateBgOrNpcOrBorder(type, itemID);
  101. break;
  102. case EnumPhotographType.SCENE:
  103. if (!_equipSceneData.ContainsKey(itemID))
  104. {
  105. _equipSceneData.Add(itemID, new List<int>());
  106. }
  107. _equipSceneData[itemID].Add(itemID);
  108. AddScene(itemID, _equipSceneData[itemID].Count - 1);
  109. break;
  110. case EnumPhotographType.EFFECT:
  111. break;
  112. }
  113. }
  114. /************************************************************场景*********************************************************/
  115. private void OnTimerListener(object param)
  116. {
  117. GameObject gameObj = null;
  118. // Debug.Log("点击鼠标:" + Input.GetMouseButtonDown(0));
  119. if (Input.GetMouseButtonDown(0))
  120. {
  121. if (Stage.isTouchOnUI) //点在了UI上
  122. {
  123. Debug.Log("点击UI");
  124. }
  125. else //没有点在UI上
  126. {
  127. Debug.Log("点击场景");
  128. }
  129. //从摄像机发出到点击坐标的射线
  130. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
  131. // Vector2 pos = Input.mousePosition;
  132. // Debug.Log("mousePosition:" + pos);
  133. if (hit.collider != null)
  134. {
  135. //划出射线,只有在scene视图中才能看到
  136. // Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
  137. gameObj = hit.collider.gameObject;
  138. Debug.Log("ClickName:" + gameObj.name);
  139. // Debug.Log("ClickName:" + gameObj.name + " Pos:" + gameObj.transform.position + " LocalPos:" + gameObj.transform.position);
  140. // Debug.Log("mousePosition:" + Input.mousePosition);
  141. // Debug.Log("boxPos:" + _ui.m_comChecked.target.position);
  142. // //当射线碰撞目标为boot类型的物品,执行拾取操作
  143. // if (gameObj.tag == "boot")
  144. // {
  145. // Debug.Log("pickup!");
  146. // }
  147. }
  148. }
  149. // Vector2 pos = Input.mousePosition;
  150. // Debug.Log("mousePosition:" + pos);
  151. if (gameObj == null) return;
  152. GameObject obj = GetMoveObjByHitObj(gameObj);
  153. if (Input.touchCount <= 0) { return; }
  154. Debug.Log("触摸屏幕的手指个数:" + Input.touchCount);
  155. // for (var touch : Touch in Input.touches)
  156. // {
  157. if (Input.GetTouch(0).phase == TouchPhase.Began)
  158. {
  159. Debug.Log("触摸:" + "Began");
  160. Vector3 objScreenPos = Camera.main.WorldToScreenPoint(obj.transform.position);
  161. distance = Input.mousePosition - objScreenPos;
  162. }
  163. if (Input.GetTouch(0).phase == TouchPhase.Moved)
  164. {
  165. Debug.Log("触摸:" + "Moved");
  166. // Vector3 distance =
  167. ControllerObject(obj);
  168. }
  169. if (Input.GetTouch(0).phase == TouchPhase.Stationary)
  170. {
  171. Debug.Log("触摸:" + "Stationary");
  172. }
  173. if (Input.GetTouch(0).phase == TouchPhase.Ended)
  174. {
  175. Debug.Log("触摸:" + "Ended");
  176. distance = Vector3.zero;
  177. }
  178. }
  179. private GameObject GetMoveObjByHitObj(GameObject obj)
  180. {
  181. // if (obj == null) return null;
  182. if (obj.transform.parent == _sceneObject.transform.Find("Bg") || obj.transform.parent == _sceneObject.transform.Find("Npc"))
  183. {
  184. return obj;//背景和npc拖动物体本身
  185. }
  186. else
  187. {
  188. return obj.transform.parent.gameObject;//场景要拖动父物体
  189. }
  190. }
  191. private void ControllerObject(GameObject obj)
  192. {
  193. if (obj.name == "BgRes")
  194. {
  195. Vector2 size = obj.GetComponent<SpriteRenderer>().size;
  196. Vector2 uiSize = _ui.target.size;
  197. float deviationWidth = (size.x - uiSize.x) / 2;
  198. float deviationHeigh = (size.y - uiSize.y) / 2;
  199. Vector2 pos = obj.transform.position;
  200. if (pos.x <= -deviationWidth)
  201. {
  202. obj.transform.position = new Vector2(-deviationWidth, obj.transform.position.y);
  203. return;
  204. }
  205. if (pos.x >= deviationWidth)
  206. {
  207. obj.transform.position = new Vector2(deviationWidth, obj.transform.position.y);
  208. return;
  209. }
  210. if (pos.y <= -deviationHeigh)
  211. {
  212. obj.transform.position = new Vector2(obj.transform.position.x, -deviationHeigh);
  213. return;
  214. }
  215. if (pos.x >= deviationHeigh)
  216. {
  217. obj.transform.position = new Vector2(obj.transform.position.x, deviationHeigh);
  218. return;
  219. }
  220. }
  221. // obj.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - distance.x, Input.mousePosition.y - distance.y));
  222. obj.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition - distance);
  223. }
  224. //背景
  225. private void UpdateBgOrNpcOrBorder(EnumPhotographType type, int itemId)
  226. {
  227. SceneController.UpdatePhotographBgOrNpcOrBorder(_sceneObject, type, itemId);
  228. }
  229. //主角
  230. private void UpdateBody()
  231. {
  232. // int[] equipDatas = equipDataCache.equipDatas;
  233. SceneController.UpdateRole(_equipRoleData.ToArray(), _sceneObject, false, null, false);
  234. if (equipDataCache.IsSuitPic && equipDataCache.suitId > 0)
  235. {
  236. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(equipDataCache.suitId);
  237. DressUpUtil.UpdateBody(suitCfg.picRes, _sceneObject);
  238. }
  239. GameObject gameObject = _sceneObject.transform.Find("Role/Body").gameObject;
  240. SceneController.PhotographAddCollider(gameObject);
  241. }
  242. //场景道具
  243. private void UpdateScene()
  244. {
  245. ICollection keys = _equipSceneData.Keys;
  246. foreach (int key in keys)
  247. {
  248. for (int i = 0; i < _equipSceneData[key].Count; i++)
  249. {
  250. AddScene(key, i);
  251. }
  252. }
  253. }
  254. //场景添加道具
  255. private void AddScene(int itemId, int index)
  256. {
  257. var gameObj = new GameObject(string.Format("{0}_{1}", itemId, index));
  258. gameObj.transform.SetParent(_sceneObject.transform.Find("Scene"), false);
  259. // SceneController.UpdateRole(itemId, _sceneObject, false, null, false, gameObj);
  260. DressUpUtil.AddItem(itemId, _sceneObject, false, false, gameObj);
  261. for (int i = 0; i < gameObj.transform.childCount; i++)
  262. {
  263. SceneController.PhotographAddCollider(gameObj.transform.GetChild(i).gameObject);
  264. }
  265. }
  266. //滤镜效果
  267. private void UpdateEffect()
  268. {
  269. }
  270. private void UpdateEquipData()
  271. {
  272. for (int i = 0; i < equipDataCache.equipDatas.Length; i++)
  273. {
  274. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(equipDataCache.equipDatas[i]);
  275. if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING)
  276. {
  277. if (!_equipSceneData.ContainsKey(itemCfg.id))
  278. {
  279. _equipSceneData.Add(itemCfg.id, new List<int>());
  280. }
  281. _equipSceneData[itemCfg.id].Add(itemCfg.id);
  282. }
  283. else
  284. {
  285. _equipRoleData.Add(equipDataCache.equipDatas[i]);
  286. }
  287. }
  288. }
  289. private void OnClickBtnBg()
  290. {
  291. _ui.m_ComSelect.target.visible = false;
  292. Timers.inst.Add(0.001f, 0, OnTimerListener);
  293. // _ui.target.opaque = false;//false表示可穿透
  294. }
  295. private void OnClickBtnChoose()
  296. {
  297. _ui.m_ComSelect.target.visible = true;
  298. Timers.inst.Remove(OnTimerListener);
  299. // _ui.target.opaque = true;
  300. }
  301. private void OnClickBtnBack()
  302. {
  303. this.Hide();
  304. // ViewManager.Show(ViewName.DRESS_UP_VIEW);
  305. EventAgent.DispatchEvent(ConstMessage.CLOSE_PHOTOGRAPHVIEW);
  306. }
  307. protected override void OnHide()
  308. {
  309. base.OnHide();
  310. if (_sceneObject != null)
  311. {
  312. GameObject.Destroy(_sceneObject);
  313. _sceneObject = null;
  314. }
  315. Timers.inst.Remove(OnTimerListener);
  316. equipDataCache = null;
  317. _equipRoleData.Clear();
  318. _equipSceneData.Clear();
  319. }
  320. public override void Dispose()
  321. {
  322. if (_scenePrefab != null)
  323. {
  324. GameObject.Destroy(_scenePrefab);
  325. _scenePrefab = null;
  326. }
  327. base.Dispose();
  328. }
  329. }
  330. }