-
C++: Simple text output
Two ways to output text from C++ quickly and easily: – std::cout << “Hello World” << std::endl; printf(“Hello World”); You’ll find a good overview of the differences here if you’re interested
-
C#: Get the current method name
Quick bit of code to display the current method name (Switch “Debug.Log” for “Console.WriteLine” to use it without Unity) System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(); Debug.Log(st.GetFrame(0).GetMethod().Name);
-
C#: Convert byte[] to string
Quick piece of code to convert a byte array to a string: – string myString = System.Text.Encoding.UTF8.GetString(myBytes);
-
Unity: Unsupported type vector
I got this error from Unity (2018.3.3f1) while I was working with a large script and changing data that was serialized into a scene. “Unsupported type Vector” was the error but it had no call stack and no explanation of where it came from. The solution was to delete the component from the scene, save…
-
C#: World Space to Pixels
Piece of code to convert from world space size to pixel size. Handy if you need to fit something to a pixel sized displayed area in a UI. All you need is to supply the size of the object in world space and the distance from the camera to the object. private Vector2 GetSizeAtDepth(Vector2 a_size,…
-
C# Serialize to Binary
Quick code snippet to show serialization of a class to a binary file called data.bin. Two points to note; 1) The constructor on the class must be parameter-less and 2) the class (and all sub-classes) must be marker as [Serializable]. using System.Runtime.Serialization.Formatters.Binary; using (Stream stream = File.Open(“./data.bin”, FileMode.Create)) { var bin = new BinaryFormatter(); bin.Serialize(stream,…
-
Unity: Clear the console output
Quick piece of code to clear the console log: – var assembly = Assembly.GetAssembly(typeof(UnityEditor.Editor)); var type = assembly.GetType(“UnityEditor.LogEntries”); var method = type.GetMethod(“Clear”); method.Invoke(new object(), null);
-
Unity: Is this point inside a box?
See if a point is inside a box by doing a reverse transform on the point into box local space. bool PointInBox(Vector3 a_point, BoxCollider a_box) { // Transform the point into box local space a_point = a_box.transform.InverseTransformPoint(a_point) – a_box.center; // see if the point is inside the box local space Vector3 half = a_box.size *…
-
C#: Serialize to XML
Writing this here so I can find it quickly: – using System.IO; using System.Xml.Serialization; [Serializable] public class SaveData { public string title; } XmlSerializer ser = new XmlSerializer(typeof(SaveData)); TextWriter writer = new StreamWriter(Path.Combine(Application.persistentDataPath, “data.xml”)); ser.Serialize(writer, m_data); writer.Close();
-
Unity: Make a programmatically generated button event show up in the Inspector
So, when you add an event to a button using AddListener, the event doesn’t show up in the inspector so you can’t see what’s going to be triggered by the button. When looking at the button in Play mode you see something like this : – Events need to show in the Inspector so you…
