BetterRawImage.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace TheraBytes.BetterUi
  8. {
  9. [HelpURL("https://documentation.therabytes.de/better-ui/BetterRawImage.html")]
  10. [AddComponentMenu("Better UI/Controls/Better Raw Image", 30)]
  11. public class BetterRawImage : RawImage, IImageAppearanceProvider, IResolutionDependency
  12. {
  13. #region Nested Types
  14. [Serializable]
  15. public class TextureSettings : IScreenConfigConnection
  16. {
  17. public Texture Texture;
  18. public ColorMode ColorMode;
  19. public Color PrimaryColor;
  20. public Color SecondaryColor;
  21. public Rect UvRect;
  22. [SerializeField]
  23. string screenConfigName;
  24. public string ScreenConfigName { get { return screenConfigName; } set { screenConfigName = value; } }
  25. public TextureSettings(Texture texture, ColorMode colorMode, Color primary, Color secondary, Rect uvRect)
  26. {
  27. this.Texture = texture;
  28. this.ColorMode = colorMode;
  29. this.PrimaryColor = primary;
  30. this.SecondaryColor = secondary;
  31. this.UvRect = uvRect;
  32. }
  33. }
  34. [Serializable]
  35. public class TextureSettingsConfigCollection : SizeConfigCollection<TextureSettings> { }
  36. #endregion
  37. public string MaterialType
  38. {
  39. get { return materialType; }
  40. set { ImageAppearanceProviderHelper.SetMaterialType(value, this, materialProperties, ref materialEffect, ref materialType); }
  41. }
  42. public MaterialEffect MaterialEffect
  43. {
  44. get { return materialEffect; }
  45. set { ImageAppearanceProviderHelper.SetMaterialEffect(value, this, materialProperties, ref materialEffect, ref materialType); }
  46. }
  47. public VertexMaterialData MaterialProperties { get { return materialProperties; } }
  48. public ColorMode ColoringMode
  49. {
  50. get { return colorMode; }
  51. set
  52. {
  53. Config.Set(value, (o) => colorMode = value, (o) => CurrentTextureSettings.ColorMode = value);
  54. SetVerticesDirty();
  55. }
  56. }
  57. public Color SecondColor
  58. {
  59. get { return secondColor; }
  60. set
  61. {
  62. Config.Set(value, (o) => secondColor = value, (o) => CurrentTextureSettings.SecondaryColor = value);
  63. SetVerticesDirty();
  64. }
  65. }
  66. public override Color color
  67. {
  68. get { return base.color; }
  69. set
  70. {
  71. Config.Set(value, (o) => base.color = value, (o) => CurrentTextureSettings.PrimaryColor = value);
  72. }
  73. }
  74. public new Texture texture
  75. {
  76. get { return base.texture; }
  77. set
  78. {
  79. Config.Set(value, (o) => base.texture = value, (o) => CurrentTextureSettings.Texture = value);
  80. }
  81. }
  82. public new Rect uvRect
  83. {
  84. get { return base.uvRect; }
  85. set
  86. {
  87. Config.Set(value, (o) => base.uvRect = value, (o) => CurrentTextureSettings.UvRect = value);
  88. }
  89. }
  90. [SerializeField]
  91. ColorMode colorMode = ColorMode.Color;
  92. [SerializeField]
  93. Color secondColor = Color.white;
  94. [SerializeField]
  95. VertexMaterialData materialProperties = new VertexMaterialData();
  96. [SerializeField]
  97. string materialType;
  98. [SerializeField]
  99. MaterialEffect materialEffect;
  100. [SerializeField]
  101. float materialProperty1, materialProperty2, materialProperty3;
  102. [SerializeField]
  103. TextureSettings fallbackTextureSettings;
  104. [SerializeField]
  105. TextureSettingsConfigCollection customTextureSettings = new TextureSettingsConfigCollection();
  106. public TextureSettings CurrentTextureSettings
  107. {
  108. get
  109. {
  110. DoValidation();
  111. return customTextureSettings.GetCurrentItem(fallbackTextureSettings);
  112. }
  113. }
  114. protected override void OnEnable()
  115. {
  116. base.OnEnable();
  117. AssignTextureSettings();
  118. if (MaterialProperties.FloatProperties != null)
  119. {
  120. if (MaterialProperties.FloatProperties.Length > 0)
  121. materialProperty1 = MaterialProperties.FloatProperties[0].Value;
  122. if (MaterialProperties.FloatProperties.Length > 1)
  123. materialProperty2 = MaterialProperties.FloatProperties[1].Value;
  124. if (MaterialProperties.FloatProperties.Length > 2)
  125. materialProperty3 = MaterialProperties.FloatProperties[2].Value;
  126. }
  127. }
  128. public float GetMaterialPropertyValue(int propertyIndex)
  129. {
  130. return ImageAppearanceProviderHelper.GetMaterialPropertyValue(propertyIndex,
  131. ref materialProperty1, ref materialProperty2, ref materialProperty3);
  132. }
  133. public void SetMaterialProperty(int propertyIndex, float value)
  134. {
  135. ImageAppearanceProviderHelper.SetMaterialProperty(propertyIndex, value, this, materialProperties,
  136. ref materialProperty1, ref materialProperty2, ref materialProperty3);
  137. }
  138. protected override void OnPopulateMesh(VertexHelper vh)
  139. {
  140. Rect rect = GetPixelAdjustedRect();
  141. Vector2 pMin = new Vector2(rect.x, rect.y);
  142. Vector2 pMax = new Vector2(rect.x + rect.width, rect.y + rect.height);
  143. float w = (texture != null) ? (float)texture.width * texture.texelSize.x : 1;
  144. float h = (texture != null) ? (float)texture.height * texture.texelSize.y : 1;
  145. Vector2 uvMin = new Vector2(this.uvRect.xMin * w, this.uvRect.yMin * h);
  146. Vector2 uvMax = new Vector2(this.uvRect.xMax * w, this.uvRect.yMax * h);
  147. vh.Clear();
  148. ImageAppearanceProviderHelper.AddQuad(vh, rect,
  149. pMin, pMax,
  150. colorMode, color, secondColor,
  151. uvMin, uvMax,
  152. materialProperties);
  153. }
  154. public void OnResolutionChanged()
  155. {
  156. AssignTextureSettings();
  157. }
  158. private void AssignTextureSettings()
  159. {
  160. var settings = CurrentTextureSettings;
  161. this.texture = settings.Texture;
  162. this.colorMode = settings.ColorMode;
  163. this.color = settings.PrimaryColor;
  164. this.secondColor = settings.SecondaryColor;
  165. this.uvRect = settings.UvRect;
  166. }
  167. #if UNITY_EDITOR
  168. protected override void OnValidate()
  169. {
  170. base.OnValidate();
  171. DoValidation();
  172. AssignTextureSettings();
  173. }
  174. #endif
  175. void DoValidation()
  176. {
  177. bool isUnInitialized = fallbackTextureSettings == null
  178. || (fallbackTextureSettings.Texture == null
  179. && fallbackTextureSettings.ColorMode == ColorMode.Color
  180. && fallbackTextureSettings.PrimaryColor == new Color()
  181. && uvRect == new Rect());
  182. if (isUnInitialized)
  183. {
  184. fallbackTextureSettings = new TextureSettings(this.texture, this.colorMode, this.color, this.secondColor, this.uvRect);
  185. }
  186. }
  187. }
  188. }