├── .gitignore ├── AudioSourceExtensions.cs ├── CameraExtensions.cs ├── ComponentExtensions.cs ├── ConfigurableJointExtensions.cs ├── GameObjectExtensions.cs ├── LICENSE ├── ListExtensions.cs ├── MeshExtensions.cs ├── MonobehaviourExtensions.cs ├── ObjectExtensions.cs ├── RayExtensions.cs ├── RectExtensions.cs ├── Texture2DExtensions.cs ├── TransformExtensions.cs └── Vector3Extensions.cs /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /AudioSourceExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public static class AudioSourceExtensions 5 | { 6 | 7 | public static void PlayClip (this AudioSource source, AudioClip clip) 8 | { 9 | source.clip = clip; 10 | source.Play (); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /CameraExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public static class CameraExtensions { 5 | 6 | /// 7 | /// Camera extension method for creating a screen point ray that takes into account 8 | /// a modified camera projection matrix 9 | /// 10 | public static Ray ShiftedScreenPointToRay (this Camera cam, Vector3 position) 11 | { 12 | Vector3 nearPoint = cam.ScreenToViewportPoint (position); 13 | 14 | Vector3 farPoint = new Vector3 (nearPoint.x, nearPoint.y, -1); 15 | farPoint.z = -1; 16 | 17 | nearPoint = cam.cameraToWorldMatrix * nearPoint; 18 | farPoint = cam.cameraToWorldMatrix * farPoint; 19 | 20 | return new Ray (nearPoint, farPoint); 21 | } 22 | 23 | public static float HorizontalFieldOfView (this Camera cam) 24 | { 25 | float side = Mathf.Tan (cam.fieldOfView * Mathf.Deg2Rad); 26 | return Mathf.Atan (side * cam.aspect) * Mathf.Rad2Deg; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /ComponentExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | public static class ComponentExtensions { 5 | 6 | public static T NearestTo (this IEnumerable components, Vector3 position) 7 | where T : Component 8 | { 9 | float dist = Mathf.Infinity; 10 | T target = null; 11 | foreach (var c in components) { 12 | float newDist = Vector3.SqrMagnitude (position - c.transform.position); 13 | if (newDist < dist) { 14 | dist = newDist; 15 | target = c; 16 | } 17 | } 18 | return target; 19 | } 20 | 21 | public static T FarthestFrom (this IEnumerable components, Vector3 position) 22 | where T : Component 23 | { 24 | float dist = 0; 25 | T target = null; 26 | foreach (var c in components) { 27 | float newDist = Vector3.SqrMagnitude (position - c.transform.position); 28 | if (newDist > dist) { 29 | dist = newDist; 30 | target = c; 31 | } 32 | } 33 | return target; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /ConfigurableJointExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public static class ConfigurableJointExtensions { 4 | /// 5 | /// Sets a joint's targetRotation to match a given local rotation. 6 | /// The joint transform's local rotation must be cached on Start and passed into this method. 7 | /// 8 | public static void SetTargetRotationLocal (this ConfigurableJoint joint, Quaternion targetLocalRotation, Quaternion startLocalRotation) 9 | { 10 | if (joint.configuredInWorldSpace) { 11 | Debug.LogError ("SetTargetRotationLocal should not be used with joints that are configured in world space. For world space joints, use SetTargetRotation.", joint); 12 | } 13 | SetTargetRotationInternal (joint, targetLocalRotation, startLocalRotation, Space.Self); 14 | } 15 | 16 | /// 17 | /// Sets a joint's targetRotation to match a given world rotation. 18 | /// The joint transform's world rotation must be cached on Start and passed into this method. 19 | /// 20 | public static void SetTargetRotation (this ConfigurableJoint joint, Quaternion targetWorldRotation, Quaternion startWorldRotation) 21 | { 22 | if (!joint.configuredInWorldSpace) { 23 | Debug.LogError ("SetTargetRotation must be used with joints that are configured in world space. For local space joints, use SetTargetRotationLocal.", joint); 24 | } 25 | SetTargetRotationInternal (joint, targetWorldRotation, startWorldRotation, Space.World); 26 | } 27 | 28 | static void SetTargetRotationInternal (ConfigurableJoint joint, Quaternion targetRotation, Quaternion startRotation, Space space) 29 | { 30 | // Calculate the rotation expressed by the joint's axis and secondary axis 31 | var right = joint.axis; 32 | var forward = Vector3.Cross (joint.axis, joint.secondaryAxis).normalized; 33 | var up = Vector3.Cross (forward, right).normalized; 34 | Quaternion worldToJointSpace = Quaternion.LookRotation (forward, up); 35 | 36 | // Transform into world space 37 | Quaternion resultRotation = Quaternion.Inverse (worldToJointSpace); 38 | 39 | // Counter-rotate and apply the new local rotation. 40 | // Joint space is the inverse of world space, so we need to invert our value 41 | if (space == Space.World) { 42 | resultRotation *= startRotation * Quaternion.Inverse (targetRotation); 43 | } else { 44 | resultRotation *= Quaternion.Inverse (targetRotation) * startRotation; 45 | } 46 | 47 | // Transform back into joint space 48 | resultRotation *= worldToJointSpace; 49 | 50 | // Set target rotation to our newly calculated rotation 51 | joint.targetRotation = resultRotation; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /GameObjectExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | public static class GameObjectExtensions { 6 | 7 | // From this thread: http://forum.unity3d.com/threads/101028-How-to-Get-all-components-on-an-object-that-implement-an-interface. 8 | 9 | /// 10 | /// Returns all monobehaviours (casted to T) 11 | /// 12 | /// interface type 13 | /// 14 | /// 15 | public static T[] GetInterfaces (this GameObject gObj) 16 | { 17 | if (!typeof(T).IsInterface) 18 | throw new System.Exception ("Specified type is not an interface!"); 19 | var mObjs = gObj.GetComponents (); 20 | return (from a in mObjs 21 | where a.GetType ().GetInterfaces ().Any(k => k == typeof(T)) 22 | select (T)(object)a 23 | ).ToArray(); 24 | } 25 | 26 | /// 27 | /// Returns the first monobehaviour that is of the interface type (casted to T) 28 | /// 29 | /// Interface type 30 | /// 31 | /// 32 | public static T GetInterface (this GameObject gObj) 33 | { 34 | if (!typeof(T).IsInterface) 35 | throw new System.Exception("Specified type is not an interface!"); 36 | return gObj.GetInterfaces ().FirstOrDefault(); 37 | } 38 | 39 | /// 40 | /// Returns the first instance of the monobehaviour that is of the interface type T (casted to T) 41 | /// 42 | /// 43 | /// 44 | /// 45 | public static T GetInterfaceInChildren (this GameObject gObj) 46 | { 47 | if (!typeof (T).IsInterface) 48 | throw new System.Exception ("Specified type is not an interface!"); 49 | return gObj.GetInterfacesInChildren ().FirstOrDefault (); 50 | } 51 | 52 | /// 53 | /// Gets all monobehaviours in children that implement the interface of type T (casted to T) 54 | /// 55 | /// 56 | /// 57 | /// 58 | public static T[] GetInterfacesInChildren (this GameObject gObj) 59 | { 60 | if (!typeof(T).IsInterface) 61 | throw new System.Exception("Specified type is not an interface!"); 62 | var mObjs = gObj.GetComponentsInChildren(); 63 | return (from a in mObjs 64 | where a.GetType ().GetInterfaces ().Any (k => k == typeof(T)) 65 | select (T)(object)a 66 | ).ToArray(); 67 | } 68 | 69 | /// 70 | /// Finds the GameObject within a collection that is nearest to the given point. 71 | /// 72 | public static GameObject NearestTo (this IEnumerable components, Vector3 position) 73 | { 74 | float dist = Mathf.Infinity; 75 | GameObject target = null; 76 | foreach (var c in components) { 77 | float newDist = Vector3.SqrMagnitude (position - c.transform.position); 78 | if (newDist < dist) { 79 | dist = newDist; 80 | target = c; 81 | } 82 | } 83 | return target; 84 | } 85 | 86 | /// 87 | /// Finds the GameObject within a collection that is farthest from the given point. 88 | /// 89 | public static GameObject FarthestFrom (this IEnumerable components, Vector3 position) 90 | { 91 | float dist = 0; 92 | GameObject target = null; 93 | foreach (var c in components) { 94 | float newDist = Vector3.SqrMagnitude (position - c.transform.position); 95 | if (newDist > dist) { 96 | dist = newDist; 97 | target = c; 98 | } 99 | } 100 | return target; 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Michael Stevenson 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /ListExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | public static class ListExtensions 6 | { 7 | 8 | /// 9 | /// Move an item from one index to another 10 | /// 11 | public static void Move (this List list, int oldIndex, int newIndex) 12 | { 13 | T item = list[oldIndex]; 14 | list.RemoveAt (oldIndex); 15 | if (newIndex > oldIndex) 16 | newIndex--; 17 | list.Insert (newIndex, item); 18 | } 19 | 20 | 21 | /// 22 | /// Move an item from one index to another 23 | /// 24 | public static void Move (this List list, T item, int newIndex) 25 | { 26 | int oldIndex = list.IndexOf (item); 27 | list.RemoveAt (oldIndex); 28 | if (newIndex > oldIndex) 29 | newIndex--; 30 | list.Insert (newIndex, item); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /MeshExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public static class MeshExtensions 5 | { 6 | /// 7 | /// Clone a given mesh and return a reference to it. 8 | /// 9 | public static Mesh CopyMesh (this Mesh mesh) 10 | { 11 | Mesh newMesh = new Mesh(); 12 | newMesh.vertices = mesh.vertices; 13 | newMesh.triangles = mesh.triangles; 14 | newMesh.uv = mesh.uv; 15 | newMesh.normals = mesh.normals; 16 | newMesh.colors = mesh.colors; 17 | newMesh.tangents = mesh.tangents; 18 | return newMesh; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /MonobehaviourExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public static class MonobehaviourExtensions 5 | { 6 | public static void AutoSerializeNetworkView (this MonoBehaviour self, BitStream stream, NetworkMessageInfo info) 7 | where T : MonoBehaviour 8 | { 9 | var fields = typeof(T).GetFields (); 10 | foreach (var f in fields) { 11 | var val = f.GetValue (self); 12 | if (val is int) { 13 | int s = 0; 14 | if (stream.isWriting) { 15 | s = (int)val; 16 | stream.Serialize (ref (int)s); 17 | } else { 18 | stream.Serialize (ref s); 19 | f.SetValue (self, s); 20 | } 21 | } else if (val is float) { 22 | float s = 0; 23 | if (stream.isWriting) { 24 | s = (float)val; 25 | stream.Serialize (ref s); 26 | } else { 27 | stream.Serialize (ref s); 28 | f.SetValue (self, s); 29 | } 30 | } else if (val is bool) { 31 | bool s = false; 32 | if (stream.isWriting) { 33 | s = (bool)val; 34 | stream.Serialize (ref s); 35 | } else { 36 | stream.Serialize (ref s); 37 | f.SetValue (self, s); 38 | } 39 | } else if (val is char) { 40 | char s = ' '; 41 | if (stream.isWriting) { 42 | s = (char)val; 43 | stream.Serialize (ref s); 44 | } else { 45 | stream.Serialize (ref s); 46 | f.SetValue (self, s); 47 | } 48 | } else if (val is short) { 49 | short s = 0; 50 | if (stream.isWriting) { 51 | s = (short)val; 52 | stream.Serialize (ref s); 53 | } else { 54 | stream.Serialize (ref s); 55 | f.SetValue (self, s); 56 | } 57 | } else if (val is Quaternion) { 58 | Quaternion s = Quaternion.identity; 59 | if (stream.isWriting) { 60 | s = (Quaternion)val; 61 | stream.Serialize (ref s); 62 | } else { 63 | stream.Serialize (ref s); 64 | f.SetValue (self, s); 65 | } 66 | } else if (val is Vector3) { 67 | Vector3 s = Vector3.zero; 68 | if (stream.isWriting) { 69 | s = (Vector3)val; 70 | stream.Serialize (ref s); 71 | } else { 72 | stream.Serialize (ref s); 73 | f.SetValue (self, s); 74 | } 75 | } else if (val is NetworkPlayer) { 76 | NetworkPlayer s = new NetworkPlayer (); 77 | if (stream.isWriting) { 78 | s = (NetworkPlayer)val; 79 | stream.Serialize (ref s); 80 | } else { 81 | stream.Serialize (ref s); 82 | f.SetValue (self, s); 83 | } 84 | } else if (val is NetworkViewID) { 85 | NetworkViewID s = new NetworkViewID (); 86 | if (stream.isWriting) { 87 | s = (NetworkViewID)val; 88 | stream.Serialize (ref s); 89 | } else { 90 | stream.Serialize (ref s); 91 | f.SetValue (self, s); 92 | } 93 | } 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /ObjectExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public static class ObjectExtensions { 5 | 6 | public static T FindObjectOfType (this UnityEngine.Object obj) 7 | where T : UnityEngine.Object 8 | { 9 | return (UnityEngine.Object.FindObjectOfType (typeof(T)) as T); 10 | } 11 | 12 | public static T[] FindObjectsOfType (this UnityEngine.Object obj) 13 | where T : UnityEngine.Object 14 | { 15 | return (UnityEngine.Object.FindObjectsOfType (typeof(T)) as T[]); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /RayExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public static class RayExtensinos 5 | { 6 | 7 | public static Vector3 ClosestPoint (this Ray r, Vector3 position) 8 | { 9 | Vector3 point = position - r.origin; 10 | Vector3 projection = Vector3.Project (point, r.direction); 11 | return point - projection; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /RectExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public static class RectExtensions 4 | { 5 | public static Rect Scale (this Rect rect, float scale) 6 | { 7 | return new Rect (rect.x * scale, rect.y * scale, rect.width * scale, rect.height * scale); 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Texture2DExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public static class Texture2DExtensions 5 | { 6 | /// 7 | /// Draw a GUI texture 8 | /// 9 | public static void DrawGUI (this Texture2D tex, float xOffset, float yOffset) 10 | { 11 | GUI.DrawTexture (new Rect (xOffset, yOffset, tex.width, tex.height), tex); 12 | } 13 | 14 | 15 | /// 16 | /// Create a basic material from this texture. Any materials created with this 17 | /// method from the same texture will be shared and can be batched. 18 | /// 19 | public static Material CreateSharedMaterial (this Texture2D texture, string shader) 20 | { 21 | Shader targetShader = Shader.Find (shader); 22 | Material[] mats = Object.FindObjectsOfType (typeof(Material)) as Material[]; 23 | // Return existing material 24 | foreach (var mat in mats) { 25 | if (mat.shader != targetShader) 26 | continue; 27 | if (mat.mainTexture == null) 28 | continue; 29 | if (mat.mainTexture == texture) 30 | return mat; 31 | } 32 | // Create new material 33 | Material newMat = new Material (targetShader); 34 | newMat.mainTexture = texture; 35 | return newMat; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /TransformExtensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public static class TransformExtensions 5 | { 6 | /// 7 | /// Apply a transformation resembling a parenting relationship without setting up an actual parenting hierarchy. 8 | /// 9 | public static void SoftParent (this Transform child, Transform parent) 10 | { 11 | Vector3 offset = child.position - parent.position; 12 | Vector3 scale = parent.localScale; 13 | 14 | // TRS 15 | offset.Scale (scale); 16 | child.position = parent.rotation * offset + parent.position; 17 | child.rotation = parent.rotation; 18 | // FIXME need to add the original rotation 19 | child.localScale.Scale (parent.localScale); 20 | } 21 | 22 | public static Transform FindParent (this Transform transform, string name) 23 | { 24 | Transform current = transform; 25 | while (current.parent) { 26 | current = current.parent; 27 | if (current.name == name) 28 | return current; 29 | } 30 | return null; 31 | } 32 | 33 | public static Transform FindSibling (this Transform transform, string name) 34 | { 35 | if (transform.parent) 36 | return transform.parent.Find (name); 37 | return null; 38 | } 39 | 40 | public static Transform FindFromRoot (this Transform transform, string name) 41 | { 42 | return transform.root.Find (name); 43 | } 44 | 45 | public static T GetComponentInParent (this Transform transform) 46 | where T : Component 47 | { 48 | Transform current = transform; 49 | while (current.parent) { 50 | current = current.parent; 51 | T component = current.GetComponent (); 52 | if (component != null) 53 | return component; 54 | } 55 | return null; 56 | } 57 | 58 | public static void SetScaleX (this Transform trans, float value) 59 | { 60 | trans.localScale = new Vector3 (value, trans.localScale.y, trans.localScale.z); 61 | } 62 | 63 | public static void SetScaleY (this Transform trans, float value) 64 | { 65 | trans.localScale = new Vector3 (trans.localScale.x, value, trans.localScale.z); 66 | } 67 | 68 | public static void SetScaleZ (this Transform trans, float value) 69 | { 70 | trans.localScale = new Vector3 (trans.localScale.x, trans.localScale.y, value); 71 | } 72 | 73 | public static void SetPositionX (this Transform trans, float value) 74 | { 75 | trans.position = new Vector3 (value, trans.position.y, trans.position.z); 76 | } 77 | 78 | public static void SetPositionY (this Transform trans, float value) 79 | { 80 | trans.position = new Vector3 (trans.position.x, value, trans.position.z); 81 | } 82 | 83 | public static void SetPositionZ (this Transform trans, float value) 84 | { 85 | trans.position = new Vector3 (trans.position.x, trans.position.y, value); 86 | } 87 | 88 | public static void SetLocalPositionX (this Transform trans, float value) 89 | { 90 | trans.localPosition = new Vector3 (value, trans.localPosition.y, trans.localPosition.z); 91 | } 92 | 93 | public static void SetLocalPositionY (this Transform trans, float value) 94 | { 95 | trans.localPosition = new Vector3 (trans.localPosition.x, value, trans.localPosition.z); 96 | } 97 | 98 | public static void SetLocalPositionZ (this Transform trans, float value) 99 | { 100 | trans.localPosition = new Vector3 (trans.localPosition.x, trans.localPosition.y, value); 101 | } 102 | 103 | public static void SnapTo (this Transform self, Transform target) 104 | { 105 | self.position = target.position; 106 | self.rotation = target.rotation; 107 | } 108 | 109 | public static void SnapTo (this Transform self, GameObject target) 110 | { 111 | self.position = target.transform.position; 112 | self.rotation = target.transform.rotation; 113 | } 114 | 115 | public static void ParentTo (this Transform self, Transform parent) 116 | { 117 | self.parent = parent; 118 | } 119 | 120 | public static void ParentTo (this Transform self, GameObject parent) 121 | { 122 | self.parent = parent.transform; 123 | } 124 | 125 | /// 126 | /// Sets the transform to be a child of the given transform, and modifies its position and rotation to match its new parent. 127 | /// 128 | /// 129 | public static void SnapAndParent (this Transform self, Transform parent) 130 | { 131 | self.parent = parent; 132 | self.localPosition = Vector3.zero; 133 | self.localRotation = Quaternion.identity; 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /Vector3Extensions.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public static class Vector3Extensions { 5 | 6 | /// 7 | /// Find the largest component of a Vector3. 8 | /// 9 | public static float LargestComponent (this Vector3 v) 10 | { 11 | return v.x > v.y ? (v.x > v.z ? v.x : (v.y > v.z ? v.y : v.z)) : (v.y > v.z ? v.y : (v.x > v.z ? v.x : v.z)); 12 | } 13 | 14 | public static Vector3 WithX (this Vector3 vec, float value) 15 | { 16 | return new Vector3 (value, vec.y, vec.z); 17 | } 18 | 19 | public static Vector3 WithY (this Vector3 vec, float value) 20 | { 21 | return new Vector3 (vec.x, value, vec.z); 22 | } 23 | 24 | public static Vector3 WithZ (this Vector3 vec, float value) 25 | { 26 | return new Vector3 (vec.x, vec.y, value); 27 | } 28 | 29 | } 30 | --------------------------------------------------------------------------------