Skip to content

Instantly share code, notes, and snippets.

@kkukshtel
Last active September 12, 2019 20:32
Show Gist options
  • Save kkukshtel/3cec9473bb5eda90635f1765e259498d to your computer and use it in GitHub Desktop.
Save kkukshtel/3cec9473bb5eda90635f1765e259498d to your computer and use it in GitHub Desktop.
Add text into your Unity Game/Scene view using only a script. Basically Debug.Log for runtime text.
//by kyle kukshtel
//sample usage of DebugText.cs
using UnityEngine;
using System.Collections.Generic;
public static class DebugText
{
static float defaultXScale = .3f;
static float defaultYScale = .3f;
static float defaultKerning = .5f;
//write along the x axis, facing positive z
public static void Write(string text, Vector3 position)
{
Write(text, position, Vector3.right, Vector3.forward, Color.yellow, defaultXScale, defaultYScale, defaultKerning);
}
public static void Write(string text, Vector3 position, Color c)
{
Write(text, position, Vector3.right, Vector3.forward, c, defaultXScale, defaultYScale, defaultKerning);
}
public static void Write(string text, Vector3 position, Vector3 writeDirection, Vector3 lookDirection)
{
Write(text, position, writeDirection, lookDirection, Color.yellow, defaultXScale, defaultYScale, defaultKerning);
}
public static void Write(string text, Vector3 position, Vector3 writeDirection, Vector3 lookDirection, Color color)
{
Write(text, position, writeDirection, lookDirection, color, defaultXScale, defaultYScale, defaultKerning);
}
public static void Write(string text, Vector3 position, Vector3 writeDirection, Vector3 lookDirection, Color color, float xScale, float yScale)
{
Write(text, position, writeDirection, lookDirection, color, xScale, yScale, defaultKerning);
}
public static void Write(string text, Vector3 position, Vector3 writeDirection, Vector3 lookDirection, Color color, float xScale, float yScale, float kerning)
{
Vector3 normWrite = Vector3.Normalize(writeDirection);
Vector3 normLook = Vector3.Normalize(lookDirection);
Quaternion q = Quaternion.LookRotation(normLook, Vector3.Cross(normLook, normWrite));
char[] charText = text.ToUpper().ToCharArray();
float offset = 0;
for (int i = 0; i < text.Length; i++)
{
int[] instruction = DebugLetters.GetCharInstructions(charText[i]);
for (int l = 0; l < instruction.Length; l++)
{
if(l+3 >= instruction.Length){break;}
Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, q, Vector3.one);
Vector3 start = new Vector3((position.x + offset + instruction[l])*xScale, (position.y + instruction[l+1]) * yScale, position.z);
Vector3 end = new Vector3((position.x + offset + instruction[l+2])*xScale, (position.y + instruction[l+3]) * yScale, position.z);
start = m.MultiplyPoint3x4(start);
end = m.MultiplyPoint3x4(end);
Debug.DrawLine(start,end,color);
l++;
}
offset += (2 + kerning); //2 is width of a single letter
}
}
}
public static class DebugLetters
{
public static int[] GetCharInstructions(char c)
{
switch (c)
{
case 'A':
return A;
case 'B':
return B;
case 'C':
return C;
case 'D':
return D;
case 'E':
return E;
case 'F':
return F;
case 'G':
return G;
case 'H':
return H;
case 'I':
return I;
case 'J':
return J;
case 'K':
return K;
case 'L':
return L;
case 'M':
return M;
case 'N':
return N;
case 'O':
return O;
case 'P':
return P;
case 'Q':
return Q;
case 'R':
return R;
case 'S':
return S;
case 'T':
return T;
case 'U':
return U;
case 'V':
return V;
case 'W':
return W;
case 'X':
return X;
case 'Y':
return Y;
case 'Z':
return Z;
case '0':
return zero;
case '1':
return one;
case '2':
return two;
case '3':
return three;
case '4':
return four;
case '5':
return five;
case '6':
return six;
case '7':
return seven;
case '8':
return eight;
case '9':
return nine;
default:
return new int[0];
}
}
/*
0,2 1,2 2,2
0,1 1,1 2,1
0,0 1,0 2,0
*/
public static int[] A = new int[]
{
0,0,
0,2,
2,2,
2,0,
2,1,
0,1,
};
public static int[] B = new int[]
{
0,0,
2,0,
2,2,
0,2,
1,2,
1,0,
1,1,
2,1
};
public static int[] C = new int[]
{
2,2,
0,2,
0,0,
2,0
};
public static int[] D = new int[]
{
0,2,
2,2,
2,0,
0,0,
1,0,
1,2
};
public static int[] E = new int[]
{
2,2,
0,2,
0,0,
2,0,
0,0,
0,1,
1,1
};
public static int[] F = new int[]
{
0,0,
0,2,
2,2,
0,2,
0,1,
2,1
};
public static int[] G = new int[]
{
2,2,
0,2,
0,0,
2,0,
2,1,
1,1
};
public static int[] H = new int[]
{
0,0,
0,2,
0,1,
2,1,
2,2,
2,0
};
public static int[] I = new int[]
{
0,0,
2,0,
1,0,
1,2,
0,2,
2,2
};
public static int[] J = new int[]
{
1,2,
2,2,
2,0,
0,0,
0,1
};
public static int[] K = new int[]
{
0,0,
0,2,
0,1,
2,2,
0,1,
2,0
};
public static int[] L = new int[]
{
0,2,
0,0,
2,0
};
public static int[] M = new int[]
{
0,0,
0,2,
1,1,
2,2,
2,0
};
public static int[] N = new int[]
{
0,0,
0,2,
2,0,
2,2
};
public static int[] O = new int[]
{
0,0,
2,0,
2,2,
0,2,
0,0
};
public static int[] P = new int[]
{
0,0,
0,2,
2,2,
2,1,
0,1
};
public static int[] Q = new int[]
{
1,1,
2,0,
2,2,
0,2,
0,0,
2,0
};
public static int[] R = new int[]
{
0,0,
0,2,
2,2,
2,1,
0,1,
1,1,
2,0
};
public static int[] S = new int[]
{
0,0,
2,0,
2,1,
0,1,
0,2,
2,2
};
public static int[] T = new int[]
{
0,2,
2,2,
1,2,
1,0
};
public static int[] U = new int[]
{
0,2,
0,0,
2,0,
2,2
};
public static int[] V = new int[]
{
0,2,
1,0,
2,2
};
public static int[] W = new int[]
{
0,2,
0,0,
1,1,
2,0,
2,2
};
public static int[] X = new int[]
{
0,0,
2,2,
1,1,
2,0,
0,2
};
public static int[] Y = new int[]
{
0,2,
1,1,
2,2,
1,1,
1,0
};
public static int[] Z = new int[]
{
0,2,
2,2,
0,0,
2,0
};
public static int[] zero = new int[]
{
0,0,
2,0,
2,2,
0,2,
0,0,
2,2
};
public static int[] one = new int[]
{
0,1,
1,2,
1,0,
0,0,
2,0
};
public static int[] two = new int[]
{
0,2,
2,2,
2,1,
0,1,
0,0,
2,0
};
public static int[] three = new int[]
{
0,0,
2,0,
2,2,
0,2,
2,2,
2,1,
1,1
};
public static int[] four = new int[]
{
0,2,
0,1,
2,1,
2,2,
2,0
};
public static int[] five = new int[]
{
2,2,
0,2,
0,1,
2,1,
2,0,
0,0
};
public static int[] six = new int[]
{
2,2,
0,2,
0,0,
2,0,
2,1,
0,1
};
public static int[] seven = new int[]
{
0,2,
2,2,
1,0
};
public static int[] eight = new int[]
{
0,1,
2,1,
2,0,
0,0,
0,2,
2,2,
2,1
};
public static int[] nine = new int[]
{
0,0,
2,0,
2,2,
0,2,
0,1,
2,1
};
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//sample usage of DebugText.cs
[ExecuteInEditMode]
public class DebugTest : MonoBehaviour {
// Update is called once per frame
[ExecuteInEditMode]
void Update () {
DebugText.Write("hello world", Vector3.zero, Color.red);
DebugText.Write("YDIRECTION", Vector3.zero, Vector3.up, Vector3.forward, Color.green);
DebugText.Write("ZDIRECTION", Vector3.zero, Vector3.forward, Vector3.left, Color.blue);
Vector3 v = new Vector3(1,1,-1);
DebugText.Write("MY DIRECTION", Vector3.zero, v, Vector3.Cross(v, Vector3.up), Color.blue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment