Skip to content

Instantly share code, notes, and snippets.

@winkels
winkels / RefactorTemplate.cs
Last active June 26, 2017 00:15
Set of Unity editor classes that provide a template for programatically refactoring prefabs. As a trivial example, this version changes all colliders to be triggers. Customize it to your needs by changing the code in RefactorTemplate.Refactor() (i.e. the component type you are searching for and what is done those components).
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RefactorTemplate : MonoBehaviour
{
[MenuItem("Refactor/Refactor")]
static void Refactor()
{
@winkels
winkels / InterfaceExtensions.cs
Created January 19, 2015 18:52
Extension methods to get Unity MonoBehaviours (or, in some unlikely scenario, Components) that implement a given C# interface.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class InterfaceExtensions
{
#region GameObject methods
public static T GetInterface<T>(this GameObject gameObject) where T : class
{
@winkels
winkels / FindDependentAssets.cs
Created November 17, 2014 05:20
Unity editor script to find all assets that depend on the currently selected asset (i.e. assets that have serialized references to the selected asset). NOTE: Currently only works on OS X.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class FindDependentAssets
{
[MenuItem("Assets/Find Dependent Assets")]
public static void FindAssets()
@winkels
winkels / CoroutineTimer.cs
Last active May 8, 2017 12:03
Unity script that creates a coroutine-based countdown timer. See http://www.asteroidbase.com/?p=853 for context.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class CoroutineTimer
{
//public fields
public float TimerDuration, TimerRandomScaleFactor, TimerStartDelay;
public bool Repeats;
@winkels
winkels / TimeScaleIndependentParticleSystem.cs
Last active March 25, 2018 10:46
Unity script that allows for particle motion while Time.timeScale = 0. See http://www.asteroidbase.com/?p=569 for context.
using UnityEngine;
using System.Collections;
public class TimeScaleIndependentParticleSystem : TimeScaleIndependentUpdate
{
protected override void Update()
{
base.Update();
particleSystem.Simulate(deltaTime, true, false);
@winkels
winkels / TimeScaleIndependentAnimation.cs
Last active March 25, 2018 10:45
Unity script that allows for animation while Time.timeScale = 0. See http://www.asteroidbase.com/?p=569 for context.
using UnityEngine;
using System.Collections;
public class TimeScaleIndependentAnimation : TimeScaleIndependentUpdate
{
//inspector fields
public bool playOnStart;
public string playOnStartStateName;
//private fields
@winkels
winkels / TimeScaleIndependentUpdate.cs
Last active December 21, 2022 12:10
Unity script that allows for updates while Time.timeScale = 0. See http://www.asteroidbase.com/?p=569 for context.
using UnityEngine;
using System.Collections;
public class TimeScaleIndependentUpdate : MonoBehaviour
{
//inspector fields
public bool pauseWhenGameIsPaused = true;
//private fields
float previousTimeSinceStartup;