using FairyGUI; using System; using System.Collections; using System.Collections.Generic; using UI.DressUp; using UnityEngine; using UnityEngine.EventSystems; namespace GFGGame { public class PhotographView : BaseView { private UI_PhotographUI _ui; private GameObject _scenePrefab; private GameObject _sceneObject; private DressUpObjDataCache equipDataCache; private List _listData = null;//当前选择的资源数据 private List _equipRoleData = new List();//当前穿戴的角色数据 private Dictionary> _equipSceneData = new Dictionary>();//当前穿戴的场景数据 private Vector3 distance = Vector3.zero; protected override void OnInit() { base.OnInit(); packageName = UI_PhotographUI.PACKAGE_NAME; _ui = UI_PhotographUI.Create(); viewCom = _ui.target; isfullScreen = true; _ui.m_btnBg.onClick.Add(OnClickBtnBg); _ui.m_btnChoose.onClick.Add(OnClickBtnChoose); _ui.m_btnBack.onClick.Add(OnClickBtnBack); _ui.m_ComSelect.m_list.itemRenderer = RenderListItem; _ui.m_ComSelect.m_list.onClickItem.Add(OnListItemClick); _ui.m_ComSelect.m_c1.onChanged.Add(OnContorllerChanged); _scenePrefab = GFGAsset.Load(ResPathUtil.GetPrefabPath("ScenePhotograph")); } protected override void OnShown() { base.OnShown(); Input.multiTouchEnabled = true; equipDataCache = EquipDataCache.cacher; if (_sceneObject == null) { _sceneObject = GameObject.Instantiate(_scenePrefab); EquipDataCache.cacher.setSceneObj(_sceneObject); } _ui.m_ComSelect.m_c1.selectedIndex = 0; OnClickBtnChoose(); RefreshList(EnumPhotographType.BG); UpdateEquipData(); UpdateBgOrNpcOrBorder(EnumPhotographType.BG, equipDataCache.bgId); UpdateBody(); UpdateScene(); // Timers.inst.Add(0.001f, 0, OnTimerListener); } /************************************************************UI界面*********************************************************/ private void OnContorllerChanged(EventContext context) { int index = _ui.m_ComSelect.m_c1.selectedIndex; RefreshList((EnumPhotographType)index); } private void RefreshList(EnumPhotographType index) { _ui.m_ComSelect.m_list.numItems = 0; switch (index) { case EnumPhotographType.BG: _listData = PhotographDataManager.Instance.listBgData; break; case EnumPhotographType.NPC: _listData = PhotographDataManager.Instance.listNpcData; break; case EnumPhotographType.SCENE: _listData = PhotographDataManager.Instance.listSceneData; break; case EnumPhotographType.BORDER: _listData = PhotographDataManager.Instance.listBorderData; break; case EnumPhotographType.EFFECT: _listData = PhotographDataManager.Instance.listEffectData; break; } _ui.m_ComSelect.m_list.numItems = _listData.Count; } private void RenderListItem(int index, GObject obj) { ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_listData[index]); string resPath = ResPathUtil.GetPhotographFPath(itemCfg.res, ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType)); UI_ListItem item = UI_ListItem.Proxy(obj); item.target.data = _listData[index]; item.m_loaIcon.url = resPath; } private void OnListItemClick(EventContext context) { int itemID = (int)((context.data as GObject).data); EnumPhotographType type = (EnumPhotographType)_ui.m_ComSelect.m_c1.selectedIndex; switch (type) { case EnumPhotographType.BG: case EnumPhotographType.BORDER: case EnumPhotographType.NPC: UpdateBgOrNpcOrBorder(type, itemID); break; case EnumPhotographType.SCENE: if (!_equipSceneData.ContainsKey(itemID)) { _equipSceneData.Add(itemID, new List()); } _equipSceneData[itemID].Add(itemID); AddScene(itemID, _equipSceneData[itemID].Count - 1); break; case EnumPhotographType.EFFECT: break; } } /************************************************************场景*********************************************************/ private void OnTimerListener(object param) { GameObject gameObj = null; // Debug.Log("点击鼠标:" + Input.GetMouseButtonDown(0)); if (Input.GetMouseButtonDown(0)) { if (Stage.isTouchOnUI) //点在了UI上 { Debug.Log("点击UI"); } else //没有点在UI上 { Debug.Log("点击场景"); } //从摄像机发出到点击坐标的射线 RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); // Vector2 pos = Input.mousePosition; // Debug.Log("mousePosition:" + pos); if (hit.collider != null) { //划出射线,只有在scene视图中才能看到 // Debug.DrawLine(ray.origin, hitInfo.point, Color.red); gameObj = hit.collider.gameObject; Debug.Log("ClickName:" + gameObj.name); // Debug.Log("ClickName:" + gameObj.name + " Pos:" + gameObj.transform.position + " LocalPos:" + gameObj.transform.position); // Debug.Log("mousePosition:" + Input.mousePosition); // Debug.Log("boxPos:" + _ui.m_comChecked.target.position); // //当射线碰撞目标为boot类型的物品,执行拾取操作 // if (gameObj.tag == "boot") // { // Debug.Log("pickup!"); // } } } // Vector2 pos = Input.mousePosition; // Debug.Log("mousePosition:" + pos); if (gameObj == null) return; GameObject obj = GetMoveObjByHitObj(gameObj); if (Input.touchCount <= 0) { return; } Debug.Log("触摸屏幕的手指个数:" + Input.touchCount); // for (var touch : Touch in Input.touches) // { if (Input.GetTouch(0).phase == TouchPhase.Began) { Debug.Log("触摸:" + "Began"); Vector3 objScreenPos = Camera.main.WorldToScreenPoint(obj.transform.position); distance = Input.mousePosition - objScreenPos; } if (Input.GetTouch(0).phase == TouchPhase.Moved) { Debug.Log("触摸:" + "Moved"); // Vector3 distance = ControllerObject(obj); } if (Input.GetTouch(0).phase == TouchPhase.Stationary) { Debug.Log("触摸:" + "Stationary"); } if (Input.GetTouch(0).phase == TouchPhase.Ended) { Debug.Log("触摸:" + "Ended"); distance = Vector3.zero; } } private GameObject GetMoveObjByHitObj(GameObject obj) { // if (obj == null) return null; if (obj.transform.parent == _sceneObject.transform.Find("Bg") || obj.transform.parent == _sceneObject.transform.Find("Npc")) { return obj;//背景和npc拖动物体本身 } else { return obj.transform.parent.gameObject;//场景要拖动父物体 } } private void ControllerObject(GameObject obj) { if (obj.name == "BgRes") { Vector2 size = obj.GetComponent().size; Vector2 uiSize = _ui.target.size; float deviationWidth = (size.x - uiSize.x) / 2; float deviationHeigh = (size.y - uiSize.y) / 2; Vector2 pos = obj.transform.position; if (pos.x <= -deviationWidth) { obj.transform.position = new Vector2(-deviationWidth, obj.transform.position.y); return; } if (pos.x >= deviationWidth) { obj.transform.position = new Vector2(deviationWidth, obj.transform.position.y); return; } if (pos.y <= -deviationHeigh) { obj.transform.position = new Vector2(obj.transform.position.x, -deviationHeigh); return; } if (pos.x >= deviationHeigh) { obj.transform.position = new Vector2(obj.transform.position.x, deviationHeigh); return; } } // obj.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x - distance.x, Input.mousePosition.y - distance.y)); obj.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition - distance); } //背景 private void UpdateBgOrNpcOrBorder(EnumPhotographType type, int itemId) { SceneController.UpdatePhotographBgOrNpcOrBorder(_sceneObject, type, itemId); } //主角 private void UpdateBody() { // int[] equipDatas = equipDataCache.equipDatas; SceneController.UpdateRole(_equipRoleData.ToArray(), _sceneObject, false, null, false); if (equipDataCache.IsSuitPic && equipDataCache.suitId > 0) { SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(equipDataCache.suitId); DressUpUtil.UpdateBody(suitCfg.picRes, _sceneObject); } GameObject gameObject = _sceneObject.transform.Find("Role/Body").gameObject; SceneController.PhotographAddCollider(gameObject); } //场景道具 private void UpdateScene() { ICollection keys = _equipSceneData.Keys; foreach (int key in keys) { for (int i = 0; i < _equipSceneData[key].Count; i++) { AddScene(key, i); } } } //场景添加道具 private void AddScene(int itemId, int index) { var gameObj = new GameObject(string.Format("{0}_{1}", itemId, index)); gameObj.transform.SetParent(_sceneObject.transform.Find("Scene"), false); // SceneController.UpdateRole(itemId, _sceneObject, false, null, false, gameObj); DressUpUtil.AddItem(itemId, _sceneObject, false, false, gameObj); for (int i = 0; i < gameObj.transform.childCount; i++) { SceneController.PhotographAddCollider(gameObj.transform.GetChild(i).gameObject); } } //滤镜效果 private void UpdateEffect() { } private void UpdateEquipData() { for (int i = 0; i < equipDataCache.equipDatas.Length; i++) { ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(equipDataCache.equipDatas[i]); if (itemCfg.subType == ConstDressUpItemType.QIAN_JING || itemCfg.subType == ConstDressUpItemType.BEI_SHI || itemCfg.subType == ConstDressUpItemType.HUAN_JING) { if (!_equipSceneData.ContainsKey(itemCfg.id)) { _equipSceneData.Add(itemCfg.id, new List()); } _equipSceneData[itemCfg.id].Add(itemCfg.id); } else { _equipRoleData.Add(equipDataCache.equipDatas[i]); } } } private void OnClickBtnBg() { _ui.m_ComSelect.target.visible = false; Timers.inst.Add(0.001f, 0, OnTimerListener); // _ui.target.opaque = false;//false表示可穿透 } private void OnClickBtnChoose() { _ui.m_ComSelect.target.visible = true; Timers.inst.Remove(OnTimerListener); // _ui.target.opaque = true; } private void OnClickBtnBack() { this.Hide(); // ViewManager.Show(ViewName.DRESS_UP_VIEW); EventAgent.DispatchEvent(ConstMessage.CLOSE_PHOTOGRAPHVIEW); } protected override void OnHide() { base.OnHide(); if (_sceneObject != null) { GameObject.Destroy(_sceneObject); _sceneObject = null; } Timers.inst.Remove(OnTimerListener); equipDataCache = null; _equipRoleData.Clear(); _equipSceneData.Clear(); } public override void Dispose() { if (_scenePrefab != null) { GameObject.Destroy(_scenePrefab); _scenePrefab = null; } base.Dispose(); } } }