Search code examples
c#user-interfaceunity-game-engineaugmented-realityvuforia

Vuforia + Unity : Instantiate model in AR when UI Button pressed


I know this is a really simple question but I can't figure out how to archive this:

I have a UI button in my scene and I want Vuforia to instantiate one AR Model ONLY when I press a button.

Following the tutorial on the net I was able to instantiate a model on the screen when I touch it but I need to know how to set up Vuforia for archive the same result only when I press a button.

I have to disable the "Anchor Input Listener Behaviour"? And then?

I want to call for the PositionContentAtPlaneAnchor but I can't figure out how to call it in the right way in the OnClick field of the button. I need to make a custom script for this?

Thanks for any answer.


Solution

  • Ok, sorry for the delay.

    I deduce that you are working with the ground plane, if you have the Ground Plane Stage and the Plane Finder in the scene and works, we're at a good point.

    Now, you must only add a button to the scene and into the script add something like this:

    public PlaneFinderBehaviour plane;
    
    void Start()
    {
        ...
        buttonOnTheScene.onClick.AddListener(TaskOnClick);
        ...
    }
    
    void TaskOnClick()
    {
        Vector2 aPosition = new Vector2(0,0);
        ...
        plane.PerformHitTest(aPosition);
    }
    

    What does it mean?

    First of all, you must move the Plane Finder from Hierarchy to the script variable, so we have a reference to the plane into the script.

    Then when you click (or tap) on the button you simulate the click (or tap) on the display with the PerformHitTest.

    If you're wondering why my question in the comment, it's because the Plane Finder Behaviour Script has two modes type: Interactive and Automatic. The Interactive intercept the tap on the display and shows the object (on the ground plane) in the exact position of the tap, the automatic shows the object in the center of the plane.

    So if you want the object in an exact position you can pass a Vector2 position in the PerformHitTest and if you want to show an object programmatically or do something when it shows the object you can call a custom method OnInteractiveHitTest.

    That's all.