Skip to content

Instantly share code, notes, and snippets.

View keenanwoodall's full-sized avatar
🛠️
Making stuff!

Keenan Woodall keenanwoodall

🛠️
Making stuff!
View GitHub Profile
@keenanwoodall
keenanwoodall / NativeCurve
Last active October 1, 2024 22:05
A struct that makes using animation curves with the job system easy.
using System;
using System.Runtime.CompilerServices;
using UnityEngine;
using Unity.Collections;
using static Unity.Mathematics.math;
public struct NativeCurve : IDisposable
{
public bool IsCreated => values.IsCreated;
@keenanwoodall
keenanwoodall / CurveTest
Created December 29, 2018 21:13
Example script showing how to deform a mesh using a NativeCurve.
using UnityEngine;
using Unity.Jobs;
using Unity.Burst;
using Unity.Collections;
public class CurveTest : MonoBehaviour
{
public AnimationCurve curve;
private Mesh mesh;
@keenanwoodall
keenanwoodall / Transform Rotation GUI
Last active March 5, 2019 10:20
A helper class for drawing a transform's rotation field in the same way as the Transform editor in Unity.
using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;
/// <summary>
/// Abstracts the reflection required to use the internal TransformRotationGUI class.
/// </summary>
public class TransformRotationGUI
{
@keenanwoodall
keenanwoodall / CustomFloatField.cs
Last active March 13, 2019 23:59
A helper class that lets you draw a float field with more control. You can define a rect where the value is drawn/set and another rect where you can drag to scroll the value.
using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;
public static class CustomFloatField
{
private static readonly int Hint = "EditorTextField".GetHashCode ();
private static readonly Type EditorGUIType = typeof (EditorGUI);
private static readonly Type RecycledTextEditorType = Assembly.GetAssembly (EditorGUIType).GetType ("UnityEditor.EditorGUI+RecycledTextEditor");
@keenanwoodall
keenanwoodall / Automated Deformation Test
Created August 1, 2019 16:48
A test for deforming meshes without using a deformable component
using UnityEngine;
using UnityEditor;
using Unity.Jobs;
using Deform;
public class AutomatedDeformationTestWindow : EditorWindow
{
private Mesh mesh;
[MenuItem("Tools/Automated Deformation Window")]
@keenanwoodall
keenanwoodall / PrefabUtilityMenuItems.cs
Created August 3, 2019 18:21
Makes creating more than one prefab at once possible
using UnityEngine;
using UnityEditor;
public static class PrefabUtilityMenuItems
{
[MenuItem("Tools/Prefab Utility/Create Prefabs From Selection")]
public static void CreateFromSelection()
{
var selections = Selection.gameObjects;
if (selections == null || selections.Length == 0)

I think using scopes greatly improves readability. Here's an example of converting a larger section of code. The indenting makes it immediately clear when a scopes begins and ends. Both snippets of code draw the exact same controls.

Before:

EditorGUILayout.BeginVertical("box");
disable = EditorGUILayout.Toggle("Disable Sliders", disable);
indent = EditorGUILayout.IntSlider("Indent Sliders", indent, 1, 4);
@keenanwoodall
keenanwoodall / UnityObjectOperatorOverloadTest.cs
Last active November 9, 2019 18:10
Test if the equality operator for anything that derives from Unity.Object is still overridden. Just put the script in your project and press play in any scene to test.
// read this article for more info: https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/
using UnityEngine;
public class UnityObjectOperatorOverloadTest : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod]
public static void Test()
{
var gameObject = new GameObject("UnityEngine.Object Operator Overload Test");
@keenanwoodall
keenanwoodall / BoundedBendDeformer.cs
Created December 10, 2019 17:43
A modified bend deformer that only modifies vertices within a defined bounding box
using UnityEngine;
using Unity.Jobs;
using Unity.Burst;
using Unity.Collections;
using Unity.Mathematics;
using static Unity.Mathematics.math;
using Beans.Unity.Mathematics;
namespace Deform
{
@keenanwoodall
keenanwoodall / Spring.cs
Created May 24, 2021 19:17
C# implementation of Ming-Lun "Allen" Chou's springing functions for Unity (aka my favorite functions)
// Source: http://allenchou.net/2015/04/game-math-precise-control-over-numeric-springing/
public void Spring(ref float x, ref float v, float xt, float zeta, float omega, float h)
{
float f = 1.0f + 2.0f * h * zeta * omega;
float oo = omega * omega;
float hoo = h * oo;
float hhoo = h * hoo;
float detInv = 1.0f / (f + hhoo);
float detX = f * x + h * v + hhoo * xt;
float detV = v + hoo * (xt - x);