PhotographView.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. using FairyGUI;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using UI.DressUp;
  7. using UnityEditor;
  8. using UnityEngine;
  9. using UnityEngine.EventSystems;
  10. namespace GFGGame
  11. {
  12. public class PhotographView : BaseView
  13. {
  14. private UI_PhotographUI _ui;
  15. private GameObject _scenePrefab;
  16. private GameObject _sceneObject;
  17. // private DressUpObjDataCache equipDataCache;
  18. // private const string MOVE = "MOVE";
  19. // private const string SCALE = "SCALE";
  20. // private const string ROTATION = "ROTATION";
  21. private const float MaxScale = 2;
  22. private const float MinScale = 0.1f;
  23. private const string BgResPath = "Bg/BgRes";
  24. private const string BorderResPath = "Border/BorderRes";
  25. private const string NpcResPath = "Scene/Npc/NpcRes";
  26. private const string RolePath = "Scene/Role";
  27. private const string RoleName = "Role";
  28. private const string NpcPath = "Scene/Npc";
  29. private const string BgResName = "BgRes";
  30. // private const string BorderResName = "BorderRes";
  31. // private GameObject bodyParent;
  32. // private GameObject bgParent;
  33. // private GameObject npcParent;
  34. // private GameObject borderParent;
  35. // private GameObject sceneParent;
  36. private List<int> _listData = null;//当前选择的资源数据
  37. public List<int> _equipRoleData = new List<int>();//当前穿戴的角色数据
  38. public Dictionary<int, List<int>> _equipSceneData = new Dictionary<int, List<int>>();//当前穿戴的场景数据
  39. private Dictionary<GameObject, float> _equipDistance = new Dictionary<GameObject, float>();
  40. private GameObject hitGameObj;//当前选中的物体(单指拖动,双指缩放旋转)
  41. private GameObject memoryHitGameObj;//当前选中的物体(单指缩放旋转)
  42. private Vector3 distance = Vector3.zero;//点击位置和点击物体原点的距离,用于
  43. private Vector2 lastPos = Vector2.zero;//上一次移动后从物体中心到鼠标位置的方向
  44. private float lastDistance = 0;//上一次移动后从物体中心到鼠标位置的方向
  45. // private float _startDistance;//从物体中心到缩放按钮的距离
  46. private SwipeGesture swipeGesture;
  47. private PinchGesture pinchGesture;
  48. private RotationGesture rotationGesture;
  49. private bool isTwoTouchPoint = false;
  50. private int maxLayer = int.MinValue;//最上层的层级数
  51. protected override void OnInit()
  52. {
  53. base.OnInit();
  54. packageName = UI_PhotographUI.PACKAGE_NAME;
  55. _ui = UI_PhotographUI.Create();
  56. viewCom = _ui.target;
  57. isfullScreen = true;
  58. _ui.m_btnBg.onClick.Add(OnClickBtnBg);
  59. _ui.m_loaGuide.onClick.Add(OnClickLoaGuide);
  60. _ui.m_loaGuide1.onClick.Add(OnClickLoaGuide1);
  61. _ui.m_btnChoose.onClick.Add(SetUIView);
  62. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  63. _ui.m_btnPhotograph.onClick.Add(OnClickBtnPhotograph);
  64. _ui.m_ComSelectRes.m_list.itemRenderer = RenderListItem;
  65. _ui.m_ComSelectRes.m_list.onClickItem.Add(OnListItemClick);
  66. _ui.m_ComSelectRes.m_comBtnTab.m_c1.onChanged.Add(OnContorllerChanged);
  67. _scenePrefab = GFGAsset.Load<GameObject>(ResPathUtil.GetPrefabPath("ScenePhotograph"));
  68. _ui.target.onTouchBegin.Add(OnTouchUIBegin);
  69. _ui.target.onTouchMove.Add(OnTouchUIMove);
  70. _ui.target.onTouchEnd.Add(OnTouchUIEnd);
  71. _ui.m_comSelectBox.m_btnSize.onTouchBegin.Add(OnTouchBtnSizeBegin);
  72. _ui.m_comSelectBox.m_btnSize.onTouchMove.Add(OnTouchBtnSizeMove);
  73. _ui.m_comSelectBox.m_btnSize.onTouchEnd.Add(OnTouchBtnSizeEnd);
  74. _ui.m_comSelectBox.m_btnFlip.onTouchBegin.Add(OnTouchBtnFlipBegin);
  75. _ui.m_comSelectBox.m_btnFlip.onTouchEnd.Add(OnTouchBtnFlipEnd);
  76. _ui.m_comSelectBox.m_btnDelete.onTouchBegin.Add(OnTouchBtnDeleteBegin);
  77. _ui.m_comSelectBox.m_btnDelete.onTouchEnd.Add(OnTouchBtnDeleteEnd);
  78. }
  79. protected override void OnShown()
  80. {
  81. base.OnShown();
  82. Input.multiTouchEnabled = true;
  83. // equipDataCache = EquipDataCache.cacher;
  84. PhotographDataManager.Instance.ClassifyEquipData();
  85. _equipRoleData = PhotographDataManager.Instance._equipRoleData;
  86. _equipSceneData = PhotographDataManager.Instance._equipSceneData;
  87. if (_sceneObject == null)
  88. {
  89. _sceneObject = GameObject.Instantiate(_scenePrefab);
  90. EquipDataCache.cacher.setSceneObj(_sceneObject);
  91. }
  92. pinchGesture = new PinchGesture(_ui.target);
  93. pinchGesture.onAction.Add(OnPinch);
  94. rotationGesture = new RotationGesture(_ui.target);
  95. rotationGesture.onAction.Add(OnRotate);
  96. _ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex = 0;
  97. RefreshList(EnumPhotographType.BG);
  98. SetUIView();
  99. UpdateBg(ItemCfgArray.Instance.GetCfg(EquipDataCache.cacher.bgId));
  100. UpdateBody();
  101. UpdateScene();
  102. GuideController.TryGuideDressUpViewMenuPart1(_ui.m_ComSelectRes.m_comBtnTab.m_btn2);
  103. // GuideController.TryGuideBtnPhotograph(_ui.m_btnPhotograph);
  104. if (GuideDataManager.GetGuideCount(ConstGuideId.PHOTOGRAPH_GUIDE) > 0 && GuideDataManager.GetGuideCountCopy(ConstGuideId.PHOTOGRAPH) > 0)
  105. {
  106. _ui.m_loaGuide.enabled = false;
  107. _ui.m_loaGuide1.enabled = false;
  108. }
  109. else
  110. {
  111. _ui.m_loaGuide.enabled = true;
  112. _ui.m_loaGuide1.enabled = false;
  113. }
  114. // CreatTex();
  115. Timers.inst.AddUpdate(UpdateToCheckGuide);
  116. }
  117. /************************************************************UI界面*********************************************************/
  118. private void OnContorllerChanged(EventContext context)
  119. {
  120. int index = _ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex;
  121. RefreshList((EnumPhotographType)index);
  122. GuideController.TryGuideBtnPhotograph(_ui.m_loaGuide);
  123. }
  124. private void RefreshList(EnumPhotographType index)
  125. {
  126. _ui.m_ComSelectRes.m_list.numItems = 0;
  127. _listData = PhotographDataManager.Instance.GetListData(index);
  128. _ui.m_ComSelectRes.m_list.numItems = _listData.Count;
  129. }
  130. private void RenderListItem(int index, GObject obj)
  131. {
  132. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_listData[index]);
  133. string resPath = ResPathUtil.GetIconPath(itemCfg);
  134. UI_ListItem item = UI_ListItem.Proxy(obj);
  135. item.target.data = _listData[index];
  136. item.m_loaIcon.url = resPath;
  137. item.m_txtName.text = itemCfg.name;
  138. }
  139. private void OnListItemClick(EventContext context)
  140. {
  141. int itemID = (int)((context.data as GObject).data);
  142. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
  143. EnumPhotographType type = (EnumPhotographType)_ui.m_ComSelectRes.m_comBtnTab.m_c1.selectedIndex;
  144. switch (type)
  145. {
  146. case EnumPhotographType.BG:
  147. UpdateBg(itemCfg);
  148. break;
  149. case EnumPhotographType.BORDER:
  150. if (_listData.IndexOf(itemID) == 0)
  151. {
  152. Transform tf = _sceneObject.transform.Find(BorderResPath);
  153. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  154. if (spr != null)
  155. {
  156. GameObject.Destroy(spr);
  157. }
  158. return;
  159. }
  160. UpdateBorder(itemCfg);
  161. break;
  162. case EnumPhotographType.NPC:
  163. UpdateNpc(itemCfg);
  164. break;
  165. case EnumPhotographType.SCENE:
  166. AddSceneItem(itemCfg, false);
  167. break;
  168. case EnumPhotographType.EFFECT:
  169. break;
  170. }
  171. }
  172. /************************************************************场景*********************************************************/
  173. private void OnTouchUIBegin(EventContext context)
  174. {
  175. context.CaptureTouch();
  176. if (_ui.m_ComSelectRes.target.visible == true) return;//添加道具不监听场景点击
  177. if (Stage.inst.touchCount > 1 && hitGameObj != null || Stage.inst.touchCount == 1 && context.inputEvent.touchId != 0) return;//两根手指&&两指不是同时按下||一根手指但属于中途换指
  178. if (PhotographDataManager.Instance.IsTouchUI(this.viewCom)) return;
  179. RaycastHit2D[] hit2Ds = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
  180. if (hit2Ds.Length > 0)
  181. {
  182. // maxLayer++;
  183. lastPos = Vector2.zero;
  184. hitGameObj = SceneController.GetFirstHitObj(hit2Ds);
  185. _ui.m_comSelectBox.m_btnDelete.visible = true;
  186. if (hitGameObj.name == "Body")//主角不可删除
  187. {
  188. _ui.m_comSelectBox.m_btnDelete.visible = false;
  189. }
  190. _ui.m_comSelectBox.target.visible = false;
  191. if (hitGameObj.name != BgResName)//背景不可改动层级
  192. {
  193. hitGameObj = hitGameObj.transform.parent.gameObject;
  194. PhotographDataManager.Instance.SetItemLayer(hitGameObj, maxLayer);
  195. maxLayer = PhotographDataManager.Instance.GetMaxLayer(hitGameObj, maxLayer) + 1;
  196. _ui.m_comSelectBox.target.visible = true;
  197. if (!_equipDistance.ContainsKey(hitGameObj))
  198. {
  199. SceneController.SetGameObjectCenter(hitGameObj);
  200. }
  201. }
  202. memoryHitGameObj = hitGameObj;
  203. distance = Input.mousePosition - Camera.main.WorldToScreenPoint(hitGameObj.transform.position);
  204. Vector3 localEulerAngles = hitGameObj.transform.localEulerAngles;
  205. float rotation = 0;
  206. if (localEulerAngles.y == 0)
  207. {
  208. rotation = -hitGameObj.transform.localEulerAngles.z;
  209. }
  210. else
  211. {
  212. if (hitGameObj.transform.localEulerAngles.z > 180)
  213. {
  214. rotation = hitGameObj.transform.localEulerAngles.z - 360;
  215. }
  216. else
  217. {
  218. rotation = hitGameObj.transform.localEulerAngles.z;
  219. }
  220. }
  221. _ui.m_comSelectBox.target.rotation = rotation;// localEulerAngles.y != 0 ? hitGameObj.transform.localEulerAngles.z : -hitGameObj.transform.localEulerAngles.z;
  222. Debug.Log("rotation:" + _ui.m_comSelectBox.target.rotation + " " + _ui.m_comSelectBox.target.rotationX + " " + _ui.m_comSelectBox.target.rotationY);
  223. _ui.m_comSelectBox.target.size = SceneController.GetGameObjectSize(hitGameObj);
  224. ControllerSelectBoxPos();
  225. }
  226. }
  227. private void OnTouchUIMove(EventContext context)
  228. {
  229. if (hitGameObj == null) return;//未选中任何物体
  230. if (Stage.inst.touchCount > 1) return; //只监听1根手指
  231. if (PhotographDataManager.Instance.IsTouchUI(this.viewCom)) return;
  232. Debug.Log("拖动");
  233. ControllerObjectPos();
  234. ControllerSelectBoxPos();
  235. }
  236. private void OnTouchUIEnd(EventContext context)
  237. {
  238. // if (Stage.inst.touchCount > 1 || context.inputEvent.touchId != 0) return; //只监听1根手指
  239. hitGameObj = null;
  240. }
  241. //选中物体的位置
  242. private void ControllerObjectPos()
  243. {
  244. hitGameObj.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition - distance);
  245. if (hitGameObj.name == BgResName)
  246. {
  247. PhotographDataManager.Instance.SetBgPos(hitGameObj, _ui.target.size);
  248. }
  249. }
  250. //选中框的位置
  251. private void ControllerSelectBoxPos()
  252. {
  253. Vector2 objScreenPos = Camera.main.WorldToScreenPoint(hitGameObj.transform.position);
  254. Vector2 localPos = this.viewCom.GlobalToLocal(new Vector2(objScreenPos.x, (Screen.height - objScreenPos.y)));
  255. _ui.m_comSelectBox.target.position = localPos;
  256. }
  257. private void OnTouchBtnSizeBegin(EventContext context)
  258. {
  259. InputEvent inputEvent = (InputEvent)context.data;
  260. Vector2 pt0 = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  261. Vector2 screenPos = this.viewCom.GlobalToLocal(_ui.m_comSelectBox.m_btnSize.LocalToGlobal(Vector2.zero));
  262. Vector2 pt1 = new Vector2(screenPos.x, screenPos.y);
  263. Vector2 pt2 = new Vector2(_ui.m_comSelectBox.target.x, _ui.m_comSelectBox.target.y);
  264. lastDistance = Vector2.Distance(pt0, pt2);
  265. if (!_equipDistance.ContainsKey(memoryHitGameObj))
  266. {
  267. float distance = Vector2.Distance(pt1, pt2) / memoryHitGameObj.transform.localScale.x;
  268. Debug.Log("distance:" + distance + " pt1:" + pt1 + " pt2:" + pt2);
  269. _equipDistance.Add(memoryHitGameObj, distance);
  270. }
  271. }
  272. private void OnTouchBtnSizeMove(EventContext context)
  273. {
  274. if (memoryHitGameObj == null) return;
  275. InputEvent inputEvent = (InputEvent)context.data;
  276. Vector2 pt1 = this.viewCom.GlobalToLocal(new Vector2(inputEvent.x, inputEvent.y));
  277. Vector2 pt2 = new Vector2(_ui.m_comSelectBox.target.x, _ui.m_comSelectBox.target.y);
  278. Vector2 curPos = pt1 - pt2;
  279. float angle = Vector3.Angle(lastPos, curPos); //求出两向量之间的夹角
  280. Vector3 normal = Vector3.Cross(lastPos, curPos);//叉乘求出法线向量
  281. angle *= Mathf.Sign(Vector3.Dot(normal, Vector3.forward)); //Mathf.Sign()求符号,Vector3.Dot()求方向,求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向
  282. lastPos = curPos;
  283. ControllerRotate(angle, memoryHitGameObj);
  284. float dist = Vector2.Distance(pt1, pt2);
  285. float ss = dist / lastDistance;
  286. Debug.Log("dist:" + dist + " lastDistance:" + lastDistance + " ss:" + ss);
  287. float newValue = Mathf.Clamp(ss * memoryHitGameObj.transform.localScale.x, 0.1f, 2);
  288. ControllerScale(newValue, memoryHitGameObj);
  289. if (newValue <= 0.1f) return;
  290. Debug.Log("ss:" + ss + " newValue:" + newValue);
  291. lastDistance = dist;
  292. Debug.Log("newValue:" + newValue);
  293. }
  294. private void OnTouchBtnSizeEnd(EventContext context)
  295. {
  296. lastPos = Vector2.zero;
  297. }
  298. //双指缩放
  299. private void OnPinch(EventContext context)
  300. {
  301. if (hitGameObj == null) return;
  302. if (hitGameObj.name == BgResName) return;//背景不可以缩放、旋转
  303. GTween.Kill(hitGameObj);
  304. PinchGesture gesture = (PinchGesture)context.sender;
  305. float newValue = Mathf.Clamp(hitGameObj.transform.localScale.x + gesture.delta, 0.3f, 2);
  306. Debug.Log("双指缩放:" + newValue);
  307. ControllerScale(newValue, hitGameObj);
  308. }
  309. //双指旋转
  310. private void OnRotate(EventContext context)
  311. {
  312. Debug.Log("双指旋转hitGameObj:" + hitGameObj);
  313. if (hitGameObj == null) return;
  314. Debug.Log("双指旋转name:" + hitGameObj.name);
  315. if (hitGameObj.name == BgResName) return;//背景不可以缩放、旋转
  316. // isTwoTouchPoint = true;
  317. GTween.Kill(hitGameObj.transform);
  318. RotationGesture gesture = (RotationGesture)context.sender;
  319. Debug.Log("双指旋转:" + gesture.delta);
  320. ControllerRotate(gesture.delta, hitGameObj);
  321. }
  322. private void ControllerScale(float value, GameObject gameObject)
  323. {
  324. if (value > MaxScale || value < MinScale) return;
  325. gameObject.transform.localScale = new Vector3(value, value, 1);
  326. Vector2 size = SceneController.GetGameObjectSize(gameObject);
  327. _ui.m_comSelectBox.target.size = size;
  328. // _ui.m_comSelectBox.target.SetSize(size.x, size.y);
  329. }
  330. private void ControllerRotate(float value, GameObject gameObject)
  331. {
  332. gameObject.transform.Rotate(Vector3.forward, -value, Space.World);
  333. _ui.m_comSelectBox.target.rotation += value;
  334. }
  335. private void UpdateBg(ItemCfg itemCfg)
  336. {
  337. Transform tf = _sceneObject.transform.Find(BgResPath);
  338. string resPath = ResPathUtil.GetDressUpPath(itemCfg.res, ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType));
  339. SceneController.SetSpriteRendererToTransform(tf, resPath);
  340. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  341. spr.sortingOrder = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING).defaultLayer;
  342. SceneController.SetBoxCollider2DToGameObject(tf.gameObject);
  343. }
  344. //主角
  345. private void UpdateBody()
  346. {
  347. GameObject bodyParent = _sceneObject.transform.Find(RolePath).gameObject;
  348. SceneController.UpdatePhotographBody(_equipRoleData.ToArray(), _sceneObject, bodyParent);
  349. maxLayer = PhotographDataManager.Instance.GetMaxLayer(bodyParent, maxLayer) + 1;
  350. }
  351. //添加初始场景道具
  352. private void UpdateScene()
  353. {
  354. ICollection keys = _equipSceneData.Keys;
  355. foreach (int key in keys)
  356. {
  357. for (int i = 0; i < _equipSceneData[key].Count; i++)
  358. {
  359. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(key);
  360. int defaultLayer = ItemTypeCfgArray.Instance.GetCfg(itemCfg.subType).defaultLayer;
  361. AddSceneItem(itemCfg, true);
  362. }
  363. }
  364. }
  365. private void UpdateNpc(ItemCfg itemCfg)
  366. {
  367. maxLayer++;
  368. Transform tf = _sceneObject.transform.Find(NpcResPath);
  369. string resPath = ResPathUtil.GetNpcPicFPath(itemCfg.res);
  370. SceneController.SetSpriteRendererToTransform(tf, resPath);
  371. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  372. spr.sortingOrder = maxLayer;
  373. tf.localPosition = new Vector2(-spr.bounds.center.x, -spr.bounds.center.y);
  374. SceneController.SetBoxCollider2DToGameObject(tf.gameObject);
  375. }
  376. private void UpdateBorder(ItemCfg itemCfg)
  377. {
  378. Transform tf = _sceneObject.transform.Find(BorderResPath);
  379. string resPath = ResPathUtil.GetPhotographFPath(itemCfg.res, ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType));
  380. SceneController.SetSpriteRendererToTransform(tf, resPath);
  381. SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
  382. spr.sortingOrder = 10000;//边框在所有道具的上边
  383. }
  384. private void UpdateEffect()
  385. {
  386. }
  387. private void AddSceneItem(ItemCfg itemCfg, bool isDefaultLayer)
  388. {
  389. int index = 0;
  390. GameObject parentGameObj;
  391. if (itemCfg.resLayer2 > 0)
  392. {
  393. if (!isDefaultLayer)
  394. {
  395. PhotographDataManager.Instance.AddEquipItem(_equipSceneData, itemCfg.id, out _equipSceneData);
  396. }
  397. index = _equipSceneData[itemCfg.id].Count - 1;
  398. parentGameObj = new GameObject(string.Format("{0}_{1}_{2}", itemCfg.id, index, 1));
  399. SceneController.AddItemToScene(_sceneObject, parentGameObj, itemCfg.id, 2, itemCfg.resLayer2);
  400. maxLayer = PhotographDataManager.Instance.GetMaxLayer(parentGameObj, maxLayer) + 1;
  401. if (!isDefaultLayer)
  402. {
  403. PhotographDataManager.Instance.SetItemLayer(parentGameObj, maxLayer);
  404. }
  405. }
  406. if (itemCfg.resLayer1 > 0)
  407. {
  408. if (!isDefaultLayer)
  409. {
  410. PhotographDataManager.Instance.AddEquipItem(_equipSceneData, itemCfg.id, out _equipSceneData);
  411. }
  412. index = _equipSceneData[itemCfg.id].Count - 1;
  413. parentGameObj = new GameObject(string.Format("{0}_{1}_{2}", itemCfg.id, index, 0));
  414. SceneController.AddItemToScene(_sceneObject, parentGameObj, itemCfg.id, 1, itemCfg.resLayer1);
  415. maxLayer = PhotographDataManager.Instance.GetMaxLayer(parentGameObj, maxLayer) + 1;
  416. if (!isDefaultLayer)
  417. {
  418. PhotographDataManager.Instance.SetItemLayer(parentGameObj, maxLayer);
  419. }
  420. }
  421. }
  422. private void OnClickBtnBg()
  423. {
  424. _ui.m_ComSelectRes.target.visible = false;
  425. // GuideController.TryGuideOpenPhotographView(null);
  426. }
  427. private void OnClickLoaGuide()
  428. {
  429. _ui.m_loaGuide1.enabled = true;
  430. _ui.m_loaGuide.enabled = false;
  431. _ui.m_ComSelectRes.target.visible = false;
  432. GuideController.TryGuideOpenPhotographView(_ui.m_loaGuide1);
  433. }
  434. private void OnClickLoaGuide1()
  435. {
  436. _ui.m_loaGuide1.enabled = false;
  437. _ui.m_ComSelectRes.target.visible = false;
  438. GuideController.TryGuideBtnSave(_ui.m_btnPhotograph);
  439. }
  440. private void OnTouchBtnFlipBegin()//翻转
  441. {
  442. Transform transform = memoryHitGameObj.transform;
  443. if (memoryHitGameObj.name == RoleName)
  444. {
  445. transform.Rotate(Vector3.up, 180, Space.Self);
  446. return;
  447. }
  448. for (int i = 0; i < transform.childCount; i++)
  449. {
  450. transform.GetChild(i).Rotate(Vector3.up, 180, Space.Self);
  451. }
  452. }
  453. private void OnTouchBtnFlipEnd()
  454. {
  455. }
  456. private void OnTouchBtnDeleteBegin()//删除
  457. {
  458. if (memoryHitGameObj.transform.name == RolePath)
  459. {
  460. return;
  461. }
  462. else if (memoryHitGameObj.transform.name == NpcPath)
  463. {
  464. SpriteRenderer spriteRenderer = memoryHitGameObj.transform.GetChild(0).GetComponent<SpriteRenderer>();
  465. if (spriteRenderer != null)
  466. {
  467. GameObject.Destroy(spriteRenderer);
  468. }
  469. }
  470. else
  471. {
  472. GameObject.DestroyImmediate(memoryHitGameObj);
  473. }
  474. _ui.m_comSelectBox.target.visible = false;
  475. }
  476. private void OnTouchBtnDeleteEnd()
  477. {
  478. }
  479. private void SetUIView()
  480. {
  481. _ui.m_ComSelectRes.target.visible = true;
  482. _ui.m_comSelectBox.target.visible = false;
  483. hitGameObj = null;
  484. memoryHitGameObj = null;
  485. }
  486. private void OnClickBtnPhotograph()
  487. {
  488. GuideController.HideGuide();
  489. GuideController.TryCompleteGuide(ConstGuideId.PHOTOGRAPH_GUIDE);
  490. _ui.target.visible = false;
  491. Timers.inst.StartCoroutine(ScreenShotTex());// ();
  492. }
  493. private IEnumerator ScreenShotTex()
  494. {
  495. _ui.target.visible = false;
  496. yield return new WaitForEndOfFrame();
  497. Rect rect = new Rect(0, 0, UnityEngine.Screen.width, UnityEngine.Screen.height);
  498. Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
  499. tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点
  500. tex.Apply();//保存像素信息
  501. ViewManager.Show<PhotographSaveView>(tex);
  502. _ui.target.visible = true;
  503. }
  504. private void OnClickBtnBack()
  505. {
  506. this.Hide();
  507. // ViewManager.Show(ViewName.DRESS_UP_VIEW);
  508. EventAgent.DispatchEvent(ConstMessage.CLOSE_PHOTOGRAPHVIEW);
  509. }
  510. protected override void OnHide()
  511. {
  512. base.OnHide();
  513. if (_sceneObject != null)
  514. {
  515. GameObject.Destroy(_sceneObject);
  516. _sceneObject = null;
  517. }
  518. // equipDataCache = null;
  519. _equipRoleData.Clear();
  520. _equipSceneData.Clear();
  521. hitGameObj = null;
  522. memoryHitGameObj = null;
  523. pinchGesture.onAction.Remove(OnPinch);
  524. rotationGesture.onAction.Remove(OnRotate);
  525. pinchGesture = null;
  526. rotationGesture = null;
  527. Timers.inst.Remove(UpdateToCheckGuide);
  528. }
  529. public override void Dispose()
  530. {
  531. if (_scenePrefab != null)
  532. {
  533. GameObject.Destroy(_scenePrefab);
  534. _scenePrefab = null;
  535. }
  536. base.Dispose();
  537. }
  538. private void UpdateToCheckGuide(object param)
  539. {
  540. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  541. GuideController.TryGuide(_ui.m_ComSelectRes.m_comBtnTab.m_btn2, ConstGuideId.PHOTOGRAPH, 4, "可以自由添加已有道具");
  542. GuideController.TryGuide(_ui.m_loaGuide, ConstGuideId.PHOTOGRAPH, 5, "点击空白处查看整体效果");
  543. GuideController.TryGuide(_ui.m_loaGuide1, ConstGuideId.PHOTOGRAPH, 6, "双指可控制放大缩小,或点击边框上的按键控制");
  544. GuideController.TryGuide(_ui.m_btnPhotograph, ConstGuideId.PHOTOGRAPH, 7, "点击拍照,可以记录和分享美照啦");
  545. GuideController.TryCompleteGuide(ConstGuideId.PHOTOGRAPH, 7);
  546. }
  547. private void CreatTex()
  548. {
  549. Rect rect = new Rect(-(UnityEngine.Screen.width / 2), -(UnityEngine.Screen.height / 2), UnityEngine.Screen.width, UnityEngine.Screen.height);
  550. Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
  551. Transform transform = _sceneObject.transform.Find("Scene/Role");
  552. Dictionary<SpriteRenderer, int> data = new Dictionary<SpriteRenderer, int>();
  553. float width = 0;
  554. float height = 0;
  555. for (int i = 0; i < transform.childCount; i++)
  556. {
  557. SpriteRenderer sr = transform.GetChild(i).GetComponent<SpriteRenderer>();
  558. if (sr != null)
  559. {
  560. width = Math.Max(sr.bounds.size.x + sr.transform.position.x, width);
  561. height = Math.Max(sr.bounds.size.x + sr.transform.position.x, width);
  562. data.Add(sr, sr.sortingOrder);
  563. }
  564. }
  565. List<KeyValuePair<SpriteRenderer, int>> lst = new List<KeyValuePair<SpriteRenderer, int>>(data);
  566. lst.Sort(delegate (KeyValuePair<SpriteRenderer, int> s1, KeyValuePair<SpriteRenderer, int> s2) { return s2.Value.CompareTo(s1.Value); });
  567. data.Clear();
  568. for (int i = lst.Count - 1; i >= 0; i--)
  569. {
  570. SpriteRenderer sr = lst[i].Key;
  571. if (sr != null)
  572. {
  573. // Sprite sprite1 = sr.GetComponent<Sprite>();
  574. Texture2D texture2D = sr.sprite.texture;
  575. for (int j = 0; j < texture2D.width; j++)
  576. {
  577. for (int k = 0; k < texture2D.height; k++)
  578. {
  579. var color = texture2D.GetPixel(j, k);
  580. tex.SetPixel(j, k, color);
  581. tex.Apply();
  582. Debug.Log("color:" + color + " " + j + " " + k);
  583. }
  584. }
  585. // var color = texture2D.GetPixel(0, 0);
  586. // tex.SetPixel(0, 0, color);
  587. // tex.Apply();//保存像素信息
  588. }
  589. }
  590. Sprite sprite = Sprite.Create(tex, rect, new Vector2(-(UnityEngine.Screen.width / 2), -(UnityEngine.Screen.height / 2)));
  591. GameObject gameObject = _sceneObject.transform.Find("Scene/Role/BodyRes").gameObject;
  592. gameObject.AddComponent<SpriteRenderer>();
  593. gameObject.GetComponent<SpriteRenderer>().sprite = sprite;
  594. // // Dictionary<string, int> data = new Dictionary<string, int>();
  595. // // for (int i = 0; i < _equipRoleData.Count; i++)
  596. // // {
  597. // // ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_equipRoleData[i]);
  598. // // ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(itemCfg.subType);
  599. // // if (itemCfg.resLayer1 > 0)
  600. // // {
  601. // // data.Add(itemCfg.res, typeCfg.defaultLayer);
  602. // // }
  603. // // if (itemCfg.resLayer2 > 0)
  604. // // {
  605. // // data.Add(string.Format("{0}_t", itemCfg.res), typeCfg.defaultLayer);
  606. // // }
  607. // // }
  608. // // List<KeyValuePair<string, int>> lst = new List<KeyValuePair<string, int>>(data);
  609. // // lst.Sort(delegate (KeyValuePair<string, int> s1, KeyValuePair<string, int> s2) { return s2.Value.CompareTo(s1.Value); });
  610. // // data.Clear();
  611. // // for (int i = lst.Count - 1; i >= 0; i--)
  612. // // {
  613. // // string res = lst[i].Key;
  614. // // int sortingOrder = lst[i].Value;
  615. // // float tx, ty;
  616. // // DressUpUtil.LoadSpritePos(res, out tx, out ty);
  617. // // string resPath = ResPathUtil.GetDressUpPath(res);
  618. // // Bitmap bmp = new Bitmap(resPath);
  619. // // tex.SetPixel(0, 0, );//读取像素,屏幕左下角为0点
  620. // // tex.Apply();//保存像素信息
  621. // // }
  622. // tex.SetPixel(0, 0, );//读取像素,屏幕左下角为0点
  623. // tex.Apply();//保存像素信息
  624. // return tex;
  625. }
  626. }
  627. }