2018-11-20 07:49:14 +08:00
using System.Collections.Generic ;
using UnityEngine ;
using UnityEngine.UI ;
using UnityEditor ;
2018-12-15 20:42:36 +08:00
using System.Linq ;
2018-11-20 07:49:14 +08:00
2020-05-11 15:34:22 +08:00
namespace Coffee.UISoftMask
2018-11-20 07:49:14 +08:00
{
2020-05-11 15:34:22 +08:00
/// <summary>
/// SoftMask editor.
/// </summary>
[CustomEditor(typeof(SoftMask))]
[CanEditMultipleObjects]
public class SoftMaskEditor : Editor
{
2020-06-09 13:41:36 +08:00
private const int k_PreviewSize = 128 ;
2020-06-04 12:43:17 +08:00
private const string k_PrefsPreview = "SoftMaskEditor_Preview" ;
private static readonly List < Graphic > s_Graphics = new List < Graphic > ( ) ;
private static bool s_Preview ;
2020-05-11 15:34:22 +08:00
private void OnEnable ( )
{
s_Preview = EditorPrefs . GetBool ( k_PrefsPreview , false ) ;
}
public override void OnInspectorGUI ( )
{
base . OnInspectorGUI ( ) ;
var current = target as SoftMask ;
current . GetComponentsInChildren < Graphic > ( true , s_Graphics ) ;
2020-06-09 13:41:36 +08:00
var fixTargets = s_Graphics
. Where ( x = > x . gameObject ! = current . gameObject )
. Where ( x = > ! x . GetComponent < SoftMaskable > ( ) & & ( ! x . GetComponent < Mask > ( ) | | x . GetComponent < Mask > ( ) . showMaskGraphic ) )
. ToList ( ) ;
2020-05-11 15:34:22 +08:00
if ( 0 < fixTargets . Count )
{
GUILayout . BeginHorizontal ( ) ;
2020-06-04 12:43:17 +08:00
EditorGUILayout . HelpBox ( "There are child Graphics that does not have a SoftMaskable component.\nAdd SoftMaskable component to them." , MessageType . Warning ) ;
2020-05-11 15:34:22 +08:00
GUILayout . BeginVertical ( ) ;
if ( GUILayout . Button ( "Fix" ) )
{
foreach ( var p in fixTargets )
{
p . gameObject . AddComponent < SoftMaskable > ( ) ;
}
EditorUtils . MarkPrefabDirty ( ) ;
}
if ( GUILayout . Button ( "Ping" ) )
{
EditorGUIUtility . PingObject ( fixTargets [ 0 ] ) ;
}
GUILayout . EndVertical ( ) ;
GUILayout . EndHorizontal ( ) ;
}
2020-09-08 10:33:57 +08:00
var currentImage = current . graphic as Image ;
if ( currentImage & & IsMaskUI ( currentImage . sprite ) )
{
GUILayout . BeginHorizontal ( ) ;
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 ) ;
GUILayout . BeginVertical ( ) ;
if ( GUILayout . Button ( "Fix" ) )
{
currentImage . sprite = AssetDatabase . GetBuiltinExtraResource < Sprite > ( "UI/Skin/UISprite.psd" ) ;
}
GUILayout . EndVertical ( ) ;
GUILayout . EndHorizontal ( ) ;
}
2020-05-11 15:34:22 +08:00
// Preview buffer.
2020-06-04 12:43:17 +08:00
GUILayout . BeginVertical ( EditorStyles . helpBox ) ;
2020-06-09 13:41:36 +08:00
if ( s_Preview ! = ( s_Preview = EditorGUILayout . ToggleLeft ( "Preview Soft Mask Buffer" , s_Preview ) ) )
2020-05-11 15:34:22 +08:00
{
EditorPrefs . SetBool ( k_PrefsPreview , s_Preview ) ;
}
if ( s_Preview )
{
var tex = current . softMaskBuffer ;
2020-06-09 13:41:36 +08:00
var width = tex . width * k_PreviewSize / tex . height ;
EditorGUI . DrawPreviewTexture ( GUILayoutUtility . GetRect ( width , k_PreviewSize ) , tex , null , ScaleMode . ScaleToFit ) ;
2020-05-11 15:34:22 +08:00
Repaint ( ) ;
}
2020-06-09 13:41:36 +08:00
2020-06-04 12:43:17 +08:00
GUILayout . EndVertical ( ) ;
2020-05-11 15:34:22 +08:00
}
2020-09-08 10:33:57 +08:00
private static bool IsMaskUI ( Object obj )
{
return obj
& & obj . name = = "UIMask"
& & AssetDatabase . GetAssetPath ( obj ) = = "Resources/unity_builtin_extra" ;
}
2020-05-11 15:34:22 +08:00
//%%%% Context menu for editor %%%%
[MenuItem("CONTEXT/Mask/Convert To SoftMask", true)]
2020-06-04 12:43:17 +08:00
private static bool _ConvertToSoftMask ( MenuCommand command )
2020-05-11 15:34:22 +08:00
{
return EditorUtils . CanConvertTo < SoftMask > ( command . context ) ;
}
[MenuItem("CONTEXT/Mask/Convert To SoftMask", false)]
2020-06-04 12:43:17 +08:00
private static void ConvertToSoftMask ( MenuCommand command )
2020-05-11 15:34:22 +08:00
{
EditorUtils . ConvertTo < SoftMask > ( command . context ) ;
}
[MenuItem("CONTEXT/Mask/Convert To Mask", true)]
2020-06-04 12:43:17 +08:00
private static bool _ConvertToMask ( MenuCommand command )
2020-05-11 15:34:22 +08:00
{
return EditorUtils . CanConvertTo < Mask > ( command . context ) ;
}
[MenuItem("CONTEXT/Mask/Convert To Mask", false)]
2020-06-04 12:43:17 +08:00
private static void ConvertToMask ( MenuCommand command )
2020-05-11 15:34:22 +08:00
{
EditorUtils . ConvertTo < Mask > ( command . context ) ;
}
}
}