Scripting to Extend the AR | AR Unity Developer

Derek Anderson
4 min readMay 3, 2024

--

With the completion of a simple collapsible menu, this AR app is starting to look more like a proper app. It’s time to add some more features to this app. Specifically, an examine feature that will bring an object closer to the user on screen when selected.

A fresh scene will be needed for this feature. Once created, the scene should be set up similar to the previous one, including the XR Origin, AR Session, AR Placement Interactable, Placement Prefab, etc. This scene will need an Examinable Manager GameObject created in order to get this examine feature working. This examiner scene will have some new C# scripts to create in order for the placement prefabs to be examined up close. These scripts will be named Examinable and ExaminableManager.

public class ExaminableManager : MonoBehaviour
{
[SerializeField]
private string _whatToPrint = "some words in a variable";

[SerializeField]
private Transform _examineTarget;

// Start is called before the first frame update
void Start()
{
print(_examineTarget.name);
}

// Update is called once per frame
void Update()
{

}

Right now, ExaminableManager will print out the name of the Examine Target through dot notation. Create an empty GameObject that is a child of the Main Camera and attach it to ExaminableManager. The ExaminableManager now has a reference to the Examine Target.

However, ExaminableManager and Examinable can’t communicate with each other just yet. Attach the Examinable script to the placement prefab. These placement prefabs won’t be in the scene by default, so the reference will be connected at runtime, or when a prefab is created with a tap. This connection can be done through the use of FindObjectOfType. In this case, Examinable will look for ExaminableManager to make that connection.

public class Examinable : MonoBehaviour
{
[SerializeField]
private ExaminableManager _examinableManager;

// Start is called before the first frame update
void Start()
{
_examinableManager = FindObjectOfType<ExaminableManager>();
}

// Update is called once per frame
void Update()
{
print("test!");
}
}

The scripts can now communicate with each other, and the placement prefab knows how to communicate with the ExaminableManager. The next step is to create a method to make the cube the examined object when selected. Before working on the Examinable script, add the AR Selection Interactable component to the placement prefab. Next, the code that will be added needs to be a public method in order to work with the Interactable Events on the AR Selection Interactable on the placement prefab. Create a public method on Examinable and attach the script in the Select Entered event for the AR Selection Interactable on the placement prefab.

 public void RequestExamine()
{

}

However, the ExaminableManager script will handle the movement of the placement prefab to be examined with a method. This one will also be public and called PerformExamine(). With this method, the Examinable script can talk to the ExaminableManager to perform the actual examination of the object.

public void RequestExamine()
{
_examinableManager.PerformExamine();
}

PerformExamine() on ExaminableManager doesn’t currently have a reference to the specific examinable calling the method. A parameter will help for the scope of this method.

public void PerformExamine(Examinable examinable)
{

}

RequestExamine on Examinable can use the “this” keyword, which refers to this specific instance.

public void RequestExamine()
{
_examinableManager.PerformExamine(this);
}

The information that we need to access is the position of the examinable, which is found in the transform component. The position of the examinable will be changed to the position of _examineTarget that was created earlier. In addition, the parent of the examinable will be set. This will be the child GameObject of the Main Camera, so that it will follow the camera on our phone.

    public void PerformExamine(Examinable examinable)
{
examinable.transform.position = _examineTarget.position;
examinable.transform.parent = _examineTarget;
}

As it stands, this examinable will be at the same position as the Main Camera. ExamineTarget’s position should be modified in the Unity Editor so that the placement prefab can be seen when it is examined.

After a quick cleanup of the Examinable and ExaminableManager scripts, the examine feature is just about ready to test out.

public class Examinable : MonoBehaviour
{
[SerializeField]
private ExaminableManager _examinableManager;

// Start is called before the first frame update
void Start()
{
_examinableManager = FindObjectOfType<ExaminableManager>();
}

// Update is called once per frame
void Update()
{

}

//Request object to be examined when selected
public void RequestExamine()
{
_examinableManager.PerformExamine(this);
print("Examine has been requested");
}

}
public class ExaminableManager : MonoBehaviour
{

[SerializeField]
private Transform _examineTarget;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

public void PerformExamine(Examinable examinable)
{
examinable.transform.position = _examineTarget.position;
examinable.transform.parent = _examineTarget;
}
}

The larger square is the one being examined, while the smaller square is the unselected placement prefab.

--

--