SoftMaskEditor.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEditor;
  5. using System.Linq;
  6. namespace Coffee.UISoftMask
  7. {
  8. /// <summary>
  9. /// SoftMask editor.
  10. /// </summary>
  11. [CustomEditor(typeof(SoftMask))]
  12. [CanEditMultipleObjects]
  13. public class SoftMaskEditor : Editor
  14. {
  15. private const int k_PreviewSize = 128;
  16. private const string k_PrefsPreview = "SoftMaskEditor_Preview";
  17. private static readonly List<Graphic> s_Graphics = new List<Graphic>();
  18. private static bool s_Preview;
  19. private void OnEnable()
  20. {
  21. s_Preview = EditorPrefs.GetBool(k_PrefsPreview, false);
  22. }
  23. public override void OnInspectorGUI()
  24. {
  25. base.OnInspectorGUI();
  26. var current = target as SoftMask;
  27. current.GetComponentsInChildren<Graphic>(true, s_Graphics);
  28. var fixTargets = s_Graphics
  29. .Where(x => x.gameObject != current.gameObject)
  30. .Where(x => !x.GetComponent<SoftMaskable>() && (!x.GetComponent<Mask>() || x.GetComponent<Mask>().showMaskGraphic))
  31. .ToList();
  32. if (0 < fixTargets.Count)
  33. {
  34. GUILayout.BeginHorizontal();
  35. EditorGUILayout.HelpBox("There are child Graphics that does not have a SoftMaskable component.\nAdd SoftMaskable component to them.", MessageType.Warning);
  36. GUILayout.BeginVertical();
  37. if (GUILayout.Button("Fix"))
  38. {
  39. foreach (var p in fixTargets)
  40. {
  41. p.gameObject.AddComponent<SoftMaskable>();
  42. }
  43. EditorUtils.MarkPrefabDirty();
  44. }
  45. if (GUILayout.Button("Ping"))
  46. {
  47. EditorGUIUtility.PingObject(fixTargets[0]);
  48. }
  49. GUILayout.EndVertical();
  50. GUILayout.EndHorizontal();
  51. }
  52. var currentImage = current.graphic as Image;
  53. if (currentImage && IsMaskUI(currentImage.sprite))
  54. {
  55. GUILayout.BeginHorizontal();
  56. EditorGUILayout.HelpBox("SoftMask does not recommend to use 'UIMask' sprite as a source image.\n(It contains only small alpha pixels.)\nDo you want to use 'UISprite' instead?", MessageType.Warning);
  57. GUILayout.BeginVertical();
  58. if (GUILayout.Button("Fix"))
  59. {
  60. currentImage.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd");
  61. }
  62. GUILayout.EndVertical();
  63. GUILayout.EndHorizontal();
  64. }
  65. // Preview buffer.
  66. GUILayout.BeginVertical(EditorStyles.helpBox);
  67. if (s_Preview != (s_Preview = EditorGUILayout.ToggleLeft("Preview Soft Mask Buffer", s_Preview)))
  68. {
  69. EditorPrefs.SetBool(k_PrefsPreview, s_Preview);
  70. }
  71. if (s_Preview)
  72. {
  73. var tex = current.softMaskBuffer;
  74. var width = tex.width * k_PreviewSize / tex.height;
  75. EditorGUI.DrawPreviewTexture(GUILayoutUtility.GetRect(width, k_PreviewSize), tex, null, ScaleMode.ScaleToFit);
  76. Repaint();
  77. }
  78. GUILayout.EndVertical();
  79. }
  80. private static bool IsMaskUI(Object obj)
  81. {
  82. return obj
  83. && obj.name == "UIMask"
  84. && AssetDatabase.GetAssetPath(obj) == "Resources/unity_builtin_extra";
  85. }
  86. //%%%% Context menu for editor %%%%
  87. [MenuItem("CONTEXT/Mask/Convert To SoftMask", true)]
  88. private static bool _ConvertToSoftMask(MenuCommand command)
  89. {
  90. return EditorUtils.CanConvertTo<SoftMask>(command.context);
  91. }
  92. [MenuItem("CONTEXT/Mask/Convert To SoftMask", false)]
  93. private static void ConvertToSoftMask(MenuCommand command)
  94. {
  95. EditorUtils.ConvertTo<SoftMask>(command.context);
  96. }
  97. [MenuItem("CONTEXT/Mask/Convert To Mask", true)]
  98. private static bool _ConvertToMask(MenuCommand command)
  99. {
  100. return EditorUtils.CanConvertTo<Mask>(command.context);
  101. }
  102. [MenuItem("CONTEXT/Mask/Convert To Mask", false)]
  103. private static void ConvertToMask(MenuCommand command)
  104. {
  105. EditorUtils.ConvertTo<Mask>(command.context);
  106. }
  107. }
  108. }