Skip to content

Instantly share code, notes, and snippets.

@yagero
yagero / ScriptExecutionOrder.cs
Last active Apr 10, 2020
Control Unity's Script Execution Order by code
View ScriptExecutionOrder.cs
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 Nov 27, 2017
Simple struct to save and restore Unity's fog properties
View FogProperties.cs
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 Nov 21, 2017
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
View InstantiatePostProcessingProfile.cs
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 Feb 16, 2021
EditorGUILayout Fields which properly handles multi-object editing
View EditorGUILayoutExtensions.cs
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)
View EventSystemManaged.cs
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 Jan 10, 2021
Helpful extension methods to customize Unity's shaders render state from script
View ShaderRenderState.cs
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 Sep 26, 2017
Unity: How to know if a Scene exists and can be loaded?
View DoesSceneExist.cs
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 Jun 9, 2018
Unity UI ButtonSmartNav
View ButtonSmartNav.cs
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 Aug 28, 2017
Enum.HasFlag in .NET 2 / Unity 5
View UtilsEnum.cs
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 Nov 14, 2017
Unity: Make your unique data accessible globally w/o having to link it, by using a 'ScriptableObjectSingleton'
View ScriptableObjectSingleton.cs
using UnityEngine;
public class ScriptableObjectSingleton<T> : ScriptableObject where T : ScriptableObject
{
static T m_Instance = null;
public static T Instance
{
get
{
if (m_Instance == null)