using UnityEngine; using UnityEngine.UI; using QFramework; using DG.Tweening; using System.Collections.Generic; namespace QFramework.Example { public class UICameraSwitchData : UIPanelData { public string nearDevice; public string normalDevice; public Vector3 nearPos; public Vector3 nearRot; public Vector3 normalPos; public Vector3 normalRot; public Vector3 vrPos; public Vector3 vrRot; public float nearTime; public float normalTime; public float vrTime; public string isOn; } public partial class UICameraSwitch : UIPanel { bool firstFreeMove = true; protected override void OnInit(IUIData uiData = null) { TypeEventSystem.Global.Register((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject); mData = uiData as UICameraSwitchData ?? new UICameraSwitchData(); // please add init code here Near.onValueChanged.AddListener(isOn => { if (isOn) { SetNear(); } Near.transform.Find("Bg/Line").gameObject.SetActive(isOn); }); Far.onValueChanged.AddListener(isOn => { if (isOn) { SetNormal(); } Far.transform.Find("Bg/Line").gameObject.SetActive(isOn); }); } public void SetNear() { Transform trans = null; #if VR trans = UIRoot.Instance.transform.Find("ZFrame"); #else trans = Camera.main.transform; #endif trans.DOMove(mData.nearPos, mData.nearTime); trans.DORotate(mData.nearRot, mData.nearTime); } public void SetNormal() { Transform trans = null; #if VR trans = UIRoot.Instance.transform.Find("ZFrame"); #else trans = Camera.main.transform; #endif trans.DOMove(mData.normalPos, mData.normalTime); trans.DORotate(mData.normalRot, mData.normalTime); } protected override void OnOpen(IUIData uiData = null) { mData = uiData as UICameraSwitchData ?? new UICameraSwitchData(); Near.gameObject.SetActive(mData.nearPos != default); Far.gameObject.SetActive(mData.normalPos != default); //#if VR // Transform trans = UIRoot.Instance.transform.Find("ZFrame"); // if (mData.vrPos != default) // { // mData.nearPos = mData.vrPos; // mData.nearRot = mData.vrRot; // mData.nearTime = mData.vrTime; // mData.isOn = "near"; // } // else // { // if (mData.nearPos != default) // { // mData.isOn = "near"; // } // else if (mData.normalPos != default) // { // mData.isOn = "normal"; // } // } //#endif if (string.IsNullOrEmpty(mData.isOn)) { if (Near.isOn && Near.gameObject.activeSelf) { SetNear(); } if (Far.isOn && Far.gameObject.activeSelf) { SetNormal(); } } else { switch (mData.isOn) { case "near": if (Near.isOn == false) { Near.isOn = true; } else { SetNear(); } break; case "normal": if (Far.isOn == false) { Far.isOn = true; } else { SetNormal(); } break; } } //#if VR // Near.gameObject.SetActive(false); // Far.gameObject.SetActive(false); //#endif } private void Update() { #if UNITY_STANDALONE_WIN && !VR if (Near.isOn == true || Far.isOn == true) { if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D)) { Near.isOn = false; Far.isOn = false; if (firstFreeMove) { UITipWindowData data = new UITipWindowData(); data.txt = "已切换至自由移动视角,点右下角按钮可回到预制视角。"; data.btns.Add(new UITipWindowData.ItemData() { txt = "确定" }); UIKit.OpenPanelAsync(canvasLevel: UILevel.PopUI, uiData: data).ToAction().StartGlobal(); firstFreeMove = false; } } } #endif } protected override void OnShow() { } protected override void OnHide() { } protected override void OnClose() { } } }