Non-game 3D apps in Unity

HoloLens (and more generally, Windows Holographic) is around the corner and will transform the way people interact with devices and their expectancies from them. Holograms and 3D gestures are the new user interface elements you need to consider when you develop apps.

Yes, until now 3D mostly meant games, and I found that Unity is one of the best 3D game platforms. Very interestingly, it is also supported as a first class citizen for HoloLens development! So the question is asked immediately: why not developing 3D-enabled business apps (especially for holographic devices) using such a platform?

I tried it myself, and I created a simple C# script for Unity that generates a 3D bar chart based on 2D (row and column) numeric data that can be initialized into an app either with hard coded values or by generating them using other scripts. And the code is – literally – very simple (I developed it in just a few hours that included learning the basics about Unity), while the output is stunning (it would took days to do it with low level DirectX) – I posted the main part here below.

The complete source code is on GitHub. Alternatively, you can download the (importable) Unity package directly if you wish.

public class BarChartBehavior : MonoBehaviour
{
    public ChartData Data;
    ...

    void Update()
    {
        if (Data == null)
            return;
        var rows = Data.Rows;
        if (rows == null)
            return;
        var rowCount = rows.Length;
        ...
        var columnCount = ...;
        var maxValue = ...;
        for (var i = 0; i < rowCount; i++)
        {
            var row = rows[i];
            for (var j = 0; j < columnCount; j++)
            {
                var values = row.Values;
                if (values.Length <= j)
                    continue;
                var value = values[j];
                var cube = GetCube(i, j); // Create or reuse object.
                ...
                cube.transform.parent = gameObject.transform;
                cube.transform.localPosition = new Vector3((i + 0.5f) / rowCount, (value / 2f) / maxValue, (j + 0.5f) / columnCount);
                cube.transform.localScale = new Vector3(0.25f / rowCount, value / maxValue, 0.25f / columnCount);
                var cubeRenderer = cube.GetComponent<Renderer>();
                cubeRenderer.material = row.Material;
            }
            ...
        }
        ... // Also destroy objects that are no longer needed here.
    }

    private GameObject GetCube(int i, int j)
    {
        var cube = ...; // Reuse existing object from a local cache, if exists.
        if (cube == null) // Otherwise create a new object.
            cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        ...
        return cube;
    }
}

[Serializable]
public class ChartData
{
    public Row[] Rows;
    public string[] ColumnNames;
}

[Serializable]
public class Row
{
    public string Name;
    public Material Material;
    public float[] Values;
}

Here is also a screenshot of the output (you may see the project was not configured to run on HoloLens, but as a standalone app; however adapting it for HoloLens is very easy too):

unity-demo

 

Advertisements

About Sorin Dolha

My passion is software development, but I also like physics.
This entry was posted in HoloLens, Unity, UWP and tagged , , , . Bookmark the permalink.

One Response to Non-game 3D apps in Unity

  1. Pingback: Bridging | Code {Sections}

Add a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s