Skip to content

Instantly share code, notes, and snippets.

@PopupAsylumUK
PopupAsylumUK / ShadowMeshBatcher.cs
Last active April 12, 2017 13:52
A script that finds meshes in the scene that could probably all be rendered as a single shadow caster and creates a moderately optimized mesh or meshes for shadow rendering. The resulting meshes should be cast shadows only, and the meshes they represent/replace should not cast shadows.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class ShadowMeshBatcher : EditorWindow {
[MenuItem("Window/Shadow Mesh Batcher")]
static void Init() {
// Get existing open window or if none, make a new one:
@PopupAsylumUK
PopupAsylumUK / SmoothClip.snippet
Created May 23, 2017 13:01
A Smooth Clipping Function
public static float SmoothClip(float input, float min, float max, float spread, float step) {
float timelinePosition = (input - min) / (max - min) * (1 - spread) + spread;
float signedDistanceToScrubber = timelinePosition - step;
float normalizedDistanceToScrubber = signedDistanceToScrubber / spread;
return Mathf.Clamp01(1 - normalizedDistanceToScrubber);
}
@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;
@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 / 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 / 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 / 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 / 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 / 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 / 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; } }