Skip to content

Instantly share code, notes, and snippets.

@lucatronica
Last active April 23, 2021 00:30
Show Gist options
  • Save lucatronica/0fdb3b16934de7160cc94f4d2798aec7 to your computer and use it in GitHub Desktop.
Save lucatronica/0fdb3b16934de7160cc94f4d2798aec7 to your computer and use it in GitHub Desktop.

Unity Script Attributes

General

using UnityEngine;

public class MyClass
{
	// [RuntimeInitializeOnLoadMethod]
	// Invokes a static method when the game is loaded at runtime.

	[RuntimeInitializeOnLoadMethod]
	public static void OnLoad()
	{
		Debug.Log("Loaded!");
	}

	[RuntimeInitializeOnLoadMethod(loadType: RuntimeInitializeLoadType.AfterSceneLoad)]
	public static void OnSceneLoad()
	{
		Debug.Log("Scene loaded!");
	}
}

MonoBehaviour Scripts

using System.Collections.Generic;
using UnityEngine;

// [ExecuteAlways]
// Executes MonoBehaviour methods outside of play mode.

[ExecuteAlways]


// [DisallowMultipleComponent]
// Prevents MonoBehaviour of same type (or subtype) to be added more than once to a GameObject.

[DisallowMultipleComponent]


// [RequireComponent]
// When adding this script to an object, the required component will also automatically be added.

[RequireComponent(typeof(Rigidbody))]


// [HelpURLAttribute]
// Provide a custom documentation URL for a class.

[HelpURLAttribute("http://www.google.com")]


// [SelectionBase]
// Make objects with this script the selection target (in the hierarchy of objects).

[SelectionBase]


// [AddComponentMenu]
// Set where the component appears in the "Component" menu and "Add Component" dialog.
// Can use `order` to re-order items.

[AddComponentMenu("MyScripts/Example")]
[AddComponentMenu("MyScripts/Example2", 1)]


public class MyScript : MonoBehaviour
{
	// [Min]
	// Restrict a float or int to a minimum value;

	[Min(0)]
	public float minZero;


	// [Range]
	// Restrict a float or int to a range.
	// Adds a slider to the inspector.

	[Range(0, 1)]
	public float opacity;


	// [Multiline]
	// Present a string field with a multi-line editor.
	// Fixed height and sits to the right of the field label.

	[Multiline]
	public string defaultMultiline;

	
	// [TextArea]
	// Present a string field with a multi-line editor.
	// Sits below the field label, using the full width of the inspector.
	// Can customize height.

	[TextArea]
	public string defaultTextArea;

	[TextArea(minLines: 6, maxLines: 10)]
	public string customTextArea;


	// [InspectorName]
	// Set the display name of an enum value.

	public enum Difficulty
	{
		Easy,
		[InspectorName("Heroic")]
		Medium,
		[InspectorName("Legendary")]
		Hard,
	}

	public Difficulty displayMode = Difficulty.Medium;


	// [ColorUsage]
	// Customize Color inspector.
	// By default alpha=true, hdr=false.

	[ColorUsage(showAlpha: false)]
	public Color colorNoAlpha;

	[ColorUsage(showAlpha: true, hdr: true)]
	public Color colorAlphaAndHDR;


	// [GradientUsage]
	// Customize Gradient inspector.
	// By default hdr=false.

	[GradientUsage(hdr: true)]
	public Gradient gradientHDR;

	
	// [HideInInspector]
	
	[HideInInspector]
	public int thisWillNotShowInTheInspector;


	// [Delayed]
	// Field will only update underlying values once the user presses enter or moves focus.
	// Can use on float, int or string.

	[Delayed]
	public float delayedFloat;


	// [Tooltip]
	// Add a tooltip to a field.

	[Tooltip("Health value between 0 and 100.")]
	public int health = 10;


	// [Header]
	// Add a header before a field in the inspector.

	[Header("Item Settings")]
	public float itemPrice;


	// [Space]
	// Add some vertical spacing before a field in the inspector.

	[Space]
	public int afterDefaultSpace;

	[Space(40)]
	public int afterCustomSpace;


	// [ContextMenu]
	// Add a command to the context menu of a script's instance inspector.
	// Can define a validation function which disables the command by returning false.
	// Can re-order commands using `priority`. 

	[ContextMenu("Look At Origin")]
	void LookAtOrigin()
	{
		transform.LookAt(Vector3.zero);
	}

	[ContextMenu("Look At Origin", isValidateFunction: true)]
	bool ValidateLookAtOrigin()
	{
		return transform.position != Vector3.zero;
	}

	[ContextMenu("Do Something", isValidateFunction: false, priority: 1000)]
	void Something() {}


	// [ContextMenuItem]
	// Add a command to the context menu of a script property's inspector.

	[ContextMenuItem("Randomize", "RandomizeColor")]
	public Color color;

	void RandomizeColor()
	{
		color = Random.ColorHSV();
	}


	// [SerializeField]
	// Force Unity to serialize a private field (only public fields a serialized by default).

	[SerializeField]
	private bool haveVisitedTown;


	// [SerializeReference]
	// Force Unity to serialize a field by reference rather than by value.

	[SerializeReference]
	public List<object> myObjects;
}

ScriptableObject

// menuItem  Where to show the command in the Asset/Create menu.
// fileName  The default file name used by newly created instances of this type.
// order     The position of the item within the given menu.
[CreateAssetMenu(menuName = "Catalog/StoreItem", fileName = "StoreItem", order = 1)]

// Use binary serialization regardless of project's asset serialization mode.
// Useful for types that contain large amounts of data.
[PreferBinarySerialization]

public class MyScriptableObject : ScriptableObject
{
}

Editor Scripts

using UnityEngine;
using UnityEditor;

// Make a custom editor support multi-object editing.
[CanEditMultipleObjects]

// Mark which type it's an editor for.
[CustomEditor(typeof(MyScript))]

public class MyScript : Editor
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment