Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active November 28, 2025 13:10
Show Gist options
  • Select an option

  • Save unitycoder/5106632ec06780ad0e0b61229f757a7d to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/5106632ec06780ad0e0b61229f757a7d to your computer and use it in GitHub Desktop.
Add ping button for scriptable object Inspector
// NOTE: this executes on all selected objects.. and compares if target is ScriptableObject
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public static class ScriptableObjectHeaderPing
{
static ScriptableObjectHeaderPing()
{
// Called after Unity draws the default header of *any* inspector
Editor.finishedDefaultHeaderGUI += OnPostHeaderGUI;
}
private static void OnPostHeaderGUI(Editor editor)
{
// Only for ScriptableObjects
if (!(editor.target is ScriptableObject))
return;
// Draw a little row right under the default header (type name + Open button)
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace(); // push to right side
if (GUILayout.Button("Ping", EditorStyles.miniButton))
{
Object target = editor.target;
if (target != null)
{
Selection.activeObject = target;
EditorGUIUtility.PingObject(target);
}
}
}
}
}
// NOTE: this is custom editor for ScriptableObjects ONLY (and add button in preview gui)
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(ScriptableObject), true)] // useForChildren = true
public class ScriptableObjectNamePreview : Editor
{
// Tell Unity that this object has a preview GUI
public override bool HasPreviewGUI()
{
return true;
}
// Optional: title text shown above the preview area
public override GUIContent GetPreviewTitle()
{
return new GUIContent(target != null ? target.name : "ScriptableObject");
}
// Small toolbar above the preview (where built-in preview buttons usually go)
public override void OnPreviewSettings()
{
base.OnPreviewSettings();
if (GUILayout.Button("Ping", EditorStyles.miniButton))
{
if (target != null)
{
// Select and ping this object (in Project window or Hierarchy if applicable)
Selection.activeObject = target;
EditorGUIUtility.PingObject(target);
}
}
}
// Draw the preview area (bottom of the inspector)
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
if (target == null)
return;
// Draw default background
if (Event.current.type == EventType.Repaint && background != null)
{
background.Draw(r, GUIContent.none, false, false, false, false);
}
// Center the name text in the preview rect
var labelStyle = new GUIStyle(EditorStyles.boldLabel)
{
alignment = TextAnchor.MiddleCenter,
wordWrap = true,
fontSize = 14
};
GUI.Label(r, target.name, labelStyle);
}
}
// NOTE most lightweight?
// Assets/Editor/ScriptableObjectPingEditor.cs
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(ScriptableObject), true)] // useForChildren = true
public class ScriptableObjectPingEditor : Editor
{
protected override void OnHeaderGUI()
{
// Default header (icon, name, Open button)
base.OnHeaderGUI();
// Extra row with Ping button
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();
if (GUILayout.Button("Ping", EditorStyles.miniButton))
{
if (target != null)
{
Selection.activeObject = target;
EditorGUIUtility.PingObject(target);
}
}
}
}
// Use default inspector; no preview overrides
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment