VirtualFramework/Assets/Scripts/Item/Body3DOjbItem.cs

180 lines
5.2 KiB
C#
Raw Normal View History

2025-02-12 17:36:00 +08:00
using QFramework;
using QFramework.Example;
using System;
using System.Collections;
using System.Collections.Generic;
2025-03-26 14:11:26 +08:00
using Turing.Samples;
using UnityEditor;
2025-02-12 17:36:00 +08:00
using UnityEngine;
using XMLTool;
public class Body3DOjbItem : MonoBehaviour
{
2025-02-13 11:02:16 +08:00
public Body3D.Body body;
2025-02-12 17:36:00 +08:00
2025-02-19 17:23:39 +08:00
public ObjectToggle objToggle;
2025-03-26 14:11:26 +08:00
IObjDrag objDrag;
2025-02-14 13:20:54 +08:00
// <20><>¼<EFBFBD><C2BC>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD>ʱ<EFBFBD><CAB1>
private float lastClickTime;
// ˫<><CBAB><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
private const float doubleClickTimeThreshold = 0.3f;
Shader shader;
2025-03-26 14:11:26 +08:00
2025-02-13 11:02:16 +08:00
private void Awake()
2025-02-12 17:36:00 +08:00
{
shader = GetComponent<Renderer>()?.material.shader;
TypeEventSystem.Global.Register<OnChangeMat>(OnChangeMatEvent).UnRegisterWhenGameObjectDestroyed(this);
2025-03-26 14:11:26 +08:00
#if VR
#if Turing
gameObject.GetOrAddComponent<Draggable>();
#endif
#endif
}
private void OnChangeMatEvent(OnChangeMat t)
{
2025-03-26 14:11:26 +08:00
if (t.shader != null)
{
GetComponent<Renderer>().material.shader = t.shader;
}
else
{
GetComponent<Renderer>().material.shader = this.shader;
}
2025-02-12 17:36:00 +08:00
}
public void Init(Body3D.Body body)
{
this.body = body;
if (body.subBody == null || body.subBody.Count == 0)
{
if (body.toggle != null)
{
2025-02-13 11:02:16 +08:00
objToggle = gameObject.GetOrAddComponent<ObjectToggle>();
2025-02-12 17:36:00 +08:00
ObjectColorToggle colorToggle = null;
if (body.toggle.color != null)
{
colorToggle = gameObject.GetOrAddComponent<ObjectColorToggle>();
2025-02-14 17:09:41 +08:00
if (string.IsNullOrEmpty(body.toggle.color.isOn) == false)
{
colorToggle.isOnColor = Utility.ToColor(body.toggle.color.isOn);
}
if (string.IsNullOrEmpty(body.toggle.color.isOff) == false)
{
colorToggle.isOffColor = Utility.ToColor(body.toggle.color.isOff);
}
2025-02-12 17:36:00 +08:00
}
objToggle.OnValueChanged.AddListener(isOn =>
{
2025-02-13 17:39:33 +08:00
if (Body3DController.Instance.CheckStatus(Body3DController.Status.Active))
2025-02-12 17:36:00 +08:00
{
2025-02-13 17:39:33 +08:00
if (isOn == true)
{
gameObject.SetActive(false);
2025-02-14 08:58:46 +08:00
Body3DController.Instance.AddActiveObj(gameObject);
2025-02-13 17:39:33 +08:00
}
2025-02-13 11:02:16 +08:00
}
else
{
2025-03-17 14:51:03 +08:00
if (colorToggle != null)
{
if (isOn)
{
colorToggle.SetColor(ObjectColorToggle.State.On);
}
else
{
colorToggle.SetColor(ObjectColorToggle.State.Off);
}
}
2025-03-26 14:11:26 +08:00
#if VR
#if Turing
objDrag = gameObject.GetOrAddComponent<TuringDraggableEx>();
#endif
#else
2025-02-13 17:39:33 +08:00
objDrag = gameObject.GetOrAddComponent<ObjDrag>();
2025-03-26 14:11:26 +08:00
#endif
2025-02-14 13:20:54 +08:00
objDrag.OnDragEnd.AddListener(obj =>
{
Body3DController.Instance.AddMoveObj(gameObject);
});
2025-03-26 14:11:26 +08:00
2025-02-13 17:39:33 +08:00
RefreshDrag();
if (isOn)
{
TypeEventSystem.Global.Register<OnBody3DDragChanged>(OnBody3DDragHandler);
}
else
{
TypeEventSystem.Global.UnRegister<OnBody3DDragChanged>(OnBody3DDragHandler);
}
2025-02-12 17:36:00 +08:00
}
2025-02-17 15:14:40 +08:00
TypeEventSystem.Global.Send<OnBody3DSelected>(new OnBody3DSelected() { isOn = isOn, obj = gameObject });
2025-02-13 17:39:33 +08:00
2025-02-12 17:36:00 +08:00
});
}
}
}
2025-02-14 13:20:54 +08:00
private void OnMouseDown()
{
// <20><><EFBFBD>㵱ǰʱ<C7B0><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
float currentTime = Time.time;
if (currentTime - lastClickTime < doubleClickTimeThreshold)
{
// <20><><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD>¼<EFBFBD>
OnDoubleClick();
}
lastClickTime = currentTime;
}
public void OnDoubleClick()
{
if (Body3DController.Instance.CheckStatus(Body3DController.Status.Drag))
{
2025-03-26 14:11:26 +08:00
var drag = gameObject.GetComponent<IObjDrag>();
2025-02-14 13:20:54 +08:00
if (drag != null)
{
drag.OnDoubleClick();
}
}
else if (Body3DController.Instance.CheckStatus(Body3DController.Status.Active) == false)
{
float distance = 1;
if (float.TryParse(body.FocusDistance, out distance))
{
2025-02-14 17:09:41 +08:00
Show3DCamera.instance.FocusObj(gameObject.transform.position, distance, 0.5f);
2025-02-14 13:20:54 +08:00
}
else
{
2025-02-14 17:09:41 +08:00
Show3DCamera.instance.FocusObj(gameObject.transform.position, moveTime: 0.5f);
2025-02-14 13:20:54 +08:00
}
}
}
2025-02-12 17:36:00 +08:00
2025-02-13 11:02:16 +08:00
private void OnBody3DDragHandler(OnBody3DDragChanged drag)
{
RefreshDrag();
}
2025-02-12 17:36:00 +08:00
2025-02-13 11:02:16 +08:00
public void RefreshDrag()
{
if (objToggle != null && objDrag != null)
{
2025-02-13 17:39:33 +08:00
objDrag.isOn = objToggle.isOn && Body3DController.Instance.CheckStatus(Body3DController.Status.Drag);
2025-02-13 11:02:16 +08:00
}
}
2025-03-17 14:51:03 +08:00
2025-02-12 17:36:00 +08:00
}