Skip to content

Instantly share code, notes, and snippets.

@PopupAsylumUK
PopupAsylumUK / ImageByDefault.cs
Created April 10, 2023 16:06
Replaces the SpriteRenderer with an Image component when a Sprite asset is dragged under a Canvas
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Replaces the SpriteRenderer with an Image component when a Sprite asset is dragged under a Canvas
/// </summary>
[InitializeOnLoad]
public static class ImageByDefault
@PopupAsylumUK
PopupAsylumUK / Time.cs
Last active January 15, 2019 17:03
Class wraps the functionality of UnityEngine.Time, and provides a static bool to unscale Time.deltaTime. This was used to work around an asset store plugin that had several references to Time.deltaTime, and which I needed to work with Time.timeScale = 0
public static class Time {
public static bool unscaleDeltaTime;
// The time this frame has started (RO). This is the time in seconds since the start of the game.
public static float time { get { return UnityEngine.Time.time; } }
// The time this frame has started (RO). This is the time in seconds since the last level has been loaded.
public static float timeSinceLevelLoad { get { return UnityEngine.Time.timeSinceLevelLoad; } }
@PopupAsylumUK
PopupAsylumUK / Time.cs
Created January 15, 2019 17:01
Class wraps the functionality of UnityEngine.Time, and provides a static bool to unscale Time.deltaTime
public static class Time {
public static bool unscaleDeltaTime;
// The time this frame has started (RO). This is the time in seconds since the start of the game.
public static float time { get { return UnityEngine.Time.time; } }
// The time this frame has started (RO). This is the time in seconds since the last level has been loaded.
public static float timeSinceLevelLoad { get { return UnityEngine.Time.timeSinceLevelLoad; } }
@PopupAsylumUK
PopupAsylumUK / HexGridLayout.cs
Last active May 3, 2024 02:04
A Layout group for arranging children in a hexagon grid, and a Hexagon graphic
using UnityEngine;
using UnityEngine.UI;
public class HexGridLayout : LayoutGroup {
const float SQUARE_ROOT_OF_3 = 1.73205f;
public enum Axis { Horizontal = 0, Vertical = 1 }
public enum Constraint { Flexible = 0, FixedColumnCount = 1, FixedRowCount = 2 }
@PopupAsylumUK
PopupAsylumUK / ActionExtensions.cs
Created May 2, 2018 15:55
Adds null checking invocations to Actions
using System;
public static class ActionExtensions {
public static void SafeInvoke(this Action action) {
if (action != null) {
action();
}
}
@PopupAsylumUK
PopupAsylumUK / PlayAutomatically.cs
Created February 13, 2018 09:53
Plays an animation on an Animator automatically
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
public class PlayAutomatically : MonoBehaviour {
[SerializeField]
AnimationClip modelAnimationClip;
PlayableGraph playableGraph;
@PopupAsylumUK
PopupAsylumUK / FloatRemapExtension.cs
Created January 24, 2018 09:36
Extension function for floats to remap it from one range to another
public static class FloatRemapExtension {
public static float Remap(this float value, float min, float max, float fromMin = 0, float fromMax = 1) {
return ((value - fromMin) / (fromMax - fromMin)) * (max - min) + min;
}
}
@PopupAsylumUK
PopupAsylumUK / OnDestroyCallback.cs
Created November 8, 2017 13:14
A monobehaviour and extension function combination to receive a callback when a gameobject is destroyed
using System;
using UnityEngine;
public class OnDestroyCallback : MonoBehaviour {
Action onDestroy;
public static void AddOnDestroyCallback(GameObject gameObject, Action callback) {
OnDestroyCallback onDestroyCallback = gameObject.GetComponent<OnDestroyCallback>();
if (!onDestroyCallback) {
@PopupAsylumUK
PopupAsylumUK / CopyTransformsPositions.cs
Created June 13, 2017 21:43
An editor window for copying the local positions and rotations from 1 set of transforms to another, used in BeeBeeQ for copying a hand pose from left to right
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class CopyTransformPositions : EditorWindow {
public Transform sourceRoot;
public Transform targetRoot;
@PopupAsylumUK
PopupAsylumUK / AutoNameAnimations.cs
Last active January 3, 2024 22:49
Automatically renames animations on import from "Take 001" or "mixamo.com" to the name of their import, which I find more useful
using UnityEditor;
using UnityEngine;
public class AutoNameAnimations : AssetPostprocessor {
static readonly string[] renameMatches = new string[] { "Take 001", "mixamo.com" };
void OnPostprocessModel(GameObject gameObject) {
ModelImporter importer = assetImporter as ModelImporter;