using DG.Tweening; using System; using UnityEngine; using UnityEngine.UI; using XRTool.Util; namespace XRTool.WorldUI { /// /// 基本的3D图形 /// 与UI Image进行结合转化,自动换算 /// public class XRImage3D : Image { private Transform root; private MeshFilter body; private Renderer bodyRender; public bool isAutoScale = true; public float thickness = 100; public float scale = 0.5f; public Texture2D simple; protected override void Start() { base.Start(); if (simple) { SetSimple(); } } public void SetSimple(Texture2D tex) { this.simple = tex; if (BodyRender && tex && BodyRender.enabled) { try { UnityUtil.ChangeMateTexture2D(BodyRender, tex); } catch (Exception ex) { UnityLog.LogError(BodyRender.material + " have no mainTexture"); } } } public void SetColor(Color color) { if (BodyRender && BodyRender.enabled) { try { UnityUtil.ChangeMateColor(BodyRender, color); } catch (Exception ex) { UnityLog.LogError(BodyRender.material + " have no mainTexture"); } } } public void SetSimple(Texture tex) { if (BodyRender && tex && BodyRender.enabled) { try { UnityUtil.ChangeMateTexture(BodyRender, tex); } catch (Exception ex) { UnityLog.LogError(BodyRender.material + " have no mainTexture"); } } } public void SetSimple() { if (simple) { SetSimple(simple); } } /// /// 3D物体 /// public MeshFilter Body { get { if (!body) { body = UnityUtil.GetBreadthChild(transform, "Body"); } return body; } } public Transform Root { get { if (!root) { root = UnityUtil.GetBreadthChild(transform, "Root"); } return root; } set => root = value; } public Renderer BodyRender { get { if (!bodyRender) { bodyRender = UnityUtil.GetBreadthChild(transform, "Body"); } return bodyRender; } } /// /// 如果不是全等缩放,则设置厚度 /// public void SetThickness() { SetThickness(thickness); } public void SetRenderMesh(Mesh mesh) { if (Body && mesh && Body.sharedMesh != mesh) { Body.sharedMesh = mesh; Body.transform.localPosition = Vector3.zero; Body.transform.localRotation = Quaternion.identity; Vector3 center = mesh.bounds.center; Vector3 pos = -center; pos.z = -(mesh.bounds.center.z + mesh.bounds.size.z) / 2; Body.transform.localPosition = pos; } } public void SetRenderMate(Material mate) { if (BodyRender && mate) { BodyRender.material = mate; } } public void AutoSetSize() { if (Root) { Vector3 size = rectTransform.rect.size; if (!isAutoScale) { size.z = thickness; } else { size.z = rectTransform.rect.size.x > rectTransform.rect.size.y ? rectTransform.rect.size.y : rectTransform.rect.size.x; } Root.localScale = size * scale; } } public void SetThickness(float thickness) { this.thickness = thickness; if (!isAutoScale && Root) { Vector3 tmp = Root.localScale; tmp.z = thickness * scale; Root.localScale = tmp; } } /// /// 厚度变化动效 /// /// 比例,相对于物体厚度的本身比例 /// 变化事件 public void DoThickness(float pressDis, float pressTime) { if (Root) { Root.DOKill(); float target = thickness * pressDis; if (isAutoScale) { target = (rectTransform.rect.size.x > rectTransform.rect.size.y ? rectTransform.rect.size.y : rectTransform.rect.size.x) * scale; } Root.DOScaleZ(target, pressTime); } } protected override void OnRectTransformDimensionsChange() { base.OnRectTransformDimensionsChange(); AutoSetSize(); } } }