Skip to content

Instantly share code, notes, and snippets.

@yagero
yagero / ScriptExecutionOrder.cs
Last active November 3, 2022 13:28
Control Unity's Script Execution Order by code
using UnityEngine;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class ScriptExecutionOrder : Attribute
{
public int order;
public ScriptExecutionOrder(int order) { this.order = order; }
@yagero
yagero / FogProperties.cs
Created November 27, 2017 22:14
Simple struct to save and restore Unity's fog properties
using UnityEngine;
public struct FogProperties
{
public float EndDistance;
public float StartDistance;
public float Density;
public Color Color;
public FogMode Mode;
public bool Enabled;
@yagero
yagero / InstantiatePostProcessingProfile.cs
Created November 21, 2017 13:17
Use this class (instead of a PostProcessingBehaviour) to dynamically instantiate the PostProcessingProfile at runtime: this allows you to modify the properties of the profile during playtime, without modifying the profile asset
using UnityEngine;
using UnityEngine.PostProcessing;
using System.Collections.Generic;
/// <summary>
/// Use this component to dynamically create a PostProcessingBehaviour and instantiate a PostProcessingProfile on a Camera
/// This allows you to dynamically modify at runtime the PostProcessingProfile, without modifying the asset.
/// This component keeps track of the Profile and Instances. This means that if 2 different camera use the same Profile, they will use the same Instance.
/// </summary>
[RequireComponent(typeof(Camera))]
@yagero
yagero / EditorGUILayoutExtensions.cs
Last active February 16, 2021 19:50
EditorGUILayout Fields which properly handles multi-object editing
using System;
using UnityEngine;
using UnityEditor;
public static class EditorGUILayoutExtensions
{
/// <summary>
/// Add a EditorGUILayout.ToggleLeft which properly handles multi-object editing
/// </summary>
public static void ToggleLeft(this SerializedProperty prop, GUIContent label, params GUILayoutOption[] options)
@yagero
yagero / EventSystemManaged.cs
Created November 7, 2017 10:14
EventSystemManaged
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class EventSystemManaged : EventSystem
{
static Stack<EventSystem> ms_PrevEventSystem = new Stack<EventSystem>();
static bool ms_Forced = false;
protected override void OnEnable()
@yagero
yagero / ShaderRenderState.cs
Last active February 13, 2022 15:20
Helpful extension methods to customize Unity's shaders render state from script
public static class ShaderRenderState
{
public enum ZWrite //couldn't find any similar enum in UnityEngine.Rendering
{
Off = 0,
On = 1
}
public static void SetStencilRef(this Material mat, int value) { mat.SetInt("_StencilRef", value); }
public static void SetStencilComp(this Material mat, UnityEngine.Rendering.CompareFunction value) { mat.SetInt("_StencilComp", (int)value); }
@yagero
yagero / DoesSceneExist.cs
Created September 26, 2017 12:21
Unity: How to know if a Scene exists and can be loaded?
using UnityEngine.SceneManagement;
public static class UtilsScene
{
/// <summary>
/// Returns true if the scene 'name' exists and is in your Build settings, false otherwise
/// </summary>
public static bool DoesSceneExist(string name)
{
if (string.IsNullOrEmpty(name))
@yagero
yagero / ButtonSmartNav.cs
Last active March 18, 2024 06:39
Unity UI ButtonSmartNav
using UnityEngine.UI;
// Use this class instead of Unity's default Button
// to automatically skip disabled and inactive Buttons
// if you define your navigation as explicit
public class ButtonSmartNav : Button
{
bool CanReachSelectable(Selectable select)
{
return !select || (select.interactable && select.gameObject.activeInHierarchy);
@yagero
yagero / UtilsEnum.cs
Created August 28, 2017 09:48
Enum.HasFlag in .NET 2 / Unity 5
public static class UtilsEnum
{
public static bool HasFlag(this Enum mask, Enum flags) // Same behavior than Enum.HasFlag is .NET 4
{
#if DEBUG
if (mask.GetType() != flags.GetType())
throw new System.ArgumentException(
string.Format("The argument type, '{0}', is not the same as the enum type '{1}'.",
flags.GetType(), mask.GetType()));
#endif
@yagero
yagero / ScriptableObjectSingleton.cs
Last active November 14, 2017 13:54
Unity: Make your unique data accessible globally w/o having to link it, by using a 'ScriptableObjectSingleton'
using UnityEngine;
public class ScriptableObjectSingleton<T> : ScriptableObject where T : ScriptableObject
{
static T m_Instance = null;
public static T Instance
{
get
{
if (m_Instance == null)