C# | Find Object by Type | Examinable | ExaminableManager | ExamineTarget | AR Selection Interactable | Interaction Events | OnSelection()
Unity AR : Programming Custom Mechanics | Examination Mode
Create a Mechanic to Allow the User to Examine Objects Upon Selection
STEPS NEEDED
- Create & Configure ExamineTarget GameObject
- Create & Configure ExaminableManager GameObject & Script
- Create & Configure Examinable Script
- Configure SelectEntered() Logic
STEP ONE — Create & Configure ExamineTarget GameObject
XR Origin > Camera Offset > Main Camera > Create Empty gameObject
ExamineTarget is used as a placeholder for the desired position of the examinable object in examine mode. It also will become the parent of the examinable object, thus attaching it to a fixed position on the screen.
ExamineTarget’s position will need to be offset on the Y axis to be a short distance away from the camera.
STEP TWO — Create & Configure ExaminableManager GameObject & Script
Scene > Create Empty GameObject → ExaminableManager
Project > Scripts > Create New Script → ExaminableManager
ExaminableManager > Add Component > ExaminableManager
ExaminableManager is a manager class to handle all logic related to the examine mechanic.
public class ExaminableManager : MonoBehaviour
{
[SerializeField]
private Transform _examineTarget;
public void PerformExamine(Examinable examinable)
{
examinable.transform.position = _examineTarget.position;
examinable.transform.parent = _examineTarget;
print("Examine has been performed");
}
}
_examineTarget — examineTarget GameObject : gains vital data from the examineTarget such as its position, and the ability to assign child objects to it.
STEP THREE — Create & Configure Examinable Script
Project > Scripts > Create New Script → Examinable
ExaminationCube_PlacementPrefab > Add Component > Examinable
Examinable Component is used to allow the PlacementPrefab to communicate with the ExaminableManager.
public class Examinable : MonoBehaviour
{
[SerializeField]
private ExaminableManager _examinableManager;
private void Start()
{
_examinableManager = FindAnyObjectByType<ExaminableManager>();
}
public void RequestExamine()
{
_examinableManager.PerformExamine(this);
print("Examine has been requested");
}
}
_examinableManager → used to reference the ExaminableManager GameObject. Since this component will be on a prefab that will not be in the scene right away, we need to use the FindAnyObjectByType<>() to auto reference the ExaminableManager once the prefab enters the scene.
STEP FOUR — Configure SelectEntered() Logic
ExaminationCube_PlacementPrefab > AR Selection Interactable
SelectEntered() Interactable Events : Examinable.RequestExamine() → performs the process to enter the placementPrefab into examination mode.