111 lines
3.9 KiB
C#
Raw Normal View History

2025-02-08 16:22:00 +08:00
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using XMLTool;
using System.Collections.Generic;
using TMPro;
2025-02-10 10:54:47 +08:00
using GCSeries.Core;
using UnityEngine.EventSystems;
2025-03-06 14:36:35 +08:00
using System;
2025-02-08 16:22:00 +08:00
namespace QFramework.Example
{
public class UI3DObjShowData : UIPanelData
{
public class ObjData
{
public Vector3 rotate = Vector3.zero;
public Vector3 position = Vector3.zero;
public Vector3 scale = Vector3.one;
public float rotateSpeed = 10;
public float moveSpeed = 0.1f;
public float distance = 0.2f;
2025-02-10 10:54:47 +08:00
public float distanceMin = 0.2f;
public float distanceMax = 20f;
2025-02-08 16:22:00 +08:00
public float pitchMin = -30f;
public float pitchMax = 80;
public string deviceName;
}
public List<ObjData> datas;
}
public partial class UI3DObjShow : UIPanel
{
ResLoader loader;
GameObject curObj;
2025-02-10 10:54:47 +08:00
RectTransform rawImg;
2025-02-08 16:22:00 +08:00
protected override void OnInit(IUIData uiData = null)
{
loader = ResLoader.Allocate();
2025-02-10 10:54:47 +08:00
rawImg = DeviceRawImage.GetComponent<RectTransform>();
2025-02-08 16:22:00 +08:00
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UI3DObjShowData ?? new UI3DObjShowData();
FreeCameraController.instance.SetLock(false, false);
2025-02-10 10:54:47 +08:00
Show3DCamera.instance.gameObject.SetActive(true);
2025-02-08 16:22:00 +08:00
Content.RemoveAllChildren();
foreach (var data in mData.datas)
{
var item = DeviceController.Instance.GetDevice(data.deviceName);
GameObject obj = GameObject.Instantiate(ItemPrefab.gameObject, Content);
obj.name = item.Name;
obj.transform.Find("Name").GetComponent<TextMeshProUGUI>().text = item.Name;
Image icon = obj.transform.Find("IconBg/Icon").GetComponent<Image>();
var localImageUrl = Global.deviceIconsPath + item.Icon;
loader.Add2Load(localImageUrl.ToNetImageResName(),
(bool success, IRes res) =>
{
if (success)
{
icon.sprite = Utility.GetSprite(res.Asset as Texture2D);
}
});
obj.GetComponent<Toggle>().onValueChanged.AddListener(isOn =>
{
if (isOn)
{
curObj = GameObject.Instantiate(DeviceController.Instance.GetDeviceObj(data.deviceName));
curObj.transform.position = data.position;
curObj.transform.localScale = data.scale;
curObj.transform.eulerAngles = data.rotate;
curObj.gameObject.SetActive(true);
2025-02-10 10:54:47 +08:00
Show3DCamera.instance.Set(curObj.transform, data.rotateSpeed, data.moveSpeed, data.distance, data.pitchMin, data.pitchMax, distanceMin: data.distanceMin, distanceMax: data.distanceMax, inputRect: rawImg);
2025-02-08 16:22:00 +08:00
}
else
{
if (curObj)
{
GameObject.Destroy(curObj);
curObj = null;
}
}
});
}
2025-03-06 14:36:35 +08:00
loader.LoadAsync(() =>
{
Right.gameObject.SetActive(mData.datas.Count > 1);
Content.GetChild(0).GetComponent<Toggle>().isOn = true;
});
2025-02-08 16:22:00 +08:00
}
2025-03-06 14:36:35 +08:00
2025-02-08 16:22:00 +08:00
protected override void OnShow()
{
}
protected override void OnHide()
{
FreeCameraController.instance.SetLock(true, true);
2025-02-10 10:54:47 +08:00
Show3DCamera.instance.gameObject.SetActive(false);
2025-02-08 16:22:00 +08:00
loader.Recycle2Cache();
}
protected override void OnClose()
{
}
2025-02-10 10:54:47 +08:00
2025-02-08 16:22:00 +08:00
}
}