XRImage3D.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using DG.Tweening;
  2. using System;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using XRTool.Util;
  6. namespace XRTool.WorldUI
  7. {
  8. /// <summary>
  9. /// 基本的3D图形
  10. /// 与UI Image进行结合转化,自动换算
  11. /// </summary>
  12. public class XRImage3D : Image
  13. {
  14. private Transform root;
  15. private MeshFilter body;
  16. private Renderer bodyRender;
  17. public bool isAutoScale = true;
  18. public float thickness = 100;
  19. public float scale = 0.5f;
  20. public Texture2D simple;
  21. protected override void Start()
  22. {
  23. base.Start();
  24. if (simple)
  25. {
  26. SetSimple();
  27. }
  28. }
  29. public void SetSimple(Texture2D tex)
  30. {
  31. this.simple = tex;
  32. if (BodyRender && tex && BodyRender.enabled)
  33. {
  34. try
  35. {
  36. UnityUtil.ChangeMateTexture2D(BodyRender, tex);
  37. }
  38. catch (Exception ex)
  39. {
  40. UnityLog.Instance.LogError(BodyRender.material + " have no mainTexture");
  41. }
  42. }
  43. }
  44. public void SetColor(Color color) {
  45. if (BodyRender && BodyRender.enabled)
  46. {
  47. try
  48. {
  49. UnityUtil.ChangeMateColor(BodyRender, color);
  50. }
  51. catch (Exception ex)
  52. {
  53. UnityLog.Instance.LogError(BodyRender.material + " have no mainTexture");
  54. }
  55. }
  56. }
  57. public void SetSimple(Texture tex)
  58. {
  59. if (BodyRender && tex && BodyRender.enabled)
  60. {
  61. try
  62. {
  63. UnityUtil.ChangeMateTexture(BodyRender, tex);
  64. }
  65. catch (Exception ex)
  66. {
  67. UnityLog.Instance.LogError(BodyRender.material + " have no mainTexture");
  68. }
  69. }
  70. }
  71. public void SetSimple()
  72. {
  73. if (simple)
  74. {
  75. SetSimple(simple);
  76. }
  77. }
  78. /// <summary>
  79. /// 3D物体
  80. /// </summary>
  81. public MeshFilter Body
  82. {
  83. get
  84. {
  85. if (!body)
  86. {
  87. body = UnityUtil.GetBreadthChild<MeshFilter>(transform, "Body");
  88. }
  89. return body;
  90. }
  91. }
  92. public Transform Root
  93. {
  94. get
  95. {
  96. if (!root)
  97. {
  98. root = UnityUtil.GetBreadthChild<Transform>(transform, "Root");
  99. }
  100. return root;
  101. }
  102. set => root = value;
  103. }
  104. public Renderer BodyRender
  105. {
  106. get
  107. {
  108. if (!bodyRender)
  109. {
  110. bodyRender = UnityUtil.GetBreadthChild<Renderer>(transform, "Body");
  111. }
  112. return bodyRender;
  113. }
  114. }
  115. /// <summary>
  116. /// 如果不是全等缩放,则设置厚度
  117. /// </summary>
  118. public void SetThickness()
  119. {
  120. SetThickness(thickness);
  121. }
  122. public void SetRenderMesh(Mesh mesh)
  123. {
  124. if (Body && mesh && Body.sharedMesh != mesh)
  125. {
  126. Body.sharedMesh = mesh;
  127. Body.transform.localPosition = Vector3.zero;
  128. Body.transform.localRotation = Quaternion.identity;
  129. Vector3 center = mesh.bounds.center;
  130. Vector3 pos = -center;
  131. pos.z = -(mesh.bounds.center.z + mesh.bounds.size.z) / 2;
  132. Body.transform.localPosition = pos;
  133. }
  134. }
  135. public void SetRenderMate(Material mate)
  136. {
  137. if (BodyRender && mate)
  138. {
  139. BodyRender.material = mate;
  140. }
  141. }
  142. public void AutoSetSize()
  143. {
  144. if (Root)
  145. {
  146. Vector3 size = rectTransform.rect.size;
  147. if (!isAutoScale)
  148. {
  149. size.z = thickness;
  150. }
  151. else
  152. {
  153. size.z = rectTransform.rect.size.x > rectTransform.rect.size.y ?
  154. rectTransform.rect.size.y : rectTransform.rect.size.x;
  155. }
  156. Root.localScale = size * scale;
  157. }
  158. }
  159. public void SetThickness(float thickness)
  160. {
  161. this.thickness = thickness;
  162. if (!isAutoScale && Root)
  163. {
  164. Vector3 tmp = Root.localScale;
  165. tmp.z = thickness * scale;
  166. Root.localScale = tmp;
  167. }
  168. }
  169. /// <summary>
  170. /// 厚度变化动效
  171. /// </summary>
  172. /// <param name="pressDis">比例,相对于物体厚度的本身比例</param>
  173. /// <param name="pressTime">变化事件</param>
  174. public void DoThickness(float pressDis, float pressTime)
  175. {
  176. if (Root)
  177. {
  178. Root.DOKill();
  179. float target = thickness * pressDis;
  180. if (isAutoScale)
  181. {
  182. target = (rectTransform.rect.size.x > rectTransform.rect.size.y ?
  183. rectTransform.rect.size.y : rectTransform.rect.size.x) * scale;
  184. }
  185. Root.DOScaleZ(target, pressTime);
  186. }
  187. }
  188. protected override void OnRectTransformDimensionsChange()
  189. {
  190. base.OnRectTransformDimensionsChange();
  191. AutoSetSize();
  192. }
  193. }
  194. }