Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Stevenson mstevenson

View GitHub Profile
@mstevenson
mstevenson / SaveFBX.cs
Created Aug 5, 2013
ASCII FBX file exporter for Unity. Extracted from Michal Mandrysz's Unity Summer of Code 2009 external lightmapping project.
View SaveFBX.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
/*
View ies2cycles.py
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@mstevenson
mstevenson / Fps.cs
Created Mar 6, 2013
An accurate FPS counter for Unity. Works in builds.
View Fps.cs
using UnityEngine;
using System.Collections;
public class Fps : MonoBehaviour {
string label = "";
float count;
IEnumerator Start ()
{
@mstevenson
mstevenson / ConfigurableJointExtensions.cs
Last active Sep 15, 2021
Unity extension methods for computing a ConfigurableJoint.TargetRotation value from a given local or world rotation.
View ConfigurableJointExtensions.cs
using UnityEngine;
public static class ConfigurableJointExtensions {
/// <summary>
/// Sets a joint's targetRotation to match a given local rotation.
/// The joint transform's local rotation must be cached on Start and passed into this method.
/// </summary>
public static void SetTargetRotationLocal (this ConfigurableJoint joint, Quaternion targetLocalRotation, Quaternion startLocalRotation)
{
if (joint.configuredInWorldSpace) {
@mstevenson
mstevenson / CreateAssetFromScript.cs
Created Feb 6, 2013
Creates an on-disk instance of a Unity ScriptableObject. To install, copy CreateAssetFromScript.cs into an Editor folder in your Unity project. To use, first select a script that inherits from ScriptableObject in the Project window, then select the menu Assets > Create Asset From ScriptableObject.
View CreateAssetFromScript.cs
using UnityEngine;
using UnityEditor;
using System;
public class CreateAssetFromScript : Editor {
[MenuItem("Assets/Create Asset From Manager Script", false, 10000)]
public static void CreateManager ()
{
ScriptableObject asset = ScriptableObject.CreateInstance (Selection.activeObject.name);
@mstevenson
mstevenson / StateMachine.cs
Last active Jul 10, 2017
Untested off-the-cuff example of a possible method-based state machine design for Unity.
View StateMachine.cs
// Defines the required parameters and return value type for a StateMachine state.
// Returns a bool representing whether or not the state has finished running.
public delegate bool StateDelegate ();
// To use, create an instance of StateMachine inside of a MonoBehaviour, load it up with
// references to state methods with ChangeState(), then call its Execute() method during the
// MonoBehaviour's Update cycle. An example MonoBehaviour is included at the bottom of this file.
public class StateMachine
{
// Keep track of the currently running state
@mstevenson
mstevenson / CameraPlane.cs
Created Jan 17, 2013
A simple solution for grabbing and dragging physics objects in Unity. Attach a DragRigidbody component to an object that has a both collider and a rigidbody. Add the object to a layer named "Interactive".
View CameraPlane.cs
using UnityEngine;
using System.Collections;
/// <summary>
/// Utility class for working with planes relative to a camera.
/// </summary>
public static class CameraPlane
{
/// <summary>
/// Returns world space position at a given viewport coordinate for a given depth.
@mstevenson
mstevenson / gist:4471373
Created Jan 7, 2013
Fisher-Yates shuffle
View gist:4471373
for (int i = branches.Length - 1; i > 0; --i) {
int next = Random.Range (0, i + 1);
Transform temp = shuffled [i];
shuffled [i] = shuffled [next];
shuffled [next] = temp;
}
@mstevenson
mstevenson / FileUtility.cs
Created Dec 27, 2012
A small file utility class for Unity. Transform an absolute path into an Assets folder relative path, and get a list of all Resources directories in the project.
View FileUtility.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class FileUtility {
/// <summary>
/// Determine whether a given path is a directory.
/// </summary>
@mstevenson
mstevenson / CreateQuadMesh.cs
Created Dec 27, 2012
Create a quad mesh asset in Unity. Place this script in the Editor folder.
View CreateQuadMesh.cs
using System;
using UnityEngine;
using UnityEditor;
public class CreateQuadMesh : Editor {
[MenuItem("Assets/Create/Quad Mesh", false, 10000)]
public static void Create ()
{
Mesh mesh = BuildQuad (1, 1);