Skip to content

Instantly share code, notes, and snippets.

View AngryAnt's full-sized avatar

Emil "AngryAnt" Johansen AngryAnt

View GitHub Profile
@AngryAnt
AngryAnt / DependencyInstall.cs
Last active December 30, 2015 04:29
A quick example demonstrating how unpacking logic for an asset store package could be set up to be contingent on the presence of a specific code dependency.
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies ())
{
string location = Path.GetDirectoryName (assembly.CodeBase).Replace (Path.DirectorySeparatorChar, '/');
if (location.StartsWith ("file:"))
{
location = location.Substring ("file:".Length);
}
if (!location.BeginsWith (Application.dataPath.Substring (0, Application.dataPath.Length - "Assets".Length)))
@AngryAnt
AngryAnt / gist:7080116
Created October 21, 2013 07:53
Silly workaround for easy alpha scaling on the Unity Color type.
public static Color A (this Color color, float alphaScale)
{
return new Color (color.r, color.g, color.b, color.a * alphaScale);
}
// Gizmos.color = Color.red.A (0.3f);
@AngryAnt
AngryAnt / ConstructorIsh.cs
Created October 16, 2013 20:56
For when you really want that one line for instantiation and initialisation.
class MyBehaviour : MonoBehaviour
{
public MyBehaviour Init (int some, float parameters)
{
//...
return this;
}
}
@AngryAnt
AngryAnt / MultiScene.cs
Created August 9, 2013 10:03
This utility lets you easily combine two scenes into one.
using UnityEngine;
using UnityEditor;
using System.IO;
public class MultiScene
{
[MenuItem ("File/Combine Scenes")]
static void Combine ()
{
@AngryAnt
AngryAnt / TerriblyHackyLog.cs
Created August 7, 2013 18:04
Abusing reflection and the internal Unity player build error function to get stacktrace-less error logs. Not recommended for anything sane.
using UnityEngine;
using System.Reflection;
public class TerriblyHackyLog : MonoBehaviour
{
void Start ()
{
Log ("Aaaaarrrrgh!");
}
@AngryAnt
AngryAnt / Controllers.cs
Created August 4, 2013 18:31
Adding axes to the Unity input manager. As described in the post http://angryant.com/2013/08/04/Unity-Hacks-Dual-sticks/
public static void AddAxis (AxisDefinition axis)
{
if (AxisDefined (axis.name))
{
throw new System.ArgumentException (string.Format ("Specified axis \"{0}\" already exists", axis.name));
}
SerializedObject serializedObject = new SerializedObject (AssetDatabase.LoadAllAssetsAtPath ("ProjectSettings/InputManager.asset")[0]);
SerializedProperty axesProperty = serializedObject.FindProperty ("m_Axes");
@AngryAnt
AngryAnt / Digits.cs
Last active December 20, 2015 08:18
Display an integral value in separate textures of digits. Untested.
Texture2D[] m_Numbers, m_Digits;
void SetValue (int value)
{
if (value >= Mathf.Pow (10, m_Digits.Length))
{
Debug.LogError ("Value overflow: Unable to display value: " + value);
}
for (int i = m_Digits.Length - 1; i >= 0; --i)
@AngryAnt
AngryAnt / Control-OnGesture.cs
Created July 19, 2013 13:25
Example of an OnGesture handler - handling left and right swipes as served up by the TouchGestures component.
void OnGesture (Gesture gesture)
{
switch (gesture.type)
{
case GestureType.Swipe:
if (Vector2.Dot (gesture.direction, new Vector2 (1.0f, 0.0f)) > 0.8f)
{
Previous ();
}
else if (Vector2.Dot (gesture.direction, new Vector2 (-1.0f, 0.0f)) > 0.8f)
@AngryAnt
AngryAnt / OnRenderTextureGUI.cs
Created July 12, 2013 13:04
Example code for "OnRenderTextureGUI" blog post on AngryAnt.com
void OnGUI ()
{
BeginRenderTextureGUI (m_TargetTexture);
GUILayout.Box ("This box goes on a render texture!");
EndRenderTextureGUI ();
}
@AngryAnt
AngryAnt / Broadcaster.cs
Created June 7, 2013 16:41
Broadcaster / Seeker basics for sharing and joining games on your local network. Example code for "The implicit local network interface" blog post on AngryAnt.com
if (m_BroadcastTimer == null)
// Setup broadcast of hosting service
{
m_BroadcastSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
IPAddress broadcastGroup = IPAddress.Parse (kBroadcastGroup);
m_BroadcastSocket.SetSocketOption (