👩‍💻 Edit code

Learn how to add code to your Unity project while getting data from the cloud.

Now that you are able to successfully stream the 3D model into Unity, it's time to make some custom adjustments.

Each hologram will be instantiated with a script named CustomBehaviour.cs attached. You can edit this script to create any behavior you would like while referencing additional data streamed from the cloud.

Code example

Open the Assets/echoAR/CustomBehaviour.cs script:

CustomBehaviour.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CustomBehaviour : MonoBehaviour
{
[HideInInspector]
public Entry entry;
/// <summary>
/// EXAMPLE BEHAVIOUR
/// Queries the database and names the object based on the result.
/// </summary>
// Use this for initialization
void Start()
{
// Add RemoteTransformations script to object and set its entry
this.gameObject.AddComponent<RemoteTransformations>().entry = entry;
// ADD YOUR CODE HERE //
// Qurey additional data to get the name
string value = "";
if (entry.getAdditionalData() != null &&
entry.getAdditionalData().TryGetValue("name", out value))
{
// Set name
this.gameObject.name = value;
}
}
// Update is called once per frame
void Update()
{
}
}

Note that this is an regular Unity MonoBehaviour with additions to the Start() function.

Lines 18-19 attaches a RemoteTransformations component to the game object and sets its content entry. This component is in charge of enabling real-time updates and animations.

// Add RemoteTransformations script to object and set its entry
this.gameObject.AddComponent<RemoteTransformations>().entry = entry;

You can add any custom code after line 21. An example follows.

Query metadata

Lines 22-28 are an example that queries the entry's metadata for a key called name, and if such key exists, set the game object's name to the corresponding value:

string value = "";
if (entry.getAdditionalData() != null &&
entry.getAdditionalData().TryGetValue("name", out value))
{
// Set name
this.gameObject.name = value;
}

Without the name key being set, the default game object's name is the asset filename.

Use the console to set a metadata entry with the following data:

key

value

name

empire_state

Run Unity again and notice that the game object name automatically changes.

Great work! 🎉