XRImage3D.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.LogError(BodyRender.material + " have no mainTexture");
  41. }
  42. }
  43. }
  44. public void SetColor(Color color)
  45. {
  46. if (BodyRender && BodyRender.enabled)
  47. {
  48. try
  49. {
  50. UnityUtil.ChangeMateColor(BodyRender, color);
  51. }
  52. catch (Exception ex)
  53. {
  54. UnityLog.LogError(BodyRender.material + " have no mainTexture");
  55. }
  56. }
  57. }
  58. public void SetSimple(Texture tex)
  59. {
  60. if (BodyRender && tex && BodyRender.enabled)
  61. {
  62. try
  63. {
  64. UnityUtil.ChangeMateTexture(BodyRender, tex);
  65. }
  66. catch (Exception ex)
  67. {
  68. UnityLog.LogError(BodyRender.material + " have no mainTexture");
  69. }
  70. }
  71. }
  72. public void SetSimple()
  73. {
  74. if (simple)
  75. {
  76. SetSimple(simple);
  77. }
  78. }
  79. /// <summary>
  80. /// 3D物体
  81. /// </summary>
  82. public MeshFilter Body
  83. {
  84. get
  85. {
  86. if (!body)
  87. {
  88. body = UnityUtil.GetBreadthChild<MeshFilter>(transform, "Body");
  89. }
  90. return body;
  91. }
  92. }
  93. public Transform Root
  94. {
  95. get
  96. {
  97. if (!root)
  98. {
  99. root = UnityUtil.GetBreadthChild<Transform>(transform, "Root");
  100. }
  101. return root;
  102. }
  103. set => root = value;
  104. }
  105. public Renderer BodyRender
  106. {
  107. get
  108. {
  109. if (!bodyRender)
  110. {
  111. bodyRender = UnityUtil.GetBreadthChild<Renderer>(transform, "Body");
  112. }
  113. return bodyRender;
  114. }
  115. }
  116. /// <summary>
  117. /// 如果不是全等缩放,则设置厚度
  118. /// </summary>
  119. public void SetThickness()
  120. {
  121. SetThickness(thickness);
  122. }
  123. public void SetRenderMesh(Mesh mesh)
  124. {
  125. if (Body && mesh && Body.sharedMesh != mesh)
  126. {
  127. Body.sharedMesh = mesh;
  128. Body.transform.localPosition = Vector3.zero;
  129. Body.transform.localRotation = Quaternion.identity;
  130. Vector3 center = mesh.bounds.center;
  131. Vector3 pos = -center;
  132. pos.z = -(mesh.bounds.center.z + mesh.bounds.size.z) / 2;
  133. Body.transform.localPosition = pos;
  134. }
  135. }
  136. public void SetRenderMate(Material mate)
  137. {
  138. if (BodyRender && mate)
  139. {
  140. BodyRender.material = mate;
  141. }
  142. }
  143. public void AutoSetSize()
  144. {
  145. if (Root)
  146. {
  147. Vector3 size = rectTransform.rect.size;
  148. if (!isAutoScale)
  149. {
  150. size.z = thickness;
  151. }
  152. else
  153. {
  154. size.z = rectTransform.rect.size.x > rectTransform.rect.size.y ?
  155. rectTransform.rect.size.y : rectTransform.rect.size.x;
  156. }
  157. Root.localScale = size * scale;
  158. }
  159. }
  160. public void SetThickness(float thickness)
  161. {
  162. this.thickness = thickness;
  163. if (!isAutoScale && Root)
  164. {
  165. Vector3 tmp = Root.localScale;
  166. tmp.z = thickness * scale;
  167. Root.localScale = tmp;
  168. }
  169. }
  170. /// <summary>
  171. /// 厚度变化动效
  172. /// </summary>
  173. /// <param name="pressDis">比例,相对于物体厚度的本身比例</param>
  174. /// <param name="pressTime">变化事件</param>
  175. public void DoThickness(float pressDis, float pressTime)
  176. {
  177. if (Root)
  178. {
  179. Root.DOKill();
  180. float target = thickness * pressDis;
  181. if (isAutoScale)
  182. {
  183. target = (rectTransform.rect.size.x > rectTransform.rect.size.y ?
  184. rectTransform.rect.size.y : rectTransform.rect.size.x) * scale;
  185. }
  186. Root.DOScaleZ(target, pressTime);
  187. }
  188. }
  189. protected override void OnRectTransformDimensionsChange()
  190. {
  191. base.OnRectTransformDimensionsChange();
  192. AutoSetSize();
  193. }
  194. }
  195. }