Skip to content

Instantly share code, notes, and snippets.

@plamentotev
Created February 17, 2024 17:35
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 plamentotev/5141d7d31193c40037648e5bd2111b7d to your computer and use it in GitHub Desktop.
Save plamentotev/5141d7d31193c40037648e5bd2111b7d to your computer and use it in GitHub Desktop.
Simple example that shows how to display an editor window on Unity startup.
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
// The static constructor is called before the class is used,
// but there is no guarantee when this would happen.
// InitializeOnLoad ensures that it is called when Unity loads
// or when the scripts are reloading.
[InitializeOnLoad]
public class WelcomeWindow : EditorWindow
{
private const string AlreadyShown = "com.example.welcome_window_shown";
static WelcomeWindow()
{
// The static constructor is called too early
// in Unity lifecycle and you are limited what you can do inside it.
// All delayed calls are invoked only once after all inspectors update,
// so it should be safe to use to display our editor window.
EditorApplication.delayCall += ShowOnStartup;
}
private static void ShowOnStartup()
{
// InitializeOnLoad classes are called not only on unity startup,
// but also when the scripts are reloading.
// This means the windows could be shown even after it is closed.
// Keep the state whether the window is shown in session variable.
// Session state is reset after Unity is closed.
var isAlreadyShown = SessionState.GetBool(AlreadyShown, false);
if (!isAlreadyShown)
{
ShowWelcomeWindow();
SessionState.SetBool(AlreadyShown, true);
}
}
// The rest is the code for the custom editor window.
[MenuItem("Window/My Package/Welcome")]
public static void ShowWelcomeWindow()
{
var editor = GetWindow<WelcomeWindow>();
editor.titleContent = new GUIContent("Welcome");
}
public void CreateGUI()
{
var label = new Label("Hello Word!");
rootVisualElement.Add(label);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment