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
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
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);
Quick piece of code to convert a byte array to a string: –
string myString = System.Text.Encoding.UTF8.GetString(myBytes);
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, classToSerialize); stream.Close(); }
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 * 0.5f; if (a_point.x < half.x && a_point.x > -half.x && a_point.y < half.y && a_point.y > -half.y && a_point.z < half.z && a_point.z > -half.z) { // Get a 0.0 to 1.0 range for each axis Vector3 current = a_point + half; m_last.x = current.x / a_box.size.x; m_last.y = current.y / a_box.size.y; m_last.z = current.z / a_box.size.z; m_lastPoint = a_point; // Set the last valid point inside the test area return true; } return false; }
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();
Quick and simple: –
var count = mrString.Count(x => x == '$')
Quick example of deserializing a chunk of XML into a set of classes: –
<?xml version="1.0" encoding="UTF-8"?> <!-- Parser configuration control XML --> <parserConfig version="1.0"> <parsers> <!-- Test--> <parse count="6" output="[0][3] = [5];"> <checks> <check index="0" type="whiteSpace" /> <check index="1" type="alphaNumeric" content="test"/> <check index="2" type="whiteSpace" /> <check index="3" type="alphaNumeric" /> <check index="4" type="separator" /> <check index="5" type="alphaNumeric" /> </checks> </parse> <!-- Test2--> <parse count="4" output="[0][3]();"> <checks> <check index="0" type="whiteSpace" /> <check index="1" type="alphaNumeric" content="test2"/> <check index="2" type="whiteSpace" /> </checks> </parse> </parsers> </parserConfig>
[Serializable()] public class Parse { [XmlAttribute("count")] public int Count { get; set; } [XmlAttribute("output")] public string Output { get; set; } [XmlArray("checks")] [XmlArrayItem("check", typeof(Check))] public Check[] Check { get; set; } } [Serializable()] public class Check { [XmlAttribute("index")] public int Index { get; set; } [XmlAttribute("type")] public string Type { get; set; } [XmlAttribute("content")] public string Content { get; set; } } [Serializable()] [XmlRoot("parserConfig")] public class ParserCollection { [XmlArray("parsers")] [XmlArrayItem("parse", typeof(Parse))] public Parse[] Parse { get; set; } } public Parser() { ParserCollection config = null; string path = "../../../config.xml"; XmlSerializer serializer = new XmlSerializer(typeof(ParserCollection)); StreamReader reader = new StreamReader(path); config = (ParserCollection)serializer.Deserialize(reader); reader.Close(); }
Quick code snippet to grab all scene names in a project: –
public List<string> scenes; int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCountInBuildSettings; for (int i = 0; i < sceneCount; i++) { string sceneName = System.IO.Path.GetFileNameWithoutExtension(UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex(i)); scenes.Add(sceneName); Debug.Log("Scene: " + sceneName); }
Nice little piece of code found here
public static float DistanceToLine(Ray ray, Vector3 point) { return Vector3.Cross(ray.direction, point - ray.origin).magnitude; }