VirtualFramework/Assets/Scripts/UI/UICameraSwitch.cs

190 lines
5.4 KiB
C#
Raw Normal View History

2024-12-16 15:45:19 +08:00
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using DG.Tweening;
using System.Collections.Generic;
2024-12-16 15:45:19 +08:00
namespace QFramework.Example
{
public class UICameraSwitchData : UIPanelData
{
public string nearDevice;
public string normalDevice;
2024-12-16 15:45:19 +08:00
public Vector3 nearPos;
public Vector3 nearRot;
public Vector3 normalPos;
public Vector3 normalRot;
2025-03-24 14:32:22 +08:00
public Vector3 vrPos;
public Vector3 vrRot;
public float nearTime;
public float normalTime;
2025-03-24 14:32:22 +08:00
public float vrTime;
2025-01-09 14:10:59 +08:00
public string isOn;
2024-12-16 15:45:19 +08:00
}
public partial class UICameraSwitch : UIPanel
{
2025-02-28 17:30:07 +08:00
bool firstFreeMove = true;
2024-12-16 15:45:19 +08:00
protected override void OnInit(IUIData uiData = null)
{
2025-01-09 09:43:15 +08:00
TypeEventSystem.Global.Register<OnModuleQuit>((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject);
2024-12-16 15:45:19 +08:00
mData = uiData as UICameraSwitchData ?? new UICameraSwitchData();
// please add init code here
Near.onValueChanged.AddListener(isOn =>
{
if (isOn)
{
SetNear();
2024-12-16 15:45:19 +08:00
}
2024-12-16 15:58:37 +08:00
Near.transform.Find("Bg/Line").gameObject.SetActive(isOn);
2024-12-16 15:45:19 +08:00
});
Far.onValueChanged.AddListener(isOn =>
{
2024-12-16 15:45:19 +08:00
if (isOn)
{
SetNormal();
2024-12-16 15:45:19 +08:00
}
2024-12-16 15:58:37 +08:00
Far.transform.Find("Bg/Line").gameObject.SetActive(isOn);
2024-12-16 15:45:19 +08:00
});
}
public void SetNear()
{
2025-03-19 16:31:17 +08:00
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()
{
2025-03-19 16:31:17 +08:00
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);
}
2024-12-16 15:45:19 +08:00
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UICameraSwitchData ?? new UICameraSwitchData();
2025-01-20 18:42:51 +08:00
Near.gameObject.SetActive(mData.nearPos != default);
Far.gameObject.SetActive(mData.normalPos != default);
2025-03-24 14:32:22 +08:00
//#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
2025-03-24 14:32:22 +08:00
2025-01-09 14:10:59 +08:00
if (string.IsNullOrEmpty(mData.isOn))
{
2025-01-21 10:08:13 +08:00
if (Near.isOn && Near.gameObject.activeSelf)
2025-01-09 14:10:59 +08:00
{
SetNear();
}
2025-01-21 10:08:13 +08:00
if (Far.isOn && Far.gameObject.activeSelf)
2025-01-09 14:10:59 +08:00
{
SetNormal();
}
2024-12-31 16:31:32 +08:00
}
2025-01-09 14:10:59 +08:00
else
2024-12-31 16:31:32 +08:00
{
2025-01-09 14:10:59 +08:00
switch (mData.isOn)
{
case "near":
2025-01-16 18:31:21 +08:00
if (Near.isOn == false)
{
Near.isOn = true;
}
else
{
SetNear();
}
2025-01-09 14:10:59 +08:00
break;
case "normal":
2025-01-16 18:31:21 +08:00
if (Far.isOn == false)
{
Far.isOn = true;
}
else
{
SetNormal();
}
2025-01-09 14:10:59 +08:00
break;
}
}
//#if VR
// Near.gameObject.SetActive(false);
// Far.gameObject.SetActive(false);
//#endif
2025-01-20 18:42:51 +08:00
2024-12-16 15:45:19 +08:00
}
private void Update()
{
2025-03-24 09:29:31 +08:00
#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;
2025-02-28 17:30:07 +08:00
if (firstFreeMove)
{
UITipWindowData data = new UITipWindowData();
data.txt = "<22><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>ӽǣ<D3BD><C7A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½ǰ<C2BD>ť<EFBFBD>ɻص<C9BB>Ԥ<EFBFBD><D4A4><EFBFBD>ӽǡ<D3BD>";
data.btns.Add(new UITipWindowData.ItemData() { txt = <><C8B7>" });
UIKit.OpenPanelAsync<UITipWindow>(canvasLevel: UILevel.PopUI, uiData: data).ToAction().StartGlobal();
firstFreeMove = false;
}
}
}
#endif
}
2024-12-16 15:45:19 +08:00
protected override void OnShow()
{
}
protected override void OnHide()
{
}
protected override void OnClose()
{
}
}
}