RawImage.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Serialization;
  4. namespace UnityEngine.UI
  5. {
  6. /// <summary>
  7. /// Displays a Texture2D for the UI System.
  8. /// </summary>
  9. /// <remarks>
  10. /// If you don't have or don't wish to create an atlas, you can simply use this script to draw a texture.
  11. /// Keep in mind though that this will create an extra draw call with each RawImage present, so it's
  12. /// best to use it only for backgrounds or temporary visible graphics.
  13. /// </remarks>
  14. [RequireComponent(typeof(CanvasRenderer))]
  15. [AddComponentMenu("UI/Raw Image", 12)]
  16. public class RawImage : MaskableGraphic
  17. {
  18. [FormerlySerializedAs("m_Tex")]
  19. [SerializeField] Texture m_Texture;
  20. [SerializeField] Rect m_UVRect = new Rect(0f, 0f, 1f, 1f);
  21. protected RawImage()
  22. {
  23. useLegacyMeshGeneration = false;
  24. }
  25. /// <summary>
  26. /// Returns the texture used to draw this Graphic.
  27. /// </summary>
  28. public override Texture mainTexture
  29. {
  30. get
  31. {
  32. if (m_Texture == null)
  33. {
  34. if (material != null && material.mainTexture != null)
  35. {
  36. return material.mainTexture;
  37. }
  38. return s_WhiteTexture;
  39. }
  40. return m_Texture;
  41. }
  42. }
  43. /// <summary>
  44. /// The RawImage's texture to be used.
  45. /// </summary>
  46. /// <remarks>
  47. /// Use this to alter or return the Texture the RawImage displays. The Raw Image can display any Texture whereas an Image component can only show a Sprite Texture.
  48. /// Note : Keep in mind that using a RawImage creates an extra draw call with each RawImage present, so it's best to use it only for backgrounds or temporary visible graphics.Note: Keep in mind that using a RawImage creates an extra draw call with each RawImage present, so it's best to use it only for backgrounds or temporary visible graphics.
  49. /// </remarks>
  50. /// <example>
  51. /// <code>
  52. /// //Create a new RawImage by going to Create>UI>Raw Image in the hierarchy.
  53. /// //Attach this script to the RawImage GameObject.
  54. ///
  55. /// using UnityEngine;
  56. /// using UnityEngine.UI;
  57. ///
  58. /// public class RawImageTexture : MonoBehaviour
  59. /// {
  60. /// RawImage m_RawImage;
  61. /// //Select a Texture in the Inspector to change to
  62. /// public Texture m_Texture;
  63. ///
  64. /// void Start()
  65. /// {
  66. /// //Fetch the RawImage component from the GameObject
  67. /// m_RawImage = GetComponent<RawImage>();
  68. /// //Change the Texture to be the one you define in the Inspector
  69. /// m_RawImage.texture = m_Texture;
  70. /// }
  71. /// }
  72. /// </code>
  73. /// </example>
  74. public Texture texture
  75. {
  76. get
  77. {
  78. return m_Texture;
  79. }
  80. set
  81. {
  82. if (m_Texture == value)
  83. return;
  84. m_Texture = value;
  85. SetVerticesDirty();
  86. SetMaterialDirty();
  87. }
  88. }
  89. /// <summary>
  90. /// UV rectangle used by the texture.
  91. /// </summary>
  92. public Rect uvRect
  93. {
  94. get
  95. {
  96. return m_UVRect;
  97. }
  98. set
  99. {
  100. if (m_UVRect == value)
  101. return;
  102. m_UVRect = value;
  103. SetVerticesDirty();
  104. }
  105. }
  106. /// <summary>
  107. /// Adjust the scale of the Graphic to make it pixel-perfect.
  108. /// </summary>
  109. /// <remarks>
  110. /// This means setting the RawImage's RectTransform.sizeDelta to be equal to the Texture dimensions.
  111. /// </remarks>
  112. public override void SetNativeSize()
  113. {
  114. Texture tex = mainTexture;
  115. if (tex != null)
  116. {
  117. int w = Mathf.RoundToInt(tex.width * uvRect.width);
  118. int h = Mathf.RoundToInt(tex.height * uvRect.height);
  119. rectTransform.anchorMax = rectTransform.anchorMin;
  120. rectTransform.sizeDelta = new Vector2(w, h);
  121. }
  122. }
  123. protected override void OnPopulateMesh(VertexHelper vh)
  124. {
  125. Texture tex = mainTexture;
  126. vh.Clear();
  127. if (tex != null)
  128. {
  129. var r = GetPixelAdjustedRect();
  130. var v = new Vector4(r.x, r.y, r.x + r.width, r.y + r.height);
  131. var scaleX = tex.width * tex.texelSize.x;
  132. var scaleY = tex.height * tex.texelSize.y;
  133. {
  134. var color32 = color;
  135. vh.AddVert(new Vector3(v.x, v.y), color32, new Vector2(m_UVRect.xMin * scaleX, m_UVRect.yMin * scaleY));
  136. vh.AddVert(new Vector3(v.x, v.w), color32, new Vector2(m_UVRect.xMin * scaleX, m_UVRect.yMax * scaleY));
  137. vh.AddVert(new Vector3(v.z, v.w), color32, new Vector2(m_UVRect.xMax * scaleX, m_UVRect.yMax * scaleY));
  138. vh.AddVert(new Vector3(v.z, v.y), color32, new Vector2(m_UVRect.xMax * scaleX, m_UVRect.yMin * scaleY));
  139. vh.AddTriangle(0, 1, 2);
  140. vh.AddTriangle(2, 3, 0);
  141. }
  142. }
  143. }
  144. protected override void OnDidApplyAnimationProperties()
  145. {
  146. SetMaterialDirty();
  147. SetVerticesDirty();
  148. }
  149. }
  150. }