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, classToSerialize);
stream.Close();
}

 

 

Advertisement

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 * 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;
}

 

.NET: Deserializing XML

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();
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Unity: Get all scene names

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);
}