BetterRawImageEditor.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using UnityEditor;
  7. using UnityEditor.UI;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. namespace TheraBytes.BetterUi.Editor
  11. {
  12. [CustomEditor(typeof(BetterRawImage)), CanEditMultipleObjects]
  13. public class BetterRawImageEditor : GraphicEditor
  14. {
  15. private SerializedProperty m_Texture;
  16. private GUIContent m_UVRectContent;
  17. private SerializedProperty textureSetingsFallbackProp;
  18. private SerializedProperty textureSetingsCollectionProp;
  19. private SerializedProperty maskable;
  20. ImageAppearanceProviderEditorHelper materialDrawer;
  21. /// <summary>
  22. /// <para>A string cointaining the Image details to be used as a overlay on the component Preview.</para>
  23. /// </summary>
  24. /// <returns>
  25. /// <para>The RawImage details.</para>
  26. /// </returns>
  27. public override string GetInfoString()
  28. {
  29. BetterRawImage rawImage = this.target as BetterRawImage;
  30. Rect rect = rawImage.rectTransform.rect;
  31. object num = Mathf.RoundToInt(Mathf.Abs(rect.width));
  32. Rect rect1 = rawImage.rectTransform.rect;
  33. string str = string.Format("RawImage Size: {0}x{1}", num, Mathf.RoundToInt(Mathf.Abs(rect1.height)));
  34. return str;
  35. }
  36. /// <summary>
  37. /// <para>Can this component be Previewed in its current state?</para>
  38. /// </summary>
  39. /// <returns>
  40. /// <para>True if this component can be Previewed in its current state.</para>
  41. /// </returns>
  42. public override bool HasPreviewGUI()
  43. {
  44. BetterRawImage rawImage = this.target as BetterRawImage;
  45. if (rawImage == null)
  46. {
  47. return false;
  48. }
  49. Rect rect = BetterRawImageEditor.Outer(rawImage);
  50. return (rect.width <= 0f ? false : rect.height > 0f);
  51. }
  52. protected override void OnEnable()
  53. {
  54. base.OnEnable();
  55. BetterRawImage img = this.target as BetterRawImage;
  56. this.materialDrawer = new ImageAppearanceProviderEditorHelper(base.serializedObject, img);
  57. this.m_UVRectContent = new GUIContent("UV Rect");
  58. this.m_Texture = base.serializedObject.FindProperty("m_Texture");
  59. this.textureSetingsFallbackProp = base.serializedObject.FindProperty("fallbackTextureSettings");
  60. this.textureSetingsCollectionProp = base.serializedObject.FindProperty("customTextureSettings");
  61. this.maskable = base.serializedObject.FindProperty("m_Maskable");
  62. this.SetShowNativeSize(true);
  63. }
  64. /// <summary>
  65. /// <para>Implement specific RawImage inspector GUI code here. If you want to simply extend the existing editor call the base OnInspectorGUI () before doing any custom GUI code.</para>
  66. /// </summary>
  67. public override void OnInspectorGUI()
  68. {
  69. base.serializedObject.Update();
  70. ScreenConfigConnectionHelper.DrawGui("Texture Settings", textureSetingsCollectionProp, ref textureSetingsFallbackProp, DrawTextureSettings);
  71. //base.AppearanceControlsGUI();
  72. materialDrawer.DrawMaterialGui(base.m_Material);
  73. base.RaycastControlsGUI();
  74. this.SetShowNativeSize(false);
  75. base.NativeSizeButtonGUI();
  76. if (maskable != null)
  77. {
  78. EditorGUILayout.PropertyField(maskable);
  79. }
  80. base.serializedObject.ApplyModifiedProperties();
  81. }
  82. private void DrawTextureSettings(string configName, SerializedProperty property)
  83. {
  84. SerializedProperty textureProp = property.FindPropertyRelative("Texture");
  85. EditorGUILayout.PropertyField(textureProp);
  86. SerializedProperty primeColorProp = property.FindPropertyRelative("PrimaryColor");
  87. SerializedProperty colorModeProp = property.FindPropertyRelative("ColorMode");
  88. SerializedProperty secondColorProp = property.FindPropertyRelative("SecondaryColor");
  89. ImageAppearanceProviderEditorHelper.DrawColorGui(colorModeProp, primeColorProp, secondColorProp);
  90. SerializedProperty uvProp = property.FindPropertyRelative("UvRect");
  91. EditorGUILayout.PropertyField(uvProp, m_UVRectContent);
  92. }
  93. private struct IntRect
  94. {
  95. public int x, y, width, height;
  96. public override string ToString()
  97. {
  98. return "x: " + x + "\ty: " + y + "\twidth: " + width + "\theight: " + height;
  99. }
  100. }
  101. /// <summary>
  102. /// <para>Custom preview for Image component.</para>
  103. /// </summary>
  104. /// <param name="rect">Rectangle in which to draw the preview.</param>
  105. /// <param name="background">Background image.</param>
  106. public override void OnPreviewGUI(Rect rect, GUIStyle background)
  107. {
  108. if (Event.current.type == EventType.Repaint)
  109. {
  110. BetterRawImage betterRawImage = this.target as BetterRawImage;
  111. if (betterRawImage == null || betterRawImage.texture == null)
  112. {
  113. return;
  114. }
  115. Texture rawTexture = betterRawImage.mainTexture;
  116. Rect uvRect = betterRawImage.uvRect;
  117. // Convert texture into readable texture2D
  118. RenderTexture previousRenderTexture = RenderTexture.active;
  119. RenderTexture tmp = RenderTexture.GetTemporary(rawTexture.width, rawTexture.height);
  120. Graphics.Blit(rawTexture, tmp);
  121. Texture2D rawTexture2D = new Texture2D(rawTexture.width, rawTexture.height);
  122. rawTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
  123. rawTexture2D.Apply();
  124. RenderTexture.active = previousRenderTexture;
  125. tmp.Release();
  126. // Fun calculations
  127. IntRect baseRect;
  128. baseRect.width = Mathf.Clamp((int) (rawTexture.width * uvRect.width), 0, rawTexture.width);
  129. baseRect.height = Mathf.Clamp((int) (rawTexture.height * uvRect.height), 0, rawTexture.height);
  130. baseRect.x = (int)(rawTexture.width * uvRect.x);
  131. baseRect.y = (int)(rawTexture.height * uvRect.y);
  132. IntRect textureCoordinates;
  133. textureCoordinates.width = Mathf.Clamp(baseRect.width, 0, rawTexture2D.width - baseRect.x);
  134. textureCoordinates.height = Mathf.Clamp(baseRect.height, 0, rawTexture2D.height - baseRect.y);
  135. textureCoordinates.x = Mathf.Clamp(baseRect.x, 0, rawTexture2D.width);
  136. textureCoordinates.y = Mathf.Clamp(baseRect.y, 0, rawTexture2D.height);
  137. int previewWidth = (int) Mathf.Abs(rawTexture.width * uvRect.width);
  138. int previewHeight = (int) Mathf.Abs(rawTexture.height * uvRect.height);
  139. IntRect drawCoordinates;
  140. drawCoordinates.x = uvRect.x > 0 ? 0 : (int)(textureCoordinates.width * Mathf.Abs(uvRect.x));
  141. drawCoordinates.y = uvRect.y > 0 ? 0 : (int)(textureCoordinates.height * Mathf.Abs(uvRect.y));
  142. drawCoordinates.width = Mathf.Clamp(textureCoordinates.width, 0, previewWidth - drawCoordinates.x);
  143. drawCoordinates.height = Mathf.Clamp(textureCoordinates.height, 0, previewHeight - drawCoordinates.y);
  144. // fix cutoffs
  145. if (textureCoordinates.width > drawCoordinates.width)
  146. {
  147. textureCoordinates.width = drawCoordinates.width;
  148. }
  149. if (textureCoordinates.height > drawCoordinates.height)
  150. {
  151. textureCoordinates.height = drawCoordinates.height;
  152. }
  153. // Create actual Preview
  154. Texture2D preview = new Texture2D(previewWidth, previewHeight);
  155. if (drawCoordinates.width > 0 && drawCoordinates.height > 0)
  156. {
  157. Color[] pixels = rawTexture2D.GetPixels(textureCoordinates.x, textureCoordinates.y, textureCoordinates.width, textureCoordinates.height);
  158. preview.SetPixels(drawCoordinates.x, drawCoordinates.y, drawCoordinates.width, drawCoordinates.height, pixels);
  159. preview.Apply(true, false);
  160. }
  161. EditorGUI.DrawTextureTransparent(rect, preview, ScaleMode.ScaleToFit);
  162. }
  163. }
  164. private static Rect Outer(BetterRawImage rawImage)
  165. {
  166. Rect rect = rawImage.uvRect;
  167. rect.xMin = rect.xMin * rawImage.rectTransform.rect.width;
  168. rect.xMax = rect.xMax * rawImage.rectTransform.rect.width;
  169. rect.yMin = rect.yMin * rawImage.rectTransform.rect.height;
  170. rect.yMax = rect.yMax * rawImage.rectTransform.rect.height;
  171. return rect;
  172. }
  173. private void SetShowNativeSize(bool instant)
  174. {
  175. base.SetShowNativeSize(this.m_Texture.objectReferenceValue != null, instant);
  176. }
  177. [MenuItem("CONTEXT/RawImage/♠ Make Better")]
  178. public static void MakeBetter(MenuCommand command)
  179. {
  180. RawImage img = command.context as RawImage;
  181. var texture = img.texture;
  182. var col = img.color;
  183. var uv = img.uvRect;
  184. RawImage newImg = Betterizer.MakeBetter<RawImage, BetterRawImage>(img);
  185. BetterRawImage better = newImg as BetterRawImage;
  186. if (better != null)
  187. {
  188. better.CurrentTextureSettings.Texture = texture;
  189. better.CurrentTextureSettings.PrimaryColor = col;
  190. better.CurrentTextureSettings.UvRect = uv;
  191. #if !UNITY_4 && !UNITY_5_0 && !UNITY_5_1 && !UNITY_5_2 && !UNITY_5_3 && !UNITY_5_4 && !UNITY_5_5 // from UNITY 5.6 on
  192. // ensure shader channels in canvas
  193. Canvas canvas = better.transform.GetComponentInParent<Canvas>();
  194. canvas.additionalShaderChannels = canvas.additionalShaderChannels
  195. | AdditionalCanvasShaderChannels.TexCoord1
  196. | AdditionalCanvasShaderChannels.Tangent;
  197. #endif
  198. }
  199. }
  200. }
  201. }