From b58b34c80a6fd5f548620da48deaf274b35bdcd9 Mon Sep 17 00:00:00 2001 From: shenjianxing <”315615051@qq.com“> Date: Mon, 10 Feb 2025 10:54:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84device=E7=9A=843D=E5=B1=95?= =?UTF-8?q?=E7=A4=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Art/UIPrefab/UI3DObjShow.prefab | 16 ++-- Assets/Art/UIPrefab/UIBtns.prefab | 2 +- Assets/Scripts/Item/Show3DCamera.cs | 119 ++++++++++++++++--------- Assets/Scripts/Item/TipItem.cs | 13 ++- Assets/Scripts/UI/UI3DObjShow.cs | 14 ++- Assets/Scripts/Xml/XmlParser.cs | 10 +++ Doc/Xml配置文档.xml | 17 ++++ 7 files changed, 135 insertions(+), 56 deletions(-) diff --git a/Assets/Art/UIPrefab/UI3DObjShow.prefab b/Assets/Art/UIPrefab/UI3DObjShow.prefab index 56bb1365..06410a52 100644 --- a/Assets/Art/UIPrefab/UI3DObjShow.prefab +++ b/Assets/Art/UIPrefab/UI3DObjShow.prefab @@ -309,13 +309,13 @@ RectTransform: m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] - m_Father: {fileID: 826586917723392204} + m_Father: {fileID: 8188781184013275445} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5} - m_AnchoredPosition: {x: 0, y: -0} + m_AnchoredPosition: {x: -640, y: -360} m_SizeDelta: {x: 1280, y: 720} - m_Pivot: {x: 0.5, y: 0.5} + m_Pivot: {x: 0, y: 0} --- !u!222 &3370089183326341287 CanvasRenderer: m_ObjectHideFlags: 0 @@ -364,7 +364,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: MarkType: 0 - CustomComponentName: + CustomComponentName: DeviceRawImage CustomComment: mComponentName: UnityEngine.UI.RawImage --- !u!1 &1587529206278800681 @@ -379,7 +379,7 @@ GameObject: - component: {fileID: 7277770534815806214} - component: {fileID: 5964642934946449171} m_Layer: 0 - m_Name: bg + m_Name: Right m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -744,7 +744,7 @@ GameObject: - component: {fileID: 1954549762747143972} - component: {fileID: 2228057524689582266} m_Layer: 0 - m_Name: Show + m_Name: Bg m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -761,8 +761,7 @@ RectTransform: m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 - m_Children: - - {fileID: 1614371002034144690} + m_Children: [] m_Father: {fileID: 8188781184013275445} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0.5, y: 0.5} @@ -947,6 +946,7 @@ RectTransform: m_Children: - {fileID: 1915876869541640623} - {fileID: 826586917723392204} + - {fileID: 1614371002034144690} - {fileID: 2836954293480787745} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Assets/Art/UIPrefab/UIBtns.prefab b/Assets/Art/UIPrefab/UIBtns.prefab index d359738d..a8214ef7 100644 --- a/Assets/Art/UIPrefab/UIBtns.prefab +++ b/Assets/Art/UIPrefab/UIBtns.prefab @@ -192,7 +192,7 @@ MonoBehaviour: m_Spacing: 15 m_ChildForceExpandWidth: 0 m_ChildForceExpandHeight: 1 - m_ChildControlWidth: 0 + m_ChildControlWidth: 1 m_ChildControlHeight: 0 m_ChildScaleWidth: 0 m_ChildScaleHeight: 1 diff --git a/Assets/Scripts/Item/Show3DCamera.cs b/Assets/Scripts/Item/Show3DCamera.cs index c6e08970..a88bd901 100644 --- a/Assets/Scripts/Item/Show3DCamera.cs +++ b/Assets/Scripts/Item/Show3DCamera.cs @@ -1,13 +1,19 @@ +using GCSeries.Core; +using System; +using Unity.Burst.CompilerServices; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; +using UnityEngine.UIElements; public class Show3DCamera : MonoBehaviour { - public Transform target; // ΧתĿ + public Vector3 targetPos; // ΧתĿ public float rotateSpeed = 10f; // תٶ public float moveSpeed = 10f; // ƶٶ public float distance = 10f; // Ŀľ + public float distanceMin = 0.2f; + public float distanceMax = 20f; public Vector2 pitchMinMax = new Vector2(-20, 80); // ǷΧ private Vector3 offset; // Ŀƫ @@ -16,10 +22,11 @@ public class Show3DCamera : MonoBehaviour public static Show3DCamera instance; - int rawWidth; - int rawHeight; + RectTransform inputRect; Camera self; + private GameObject lastHitObject = null; + private void Awake() { instance = this; @@ -27,35 +34,34 @@ public class Show3DCamera : MonoBehaviour DontDestroyOnLoad(this); gameObject.SetActive(false); } - public void Set(int width,int height, Transform target, float rotateSpeed = 10, float moveSpeed = 0.1f, float distance = 0.1f, float pitchMin = -20, float pitchMax = 80) + public void Set(Transform target, float rotateSpeed = 10, float moveSpeed = 0.1f, float distance = 0.1f, float pitchMin = -20, float pitchMax = 80, float distanceMin = 0.2f, float distanceMax = 20f, RectTransform inputRect = null) { if (target == null) { Debug.LogError("Target is not assigned!"); return; } - this.rawWidth = width; - this.rawHeight = height; + yaw = 0; pitch = 0; - this.target = target.transform; + this.inputRect = inputRect; + this.targetPos = target.transform.position; this.rotateSpeed = rotateSpeed; this.moveSpeed = moveSpeed; this.distance = distance; + this.distanceMin = distanceMin; + this.distanceMax = distanceMax; this.pitchMinMax = new Vector2(pitchMin, pitchMax); // ʼλ offset = new Vector3(0, 0, -distance); UpdateCameraPosition(); } - public void Clear() - { - target = null; - } + void Update() { - if (target != null) + if (targetPos != null) { // סʱת if (Input.GetMouseButton(0)) @@ -76,45 +82,76 @@ public class Show3DCamera : MonoBehaviour { MoveTarget(); } - DetectHoveredObject(); } } // ͣ - private void DetectHoveredObject() + public void DetectHoveredObject() { - // - Ray ray = self.ScreenPointToRay(ConvertScreenToRenderTextureCoordinates(Input.mousePosition)); - if (Physics.Raycast(ray, out RaycastHit hit)) + GameObject obj = null; + Vector2 mousePosition = Input.mousePosition; + if (inputRect != null) { - // ӡ - Debug.LogError("Hovered Object: " + hit.collider.gameObject.name); + var pos = (mousePosition - (Vector2)inputRect.position) / inputRect.lossyScale - inputRect.rect.position; + mousePosition = pos / inputRect.rect.size; } - Debug.DrawLine(ray.origin, hit.point, Color.red); + + var ray = self.ViewportPointToRay(mousePosition); + RaycastHit raycastHit; + if (Physics.Raycast(ray, out raycastHit)) + { + //Debug.Log(raycastHit.transform.name); + obj = raycastHit.transform.gameObject; + + + // еһλе岻ͬʾ߽ + if (obj != lastHitObject) + { + // ¼ + OnMouseEnterObj(obj); + + // һλе岻Ϊգ뿪¼ + if (lastHitObject != null && lastHitObject != obj) + { + OnMouseExitObj(lastHitObject); + // һλе + lastHitObject = obj; + } + + } + } + else + { + // ûлκ壬һλе岻Ϊգ뿪¼ + if (lastHitObject != null) + { + OnMouseExitObj(lastHitObject); + lastHitObject = null; + } + } + Debug.DrawLine(ray.origin, raycastHit.point, Color.red); } - private Vector3 ConvertScreenToRenderTextureCoordinates(Vector3 screenPosition) + + public void OnMouseEnterObj(GameObject obj) { - // ȡĻֱ - int screenWidth = Screen.width; - int screenHeight = Screen.height; - - - // - float scaleX = (float)rawWidth / screenWidth; - float scaleY = (float)rawHeight / screenHeight; - - // תλ - Vector3 renderTexturePosition = new Vector3( - screenPosition.x * scaleX, - screenPosition.y * scaleY, - screenPosition.z - ); - - return renderTexturePosition; + TipItem tip = obj.GetComponent(); + if (tip != null) + { + tip.OnEnter(); + } } + public void OnMouseExitObj(GameObject obj) + { + TipItem tip = obj.GetComponent(); + if (tip != null) + { + tip.OnExit(); + } + + } // ƶĿ private void MoveTarget() @@ -128,7 +165,7 @@ public class Show3DCamera : MonoBehaviour private void ZoomCamera(float scroll) { distance -= scroll * 5f; // ٶ - distance = Mathf.Clamp(distance, 0.2f, 20f); // ƾ뷶Χ + distance = Mathf.Clamp(distance, distanceMin, distanceMax); // ƾ뷶Χ offset = new Vector3(0, 0, -distance); UpdateCameraPosition(); } @@ -157,10 +194,10 @@ public class Show3DCamera : MonoBehaviour Vector3 rotatedOffset = rotation * offset; // λ - transform.position = target.position + rotatedOffset; + transform.position = targetPos + rotatedOffset; // ʼճĿ - transform.LookAt(target); + transform.LookAt(targetPos); } } diff --git a/Assets/Scripts/Item/TipItem.cs b/Assets/Scripts/Item/TipItem.cs index d0f746b0..3f27cb95 100644 --- a/Assets/Scripts/Item/TipItem.cs +++ b/Assets/Scripts/Item/TipItem.cs @@ -16,6 +16,11 @@ public class TipItem : MonoBehaviour } private void OnMouseEnter() + { + OnEnter(); + } + + public void OnEnter() { tip = UIKit.GetPanel(); if (tip == null) @@ -33,8 +38,7 @@ public class TipItem : MonoBehaviour tip.Active(true); } } - - private void OnMouseExit() + public void OnExit() { if (tip != null) { @@ -42,6 +46,11 @@ public class TipItem : MonoBehaviour } } + private void OnMouseExit() + { + OnExit(); + } + private void OnMouseUpAsButton() { if (tip != null) diff --git a/Assets/Scripts/UI/UI3DObjShow.cs b/Assets/Scripts/UI/UI3DObjShow.cs index 1c88b2e5..8c01c049 100644 --- a/Assets/Scripts/UI/UI3DObjShow.cs +++ b/Assets/Scripts/UI/UI3DObjShow.cs @@ -3,8 +3,9 @@ using UnityEngine.UI; using QFramework; using XMLTool; using System.Collections.Generic; -using static UnityEditor.Progress; using TMPro; +using GCSeries.Core; +using UnityEngine.EventSystems; namespace QFramework.Example { @@ -18,6 +19,8 @@ namespace QFramework.Example public float rotateSpeed = 10; public float moveSpeed = 0.1f; public float distance = 0.2f; + public float distanceMin = 0.2f; + public float distanceMax = 20f; public float pitchMin = -30f; public float pitchMax = 80; public string deviceName; @@ -28,15 +31,18 @@ namespace QFramework.Example { ResLoader loader; GameObject curObj; + RectTransform rawImg; protected override void OnInit(IUIData uiData = null) { loader = ResLoader.Allocate(); + rawImg = DeviceRawImage.GetComponent(); } protected override void OnOpen(IUIData uiData = null) { mData = uiData as UI3DObjShowData ?? new UI3DObjShowData(); FreeCameraController.instance.SetLock(false, false); + Show3DCamera.instance.gameObject.SetActive(true); Content.RemoveAllChildren(); foreach (var data in mData.datas) { @@ -63,13 +69,12 @@ namespace QFramework.Example curObj.transform.localScale = data.scale; curObj.transform.eulerAngles = data.rotate; curObj.gameObject.SetActive(true); - Show3DCamera.instance.Set(DeviceRawImage.texture.width, DeviceRawImage.texture.height, curObj.transform, data.rotateSpeed, data.moveSpeed, data.distance, data.pitchMin, data.pitchMax); + Show3DCamera.instance.Set(curObj.transform, data.rotateSpeed, data.moveSpeed, data.distance, data.pitchMin, data.pitchMax, distanceMin: data.distanceMin, distanceMax: data.distanceMax, inputRect: rawImg); } else { if (curObj) { - Show3DCamera.instance.Clear(); GameObject.Destroy(curObj); curObj = null; } @@ -79,7 +84,6 @@ namespace QFramework.Example } loader.LoadAsync(); } - protected override void OnShow() { } @@ -87,11 +91,13 @@ namespace QFramework.Example protected override void OnHide() { FreeCameraController.instance.SetLock(true, true); + Show3DCamera.instance.gameObject.SetActive(false); loader.Recycle2Cache(); } protected override void OnClose() { } + } } diff --git a/Assets/Scripts/Xml/XmlParser.cs b/Assets/Scripts/Xml/XmlParser.cs index 4886009a..8b3d7ef0 100644 --- a/Assets/Scripts/Xml/XmlParser.cs +++ b/Assets/Scripts/Xml/XmlParser.cs @@ -1325,6 +1325,16 @@ namespace XMLTool { float.TryParse(distance.Value, out objData.distance); } + XAttribute distanceMin = item.Attribute("distanceMin"); + if (distanceMin != null) + { + float.TryParse(distanceMin.Value, out objData.distanceMin); + } + XAttribute distanceMax = item.Attribute("distanceMax"); + if (distanceMax != null) + { + float.TryParse(distanceMax.Value, out objData.distanceMax); + } XAttribute pitchMin = item.Attribute("pitchMin"); if (pitchMin != null) { diff --git a/Doc/Xml配置文档.xml b/Doc/Xml配置文档.xml index 740123f1..d0ae54d8 100644 --- a/Doc/Xml配置文档.xml +++ b/Doc/Xml配置文档.xml @@ -208,6 +208,23 @@ + + + + + +