How to show BIM data in runtime in Unity

In this tutorial you will learn how to show BIM data in the Unity console in runtime.

Explore the benefits of the Tridify BIM Publishing Service and start a FREE 14-day trial (no credit card required)

Before you start

Log in to the Tridify BIM Publishing Service and upload and process your IFC file. Import your processed file into Unity using the Tridify BIM Tools.

 

Unity_Show_BIM_Data_1

 

We have created this simple C# script as an example of how to show BIM data in runtime, just by clicking a GameObject. When clicking an object in the Game view, the script will print BIM data to the console.

 

Unity_Show_BIM_Data_2

 

Few things for you to do:

  • Add the script to a GameObject e.g. the Main Camera
  • Link Main Camera GameObject from hierarchy to ShowBimData.cs script RaycastCamera public field in the Inspector
  • Add colliders to all mesh objects as we are using raycast in the script!

 

using UnityEngine;
using Tridify;
using System;
using System.Linq;

public class ShowBimData : MonoBehaviour
{

    public Camera raycastCamera;
    private GameObject _selectedObject;

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Ray ray = raycastCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 1000))
            {
                _selectedObject = hit.transform.gameObject;
                
                // Here we get all IfcPropertySets
                var ifcPropertySets = _selectedObject.GetComponents<IfcPropertySet>();

                foreach(var pSet in ifcPropertySets)
                {
                    var attributeStrings = pSet.Attributes
                        .Select(attr => attr.Name + ": " + attr.Value);
                    var attributesString =
                        string.Join(Environment.NewLine, attributeStrings);

                    Debug.Log("Selected: " + pSet.GetType().Name
                        + Environment.NewLine + attributesString);

                }

                //Here we get IfcType. Just change the parameter if you want eg. IfcWallType
                var ifcType = _selectedObject.GetComponent<IfcType>();

                if (ifcType != null)
                {
                    var attributeStrings = ifcType.Attributes
                        .Select(attr => attr.Name + ": " + attr.Value);
                    var attributesString =
                        string.Join(Environment.NewLine, attributeStrings);

                    Debug.Log("Selected: " + ifcType.GetType().Name
                        + Environment.NewLine + attributesString);
                }
                else
                {
                    Debug.Log("No IfcType found on object " + _selectedObject.name);
                }
            }
            else
            {
                Debug.Log("");
            }
        }
    }
}