BetterImageEditor.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditor.AnimatedValues;
  6. using UnityEditor.UI;
  7. using UnityEngine;
  8. using UnityEngine.Events;
  9. using UnityEngine.UI;
  10. namespace TheraBytes.BetterUi.Editor
  11. {
  12. [CustomEditor(typeof(BetterImage)), CanEditMultipleObjects]
  13. public class BetterImageEditor : GraphicEditor
  14. {
  15. private SerializedProperty m_FillMethod;
  16. private SerializedProperty m_FillOrigin;
  17. private SerializedProperty m_FillAmount;
  18. private SerializedProperty m_FillClockwise;
  19. private SerializedProperty m_Type;
  20. private SerializedProperty m_FillCenter;
  21. private SerializedProperty m_Sprite;
  22. private SerializedProperty m_PreserveAspect;
  23. private SerializedProperty m_useSpriteMesh;
  24. private SerializedProperty m_pixelPerUnitMultiplyer;
  25. private SerializedProperty m_maskable;
  26. private SerializedProperty spriteSetingsFallbackProp;
  27. private SerializedProperty spriteSetingsCollectionProp;
  28. private GUIContent m_SpriteContent;
  29. private GUIContent m_SpriteTypeContent;
  30. private GUIContent m_ClockwiseContent;
  31. private GUIContent m_UseSpriteMeshContent;
  32. private AnimBool m_ShowSlicedOrTiled;
  33. private AnimBool m_ShowSliced;
  34. private AnimBool m_ShowFilled;
  35. private AnimBool m_ShowType;
  36. ImageAppearanceProviderEditorHelper materialDrawer;
  37. BetterImage image;
  38. public override bool HasPreviewGUI()
  39. {
  40. image = this.target as BetterImage;
  41. if (image == null)
  42. {
  43. return false;
  44. }
  45. return image.sprite != null;
  46. }
  47. public override string GetInfoString()
  48. {
  49. image = this.target as BetterImage;
  50. Rect rect = image.rectTransform.rect;
  51. object num = Mathf.RoundToInt(Mathf.Abs(rect.width));
  52. Rect rect1 = image.rectTransform.rect;
  53. return string.Format("Image Size: {0}x{1}", num, Mathf.RoundToInt(Mathf.Abs(rect1.height)));
  54. }
  55. protected override void OnEnable()
  56. {
  57. image = target as BetterImage;
  58. this.materialDrawer = new ImageAppearanceProviderEditorHelper(base.serializedObject, image);
  59. base.OnEnable();
  60. this.m_SpriteContent = new GUIContent("Source Image");
  61. this.m_SpriteTypeContent = new GUIContent("Image Type");
  62. this.m_ClockwiseContent = new GUIContent("Clockwise");
  63. this.m_UseSpriteMeshContent = new GUIContent("Use Sprite Mesh",
  64. "In Better UI, this option is not supported. If you need it, please use an Image, not Better Image.");
  65. this.m_Sprite = base.serializedObject.FindProperty("m_Sprite");
  66. this.m_Type = base.serializedObject.FindProperty("m_Type");
  67. this.m_FillCenter = base.serializedObject.FindProperty("m_FillCenter");
  68. this.m_FillMethod = base.serializedObject.FindProperty("m_FillMethod");
  69. this.m_FillOrigin = base.serializedObject.FindProperty("m_FillOrigin");
  70. this.m_FillClockwise = base.serializedObject.FindProperty("m_FillClockwise");
  71. this.m_FillAmount = base.serializedObject.FindProperty("m_FillAmount");
  72. this.m_PreserveAspect = base.serializedObject.FindProperty("m_PreserveAspect");
  73. this.m_useSpriteMesh = base.serializedObject.FindProperty("m_UseSpriteMesh");
  74. this.m_pixelPerUnitMultiplyer = base.serializedObject.FindProperty("m_PixelsPerUnitMultiplier");
  75. this.m_maskable = base.serializedObject.FindProperty("m_Maskable");
  76. this.spriteSetingsFallbackProp = base.serializedObject.FindProperty("fallbackSpriteSettings");
  77. this.spriteSetingsCollectionProp = base.serializedObject.FindProperty("customSpriteSettings");
  78. this.m_ShowType = new AnimBool(this.m_Sprite.objectReferenceValue != null);
  79. this.m_ShowType.valueChanged.AddListener(new UnityAction(this.Repaint));
  80. Image.Type mType = (Image.Type)this.m_Type.enumValueIndex;
  81. this.m_ShowSlicedOrTiled = new AnimBool((this.m_Type.hasMultipleDifferentValues ? false : mType == Image.Type.Sliced));
  82. this.m_ShowSliced = new AnimBool((this.m_Type.hasMultipleDifferentValues ? false : mType == Image.Type.Sliced));
  83. this.m_ShowFilled = new AnimBool((this.m_Type.hasMultipleDifferentValues ? false : mType == Image.Type.Filled));
  84. this.m_ShowSlicedOrTiled.valueChanged.AddListener(new UnityAction(this.Repaint));
  85. this.m_ShowSliced.valueChanged.AddListener(new UnityAction(this.Repaint));
  86. this.m_ShowFilled.valueChanged.AddListener(new UnityAction(this.Repaint));
  87. this.SetShowNativeSize(true);
  88. }
  89. private void SetShowNativeSize(bool instant)
  90. {
  91. Image.Type mType = (Image.Type)this.m_Type.enumValueIndex;
  92. base.SetShowNativeSize((mType == Image.Type.Simple ? true : mType == Image.Type.Filled), instant);
  93. }
  94. public override void OnInspectorGUI()
  95. {
  96. base.serializedObject.Update();
  97. ScreenConfigConnectionHelper.DrawGui("Sprite Settings", spriteSetingsCollectionProp, ref spriteSetingsFallbackProp, DrawSpriteSettings);
  98. EditorGUILayout.Separator();
  99. if (image.type == Image.Type.Filled)
  100. {
  101. // materials not (yet) supported for filled images
  102. EditorGUILayout.PropertyField(this.m_Material);
  103. }
  104. else
  105. {
  106. // draw color and material
  107. materialDrawer.DrawMaterialGui(base.m_Material);
  108. }
  109. EditorGUILayout.Separator();
  110. base.RaycastControlsGUI();
  111. this.m_ShowType.target = this.m_Sprite.objectReferenceValue != null;
  112. if (EditorGUILayout.BeginFadeGroup(this.m_ShowType.faded))
  113. {
  114. this.TypeGUI();
  115. }
  116. EditorGUILayout.EndFadeGroup();
  117. this.SetShowNativeSize(false);
  118. if (EditorGUILayout.BeginFadeGroup(this.m_ShowNativeSize.faded))
  119. {
  120. EditorGUI.indentLevel = EditorGUI.indentLevel + 1;
  121. if (m_useSpriteMesh != null)
  122. {
  123. EditorGUI.BeginDisabledGroup(true);
  124. EditorGUILayout.Toggle(m_UseSpriteMeshContent, false, new GUILayoutOption[0]);
  125. EditorGUI.EndDisabledGroup();
  126. }
  127. EditorGUILayout.PropertyField(this.m_PreserveAspect, new GUILayoutOption[0]);
  128. EditorGUI.indentLevel = EditorGUI.indentLevel - 1;
  129. }
  130. EditorGUILayout.EndFadeGroup();
  131. base.NativeSizeButtonGUI();
  132. base.serializedObject.ApplyModifiedProperties();
  133. if (image.type == UnityEngine.UI.Image.Type.Sliced)
  134. {
  135. var prop = serializedObject.FindProperty("keepBorderAspectRatio");
  136. EditorGUILayout.PropertyField(prop);
  137. serializedObject.ApplyModifiedProperties();
  138. }
  139. if(image.type == Image.Type.Sliced || image.type == Image.Type.Tiled)
  140. {
  141. var prop = serializedObject.FindProperty("spriteBorderScaleFallback");
  142. var collection = serializedObject.FindProperty("customBorderScales");
  143. //EditorGUILayout.PropertyField(prop);
  144. ScreenConfigConnectionHelper.DrawSizerGui("Border Scale", collection, ref prop);
  145. serializedObject.ApplyModifiedProperties();
  146. }
  147. if (m_maskable != null)
  148. {
  149. EditorGUILayout.PropertyField(m_maskable);
  150. base.serializedObject.ApplyModifiedProperties();
  151. }
  152. }
  153. private void DrawSpriteSettings(string configName, SerializedProperty property)
  154. {
  155. SerializedProperty spriteProp = property.FindPropertyRelative("Sprite");
  156. SerializedProperty primeColorProp = property.FindPropertyRelative("PrimaryColor");
  157. SpriteGUI(spriteProp);
  158. // coloring not supported for Filled yet.
  159. if (image.type != Image.Type.Filled)
  160. {
  161. SerializedProperty colorModeProp = property.FindPropertyRelative("ColorMode");
  162. SerializedProperty secondColorProp = property.FindPropertyRelative("SecondaryColor");
  163. ImageAppearanceProviderEditorHelper.DrawColorGui(colorModeProp, primeColorProp, secondColorProp);
  164. }
  165. else
  166. {
  167. EditorGUILayout.PropertyField(primeColorProp);
  168. }
  169. }
  170. /// <summary>
  171. /// <para>Custom preview for Image component.</para>
  172. /// </summary>
  173. /// <param name="rect">Rectangle in which to draw the preview.</param>
  174. /// <param name="background">Background image.</param>
  175. public override void OnPreviewGUI(Rect rect, GUIStyle background)
  176. {
  177. if (Event.current.type == EventType.Repaint)
  178. {
  179. Image image = this.target as Image;
  180. if (image == null)
  181. {
  182. return;
  183. }
  184. Sprite sprite = image.sprite;
  185. if (sprite == null)
  186. {
  187. return;
  188. }
  189. Texture2D preview = AssetPreview.GetAssetPreview(sprite);
  190. EditorGUI.DrawTextureTransparent(rect, preview, ScaleMode.ScaleToFit, 1f);
  191. }
  192. }
  193. /// <summary>
  194. /// <para>GUI for showing the Sprite property.</para>
  195. /// </summary>
  196. protected void SpriteGUI(SerializedProperty spriteProp)
  197. {
  198. EditorGUI.BeginChangeCheck();
  199. EditorGUILayout.PropertyField(spriteProp, this.m_SpriteContent, new GUILayoutOption[0]);
  200. if (EditorGUI.EndChangeCheck())
  201. {
  202. Sprite mSprite = spriteProp.objectReferenceValue as Sprite;
  203. if (mSprite)
  204. {
  205. Image.Type mType = (Image.Type)this.m_Type.enumValueIndex;
  206. if (mSprite.border.SqrMagnitude() > 0f)
  207. {
  208. this.m_Type.enumValueIndex = 1;
  209. }
  210. else if (mType == Image.Type.Sliced)
  211. {
  212. this.m_Type.enumValueIndex = 0;
  213. }
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// <para>GUI for showing the image type and associated settings.</para>
  219. /// </summary>
  220. protected void TypeGUI()
  221. {
  222. bool flag;
  223. EditorGUILayout.PropertyField(this.m_Type, this.m_SpriteTypeContent, new GUILayoutOption[0]);
  224. EditorGUI.indentLevel = EditorGUI.indentLevel + 1;
  225. Image.Type mType = (Image.Type)this.m_Type.enumValueIndex;
  226. if (this.m_Type.hasMultipleDifferentValues)
  227. {
  228. flag = false;
  229. }
  230. else
  231. {
  232. flag = (mType == Image.Type.Sliced ? true : mType == Image.Type.Tiled);
  233. }
  234. bool flag1 = flag;
  235. if (flag1 && (int)base.targets.Length > 1)
  236. {
  237. flag1 = (
  238. from obj in (IEnumerable<UnityEngine.Object>)base.targets
  239. select obj as Image).All<Image>((Image img) => img.hasBorder);
  240. }
  241. this.m_ShowSlicedOrTiled.target = flag1;
  242. this.m_ShowSliced.target = (!flag1 || this.m_Type.hasMultipleDifferentValues ? false : mType == Image.Type.Sliced);
  243. this.m_ShowFilled.target = (this.m_Type.hasMultipleDifferentValues ? false : mType == Image.Type.Filled);
  244. Image image = this.target as Image;
  245. if (EditorGUILayout.BeginFadeGroup(this.m_ShowSlicedOrTiled.faded) || image.hasBorder)
  246. {
  247. if (mType == Image.Type.Sliced)
  248. {
  249. EditorGUILayout.PropertyField(this.m_FillCenter, new GUILayoutOption[0]);
  250. }
  251. if (m_pixelPerUnitMultiplyer != null)
  252. {
  253. EditorGUILayout.PropertyField(this.m_pixelPerUnitMultiplyer, new GUILayoutOption[0]);
  254. }
  255. }
  256. EditorGUILayout.EndFadeGroup();
  257. if (EditorGUILayout.BeginFadeGroup(this.m_ShowSliced.faded) && image.sprite != null && !image.hasBorder)
  258. {
  259. EditorGUILayout.HelpBox("This Image doesn't have a border.", MessageType.Warning);
  260. }
  261. EditorGUILayout.EndFadeGroup();
  262. if (EditorGUILayout.BeginFadeGroup(this.m_ShowFilled.faded))
  263. {
  264. EditorGUI.BeginChangeCheck();
  265. EditorGUILayout.PropertyField(this.m_FillMethod, new GUILayoutOption[0]);
  266. if (EditorGUI.EndChangeCheck())
  267. {
  268. this.m_FillOrigin.intValue = 0;
  269. }
  270. switch (this.m_FillMethod.enumValueIndex)
  271. {
  272. case 0:
  273. {
  274. this.m_FillOrigin.intValue = (int)((Image.OriginHorizontal)EditorGUILayout.EnumPopup("Fill Origin", (Image.OriginHorizontal)this.m_FillOrigin.intValue, new GUILayoutOption[0]));
  275. break;
  276. }
  277. case 1:
  278. {
  279. this.m_FillOrigin.intValue = (int)((Image.OriginVertical)EditorGUILayout.EnumPopup("Fill Origin", (Image.OriginVertical)this.m_FillOrigin.intValue, new GUILayoutOption[0]));
  280. break;
  281. }
  282. case 2:
  283. {
  284. this.m_FillOrigin.intValue = (int)((Image.Origin90)EditorGUILayout.EnumPopup("Fill Origin", (Image.Origin90)this.m_FillOrigin.intValue, new GUILayoutOption[0]));
  285. break;
  286. }
  287. case 3:
  288. {
  289. this.m_FillOrigin.intValue = (int)((Image.Origin180)EditorGUILayout.EnumPopup("Fill Origin", (Image.Origin180)this.m_FillOrigin.intValue, new GUILayoutOption[0]));
  290. break;
  291. }
  292. case 4:
  293. {
  294. this.m_FillOrigin.intValue = (int)((Image.Origin360)EditorGUILayout.EnumPopup("Fill Origin", (Image.Origin360)this.m_FillOrigin.intValue, new GUILayoutOption[0]));
  295. break;
  296. }
  297. }
  298. EditorGUILayout.PropertyField(this.m_FillAmount, new GUILayoutOption[0]);
  299. if (this.m_FillMethod.enumValueIndex > 1)
  300. {
  301. EditorGUILayout.PropertyField(this.m_FillClockwise, this.m_ClockwiseContent, new GUILayoutOption[0]);
  302. }
  303. }
  304. EditorGUILayout.EndFadeGroup();
  305. EditorGUI.indentLevel = EditorGUI.indentLevel - 1;
  306. }
  307. [MenuItem("CONTEXT/Image/♠ Make Better")]
  308. public static void MakeBetter(MenuCommand command)
  309. {
  310. Image img = command.context as Image;
  311. var newImg = Betterizer.MakeBetter<Image, BetterImage>(img);
  312. var sprite = img.sprite;
  313. var col = img.color;
  314. if(newImg != null)
  315. {
  316. newImg.SetAllDirty();
  317. BetterImage better = newImg as BetterImage;
  318. if(better != null)
  319. {
  320. // set border scale both to height as default to make default scale uniform.
  321. better.SpriteBorderScale.ModX.SizeModifiers[0].Mode = ImpactMode.PixelHeight;
  322. better.SpriteBorderScale.ModY.SizeModifiers[0].Mode = ImpactMode.PixelHeight;
  323. better.CurrentSpriteSettings.Sprite = sprite;
  324. better.CurrentSpriteSettings.PrimaryColor = col;
  325. #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
  326. // ensure shader channels in canvas
  327. Canvas canvas = better.transform.GetComponentInParent<Canvas>();
  328. canvas.additionalShaderChannels = canvas.additionalShaderChannels
  329. | AdditionalCanvasShaderChannels.TexCoord1
  330. | AdditionalCanvasShaderChannels.Tangent;
  331. #endif
  332. }
  333. }
  334. }
  335. }
  336. }