From 7f216dc69441a8270358eebc8a0edcbc7127ae1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Fri, 11 Apr 2025 19:57:34 +0800 Subject: [PATCH] modify --- Runtime/UI3DWidget/UI3DDisplay.Touch.cs | 110 ++++++------ Runtime/UI3DWidget/UI3DDisplay.cs | 217 ++++++++++++------------ 2 files changed, 163 insertions(+), 164 deletions(-) diff --git a/Runtime/UI3DWidget/UI3DDisplay.Touch.cs b/Runtime/UI3DWidget/UI3DDisplay.Touch.cs index a02f550..a0852b3 100644 --- a/Runtime/UI3DWidget/UI3DDisplay.Touch.cs +++ b/Runtime/UI3DWidget/UI3DDisplay.Touch.cs @@ -1,55 +1,55 @@ -// using UnityEngine; -// using UnityEngine.EventSystems; -// -// namespace AlicizaX.UI.Extension -// { -// public class UI3DDisplayTouch : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler -// { -// private GameObject m_dragObject; -// private bool m_CanDrag = true; -// private float m_DragSpeed = 20.0f; -// private float m_DragRotation; -// private Vector3 m_ShowRotation = Vector3.zero; -// private Transform view; -// -// public void SetObject(GameObject obj) -// { -// m_dragObject = obj; -// m_ShowRotation = obj.transform.eulerAngles; -// view = obj.GetComponent(); -// } -// -// public void Disopose() -// { -// m_dragObject = null; -// view = null; -// } -// -// //拖拽 -// public void OnDrag(PointerEventData eventData) -// { -// if (!m_CanDrag) return; -// if (!view) return; -// if (!m_dragObject || !(m_DragSpeed > 0.0f)) return; -// -// var delta = eventData.delta.x; -// var deltaRot = -m_DragSpeed * delta * Time.deltaTime; -// -// m_DragRotation += deltaRot; -// var showRotation = Quaternion.Euler(m_ShowRotation); -// var showUp = showRotation * Vector3.up; -// showRotation *= Quaternion.AngleAxis(m_DragRotation, showUp); -// view.rotation = showRotation; -// } -// -// //按下 -// public void OnPointerDown(PointerEventData eventData) -// { -// } -// -// //抬起 -// public void OnPointerUp(PointerEventData eventData) -// { -// } -// } -// } +using UnityEngine; +using UnityEngine.EventSystems; + +namespace AlicizaX.UI.Extension +{ + public class UI3DDisplayTouch : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler + { + private GameObject m_dragObject; + private bool m_CanDrag = true; + private float m_DragSpeed = 20.0f; + private float m_DragRotation; + private Vector3 m_ShowRotation = Vector3.zero; + private Transform view; + + public void SetObject(GameObject obj) + { + m_dragObject = obj; + m_ShowRotation = obj.transform.eulerAngles; + view = obj.GetComponent(); + } + + public void Disopose() + { + m_dragObject = null; + view = null; + } + + //拖拽 + public void OnDrag(PointerEventData eventData) + { + if (!m_CanDrag) return; + if (!view) return; + if (!m_dragObject || !(m_DragSpeed > 0.0f)) return; + + var delta = eventData.delta.x; + var deltaRot = -m_DragSpeed * delta * Time.deltaTime; + + m_DragRotation += deltaRot; + var showRotation = Quaternion.Euler(m_ShowRotation); + var showUp = showRotation * Vector3.up; + showRotation *= Quaternion.AngleAxis(m_DragRotation, showUp); + view.rotation = showRotation; + } + + //按下 + public void OnPointerDown(PointerEventData eventData) + { + } + + //抬起 + public void OnPointerUp(PointerEventData eventData) + { + } + } +} diff --git a/Runtime/UI3DWidget/UI3DDisplay.cs b/Runtime/UI3DWidget/UI3DDisplay.cs index fcdc4e9..62568c2 100644 --- a/Runtime/UI3DWidget/UI3DDisplay.cs +++ b/Runtime/UI3DWidget/UI3DDisplay.cs @@ -1,109 +1,108 @@ -// using System; -// using Sirenix.OdinInspector; -// using UnityEngine; -// using UnityEngine.UI; -// -// namespace AlicizaX.UI.Extension -// { -// [RequireComponent(typeof(RawImage))] -// public class UI3DDisplay : MonoBehaviour -// { -// private Camera m_Camera; -// private GameObject m_ShowObject; -// private RenderTexture m_ShowTexure; -// private UI3DDisplayTouch m_UI3DDisplayTouch; -// -// private RawImage m_ShowImage; -// -// [ReadOnly] -// [SerializeField] -// private int m_ResolutionX = 512; -// -// [SerializeField] -// [ReadOnly] -// private int m_ResolutionY = 512; -// -// private readonly int m_RenderTextureDepthBuffer = 16; -// -// void Awake() -// { -// UI3DDisplayHelper.UI3DDisplayHandler = this; -// m_ShowImage = GetComponent(); -// m_UI3DDisplayTouch = GetComponentInChildren(); -// } -// -// -// #if UNITY_EDITOR -// -// -// [SerializeField] [Header("分辨率")] [OnValueChanged("OnEditResolutionChanged")] -// private int EditResolution = 512; -// -// private void OnEditResolutionChanged() -// { -// if (!getPlayingState) return; -// m_ResolutionX = EditResolution; -// m_ResolutionY = EditResolution; -// var rect = transform.GetComponent(); -// if (Math.Abs(rect.sizeDelta.x - m_ResolutionX) > 0.01f || -// Math.Abs(rect.sizeDelta.y - m_ResolutionY) > 0.01f) -// rect.sizeDelta = new Vector2(m_ResolutionX, m_ResolutionY); -// } -// -// -// [ShowIf("getPlayingState")] [SerializeField] -// GameObject previewPrefab; -// -// [ShowIf("getPlayingState")] [SerializeField] -// Camera previewCamera; -// -// bool getPlayingState => !Application.isPlaying; -// -// [Sirenix.OdinInspector.Button("预览")] -// private void Preview() -// { -// if (m_ShowObject != null) -// { -// UnityEngine.Object.DestroyImmediate(m_ShowObject); -// } -// -// m_ShowObject = UnityEngine.Object.Instantiate(previewPrefab); -// m_Camera = previewCamera; -// if (m_ShowTexure != null) -// { -// RenderTexture.ReleaseTemporary(m_ShowTexure); -// } -// -// m_ShowTexure = RenderTexture.GetTemporary(m_ResolutionX, m_ResolutionY, m_RenderTextureDepthBuffer); -// m_ShowObject.SetLayerRecursively(LayerDefine.UI3DObject); -// m_Camera.targetTexture = m_ShowTexure; -// GetComponent().texture = m_ShowTexure; -// } -// #endif -// -// public void SetUp(GameObject obj) -// { -// m_ShowObject = obj; -// m_Camera = CameraManaer.Instance.UI3DRenderCamera; -// if (m_ShowTexure != null) -// { -// RenderTexture.ReleaseTemporary(m_ShowTexure); -// } -// -// m_ShowTexure = RenderTexture.GetTemporary(m_ResolutionX, m_ResolutionY, m_RenderTextureDepthBuffer); -// m_ShowObject.SetLayerRecursively(LayerDefine.UI3DObject); -// m_Camera.targetTexture = m_ShowTexure; -// m_ShowImage.texture = m_ShowTexure; -// m_UI3DDisplayTouch.SetObject(m_ShowObject); -// } -// -// void OnDestroy() -// { -// m_UI3DDisplayTouch.Disopose(); -// if (m_ShowTexure != null) -// { -// RenderTexture.ReleaseTemporary(m_ShowTexure); -// } -// } -// } -// } +using System; +using Sirenix.OdinInspector; +using UnityEngine; +using UnityEngine.UI; + +namespace AlicizaX.UI.Extension +{ + [RequireComponent(typeof(RawImage))] + public class UI3DDisplay : MonoBehaviour + { + private Camera m_Camera; + private GameObject m_ShowObject; + private RenderTexture m_ShowTexure; + private UI3DDisplayTouch m_UI3DDisplayTouch; + + private RawImage m_ShowImage; + + [ReadOnly] + [SerializeField] + private int m_ResolutionX = 512; + + [SerializeField] + [ReadOnly] + private int m_ResolutionY = 512; + + private readonly int m_RenderTextureDepthBuffer = 16; + + void Awake() + { + m_ShowImage = GetComponent(); + m_UI3DDisplayTouch = GetComponentInChildren(); + } + + +#if UNITY_EDITOR + + + [SerializeField] [Header("分辨率")] [OnValueChanged("OnEditResolutionChanged")] + private int EditResolution = 512; + + private void OnEditResolutionChanged() + { + if (!getPlayingState) return; + m_ResolutionX = EditResolution; + m_ResolutionY = EditResolution; + var rect = transform.GetComponent(); + if (Math.Abs(rect.sizeDelta.x - m_ResolutionX) > 0.01f || + Math.Abs(rect.sizeDelta.y - m_ResolutionY) > 0.01f) + rect.sizeDelta = new Vector2(m_ResolutionX, m_ResolutionY); + } + + + [ShowIf("getPlayingState")] [SerializeField] + GameObject previewPrefab; + + [ShowIf("getPlayingState")] [SerializeField] + Camera previewCamera; + + bool getPlayingState => !Application.isPlaying; + + [Sirenix.OdinInspector.Button("预览")] + private void Preview() + { + if (m_ShowObject != null) + { + UnityEngine.Object.DestroyImmediate(m_ShowObject); + } + + m_ShowObject = UnityEngine.Object.Instantiate(previewPrefab); + m_Camera = previewCamera; + if (m_ShowTexure != null) + { + RenderTexture.ReleaseTemporary(m_ShowTexure); + } + + m_ShowTexure = RenderTexture.GetTemporary(m_ResolutionX, m_ResolutionY, m_RenderTextureDepthBuffer); + // m_ShowObject.SetLayerRecursively(LayerDefine.UI3DObject); + m_Camera.targetTexture = m_ShowTexure; + GetComponent().texture = m_ShowTexure; + } +#endif + + public void SetUp(GameObject obj) + { + m_ShowObject = obj; + // m_Camera = CameraManaer.Instance.UI3DRenderCamera; + if (m_ShowTexure != null) + { + RenderTexture.ReleaseTemporary(m_ShowTexure); + } + + m_ShowTexure = RenderTexture.GetTemporary(m_ResolutionX, m_ResolutionY, m_RenderTextureDepthBuffer); + // m_ShowObject.SetLayerRecursively(LayerDefine.UI3DObject); + m_Camera.targetTexture = m_ShowTexure; + m_ShowImage.texture = m_ShowTexure; + m_UI3DDisplayTouch.SetObject(m_ShowObject); + } + + void OnDestroy() + { + m_UI3DDisplayTouch.Disopose(); + if (m_ShowTexure != null) + { + RenderTexture.ReleaseTemporary(m_ShowTexure); + } + } + } +}