Rename a member in Unity without losing the assignments in the Inspector

This is pretty simple when you have the right tag.

public GameObject m_foo;

To rename m_foo to m_bar without losing the assignments that have been set in a Unity scene, add the following on the line before the m_foo declaration: –

[FormerlySerializedAs(“m_foo”)]

Now rename m_foo to m_bar and go have a look at the scene in Unity. The member name is changed but the assignments aren’t lost.

Once you’ve been in to Unity and the scene has updated, it’s safe to delete the “[FormerlySerializedAs(“m_foo”)]” line as it’s not needed anymore.

Hope it helps!

Advertisement

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 the scene and then re-open it and re-add the component. Sounds weird but I have verified that it solves the problem.

This is all that was shown in the output window: –

UnsupportedTypeVector

 

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, float a_depth, Camera a_camera)
{
    float fov2 = a_camera.fieldOfView * Mathf.PI / 360.0f;
    float height = Mathf.Tan(fov2) * a_depth * 2.0f;

    float pixelHeight = (a_size.y / height) * Screen.height;
    float pixelWidth = (a_size.x / (height * a_camera.aspect)) * Screen.width;

    return new Vector2(pixelWidth, pixelHeight);
}

 

Call it with something like the code below and it’ll return pixel sizes using the current screen res.

float depth = 8.0f;
Vector2 res = GetSizeAtDepth(new Vector2(10.0f, 10.0f), depth, m_renderCamera);

Debug.Log("pixelWidth: " + res.x);
Debug.Log("pixelHeight: " + res.y);

 

So, this is passing in a 10 x 10 x/y as the world space size (size of a standard Unity plane).

The ‘depth’ is the distance from the camera to the object. As the object is a plane that’s facing the camera, we don’t need to find a specific depth within the object. i.e. if it was a sphere then we’d have to decide if we wanted depth to the front, middle or back.

m_renderCamera is just a standard Unity camera.

Obviously, this can easily be adapted to work without Unity but I’m using it to get something up and running quickly.

Hope it helps!

 

 

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

 

 

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