123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using DG.Tweening;
- using System;
- using UnityEngine;
- using UnityEngine.UI;
- using XRTool.Util;
- namespace XRTool.WorldUI
- {
- /// <summary>
- /// 基本的3D图形
- /// 与UI Image进行结合转化,自动换算
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 3D物体
- /// </summary>
- public MeshFilter Body
- {
- get
- {
- if (!body)
- {
- body = UnityUtil.GetBreadthChild<MeshFilter>(transform, "Body");
- }
- return body;
- }
- }
- public Transform Root
- {
- get
- {
- if (!root)
- {
- root = UnityUtil.GetBreadthChild<Transform>(transform, "Root");
- }
- return root;
- }
- set => root = value;
- }
- public Renderer BodyRender
- {
- get
- {
- if (!bodyRender)
- {
- bodyRender = UnityUtil.GetBreadthChild<Renderer>(transform, "Body");
- }
- return bodyRender;
- }
- }
- /// <summary>
- /// 如果不是全等缩放,则设置厚度
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 厚度变化动效
- /// </summary>
- /// <param name="pressDis">比例,相对于物体厚度的本身比例</param>
- /// <param name="pressTime">变化事件</param>
- 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();
- }
- }
- }
|