Skip to content

Instantly share code, notes, and snippets.

@nukadelic
Last active September 17, 2020 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nukadelic/684dea9610830a156bfcc1a32906765d to your computer and use it in GitHub Desktop.
Save nukadelic/684dea9610830a156bfcc1a32906765d to your computer and use it in GitHub Desktop.
EditorSceneGUIOverlay
Example Result .
.

Usage example:

using UnityEngine;

public class MyMonoClass : MonoBehaviour
{
    public void Call_a_func()
    {
        Debug.Log("Exec");
    }
}
 
#if UNITY_EDITOR

namespace UnityEditorScope
{
    using UnityEditor;

    [CustomEditor( typeof( MyMonoClass ) )]
    class GrassSpawnerEditor : EditorSceneGUIOverlay
    {
        void OnSceneGUI( ) => SceneGUIOverlay();
    
        public override void Draw( )
        {
            float val_min = 2f;
            float val_max = 5f;

            EditorGUILayout.MinMaxSlider( ref val_min, ref val_max, 0, 10f );
            GUILayout.Label("Label: ");
            EditorGUILayout.FloatField( 0.2f );

            if( GUILayout.Button("Exec") )
            {
                ( ( MyMonoClass ) target ).Call_a_func();
            }
        }
    }

}

#endif
#if UNITY_EDITOR
using System.Reflection;
using UnityEditor;
using System.Linq;
using UnityEngine;
public class EditorSceneGUIOverlay : Editor
{
public virtual void Draw() {}
public string overlay_title = "Title String";
public int overlay_padding = 10;
public int overlay_width = 200;
#region EditorSceneGUIOverlay
public void SceneGUIOverlay()
{
if( ! ( ( MonoBehaviour ) target ).enabled ) return;
Handles.BeginGUI();
using( new GUILayout.HorizontalScope( GUILayout.Width( overlay_width + overlay_padding ) ) )
{
GUILayout.Space( overlay_padding );
using( new GUILayout.VerticalScope() )
{
GUILayout.FlexibleSpace();
using( new GUILayout.VerticalScope( GUI.skin.window ) )
{
GUILayout.Space( 5f );
Draw();
GUILayout.Space( 5f );
}
var rect = GUILayoutUtility.GetLastRect();
//GUI.Button( rect, "" );
var titleContent = new GUIContent( overlay_title );
rect.y += 2f;
rect.x += 3f;
rect.width -= 6f;
//rect.x += ( overlay_width - 7f * overlay_title.Length ) / 2f;
rect.height = EditorGUIUtility.singleLineHeight * 1.2f;
GUI.Label( rect, titleContent, GUI.skin.box );
GUILayout.Space( 20f + overlay_padding );
}
}
Handles.EndGUI();
}
#endregion
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment