Skip to content

Instantly share code, notes, and snippets.

@mob-sakai
Last active September 13, 2024 17:20
Show Gist options
  • Save mob-sakai/7bd642398c16b9d2847817e496280f84 to your computer and use it in GitHub Desktop.
Save mob-sakai/7bd642398c16b9d2847817e496280f84 to your computer and use it in GitHub Desktop.
Unity Internal Feature Samples

Unity Internal Features

The WindowAction attribute allows you to add global actions to the windows (items in the generic menu or extra buttons).

The EditorHeaderItem attribute allows you to add extra buttons to the editor in inspector.

using System;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
public static class EditorHeaderItemImplementationTest
{
// An example of implementing EditorHeaderItem.
// The EditorHeaderItem attribute allows you to add extra buttons to the editor in inspector.
[EditorHeaderItem(typeof(Transform))]
private static bool TransformEditorHeaderItem(Rect pos, Object[] targets)
{
// Skip if the target is not Transform.
if (targets.Length == 0 || !(targets[0] is Transform)) return false;
var icon = EditorGUIUtility.FindTexture("refresh");
if (GUI.Button(pos, icon, "IconButton"))
{
Array.ForEach(targets, x => Debug.Log($"{x} Do something..."));
}
// If you draw UI, return true.
return true;
}
}
{
"name": "Unity.InternalAPIEditorBridge.001",
"rootNamespace": "",
"references": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public static class WindowActionImplementationTest
{
// An example of implementing WindowAction.
// The WindowAction attribute allows you to add global actions to the windows (items in the generic menu or extra buttons).
[WindowAction]
private static WindowAction CreateWindowAction()
{
var action = WindowAction.CreateWindowMenuItem("WindowActionTest", null, null);
// Show only in the Inspector window
action.validateHandler = (window, windowAction) => window is InspectorWindow;
// Add a menu to the Editor
action.menuPath = "Test/WindowActionTest";
// Icon specification
action.width = 17f;
action.icon = EditorGUIUtility.FindTexture("record on");
// (Optional) If you create a GUI callback, return true to execute executeHandler
action.drawHandler = (window, windowAction, pos) =>
GUI.Button(pos, EditorGUIUtility.IconContent("record on"), "IconButton");
// Execute when the icon or menu is clicked
action.executeHandler = (window, windowAction) => Debug.Log($"{window}: Clicked!");
// You can pass a custom object to the callback
action.userData = new HashSet<string>();
// Priority (The larger the value, the more left it is placed)
action.priority = 1000;
return action;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment