Skip to content

Instantly share code, notes, and snippets.

@kraj0t
kraj0t / EditorComponentUtils.cs
Created April 28, 2022 08:11
[Unity] Retrieve a component's fileId and its gameObject's asset guid (if any)
/// <summary>Universal method for retrieving the fileId of a component inside a saved gameObject, and the guid to the file that contains the gameObject.
/// The gameObject could live in a prefab or in a scene, and the component does not need to be at the root gameObject of the prefab.</summary>
/// <param name="comp"></param>
/// <returns>A tuple containing the guid and the fileId of the component. Returns null if the component's gameObject has not been saved to a scene or a prefab.</returns>
public static Tuple<string, long> GetComponentIdentifiers(Component comp)
{
// Check if the component is the direct instance from the prefab.
// In the Editor, this means that the prefab asset is selected and the component is showing in the Inspector.
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(comp, out string guid, out long fileId))
{
@kraj0t
kraj0t / CustomImageEditor.cs
Last active March 27, 2022 15:32
Extend Unity's Image editor with custom inspector GUI
using UnityEditor;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.UI;
[CustomEditor(typeof(Image))]
public class CustomImageEditor : ImageEditor
{
public override void OnInspectorGUI()
{
@kraj0t
kraj0t / CustomParticleSystemEditor.cs
Last active November 9, 2023 16:29
Extend ParticleSystem editor with custom inspector GUI
using System.Reflection;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(ParticleSystem))]
public class CustomParticleSystemEditor : Editor
{
private Editor _builtInEditor;
private void OnEnable()
@kraj0t
kraj0t / OrbitParentTool.cs
Last active February 26, 2022 20:29
OrbitParentTool - a Unity EditorTool to orbit selected objects around their parents (my first test with EditorTools)
using UnityEngine;
using UnityEditor;
using UnityEditor.EditorTools;
[EditorTool("Orbit Parent Tool", typeof(Transform))]
class OrbitParentTool : EditorTool
{
[SerializeField]
private Texture2D _toolIcon;
@kraj0t
kraj0t / .Display keyboard shortcuts.gif
Last active March 26, 2026 10:12
DisplayKeyboardShortcuts - a Unity editor tool that shows the keys and shortcuts that the user is pressing
.Display keyboard shortcuts.gif
@kraj0t
kraj0t / .AnimatedGUI preview.gif
Last active October 19, 2023 14:52
AnimatedGUI - a Unity editor convenience class for drawing temporary GUI with animated fade-in & fade-out
.AnimatedGUI preview.gif
@kraj0t
kraj0t / GUIBackgroundColorScope.cs
Last active February 14, 2022 23:38
GUIBackgroundColorScope - quickly set color of your GUI (buttons, panels...) same as EditorGUI.DisabledScope and others
using System;
using System.Collections.Generic;
using UnityEngine;
public struct GUIBackgroundColorScope : IDisposable
{
private static readonly Stack<Color> _colorStack = new Stack<Color>();
public GUIBackgroundColorScope(Color color)
{
@kraj0t
kraj0t / HasFrameBounds.cs
Last active February 14, 2022 02:26
Unity Editor: undocumented callbacks in Editor class to define bounds for focus
/// <summary>
/// Undocumented Unity callback. More info here:
/// https://forum.unity.com/threads/editor-how-do-i-set-the-bounds-of-an-empty-gameobject-for-focus-f-key.427093/
/// </summary>
/// <returns></returns>
public bool HasFrameBounds()
{
return true;
}
@kraj0t
kraj0t / DrawDottedLines.cs
Last active February 14, 2022 02:26
Editor GUI; draw dotted lines using Handles
// Somewhere in your OnSceneGUI() ...
// Draw dotted lines.
var handlesColorBkp = Handles.color;
Handles.color = new Color(0f, 0f, 0.5f);
for (int i = 0; i < points.Length - 1; i++)
{
Handles.DrawDottedLine(points[i], points[i + 1], DOTTED_LINE_SIZE);
}
@kraj0t
kraj0t / CopyGameScreenshotToClipboard.cs
Created December 12, 2019 01:33
Unity Tool - copy Game window to system clipboard
/*
* @brief Editor menu item and shortcut for capturing the game view into the system clipboard.
*
* @setup The System.Drawing and System.Windows.Forms Mono DLLs need to be referenced. See the documentation for
* CopyTextureToClipboard below.
*
* @usage Access from the menu item or use the shortcut. Both are defined below as attribute of GameViewToClipboard(),
* defaults to Shift+S
*
* @author Aurelio Provedo