Load AR-model through button tap in Android

梦想的初衷 提交于 2019-12-18 05:11:19

问题


I have made a basic AR-app using Unity & Vuforia and exported it to Android, so I can add some activities there. When I scan the image with the AR-camera in the app, a model pops up. Nothing new there.

What I want to achieve, is that when a user taps a button in some activity, lets say the text on the button is "Elephant", the AR-camera in the app opens, scans the image and loads a model of an Elephant.

My question is: is this possible? Is it possible to load a model depending on the user-input? And if this is possible, what would be the best place to search for some documentation about it? I've already searched on the internet, but can't find anything this specific. I'm also new to Unity & Vuforia, so I don't know that much about the program. Only the things some tutorials covered.

My apologies if this question has already been asked/answered.

Thanks in advance!


回答1:


Well, yes it is possible. And to make your app really use the AR feature you can implement the Virtual Button technique.

Honestly, when it comes to documentation, Qualcomm is useless. But since you use Unity with the Vuforia Extension, most features are luckily within the Unity Documentation.

I'll explain how you can achieve this in two ways:

1. Using Virtual Buttons (VB)

First off, start by adding/dropping a VB onto the scene from Assets>Qualcomm>Prefabs>Virtual Button. Make sure the VB is a child of the Image Target used.

Create a new script for the Image Target that defines what your VB needs to do.

You'll start with registering the buttons:

void Start () {
        VirtualButtonBehaviour[] vbs = transform.GetComponentsInChildren<VirtualButtonBehaviour> ();

        for (int i=0; i < vbs.Length; ++i) {
            vbs[i].RegisterEventHandler(this);
        }

And now you go ahead and start with the function of the button

public void OnButtonPressed(VirtualButtonAbstractBehaviour vb){
    //specify which button you want to function by using the if statement
    if(vb.name=="ButtonName") { callButtonfunction();}
    }

Similarly if you want a button to do something on release:

public void OnButtonReleased(VirtualButtonAbstractBehaviour vb){
    //specify which button you want to function by using the if statement
    if(vb.name=="ButtonName") { callButtonfunction();}
    }

In case you want your button to control a Gameobject, then go ahead and declare the GameObject as a public variable in the class so that it can be accessed in the Inspector and assigned accordingly.

public GameObject human;

Where GameObject is the variable type and human is the variable name that we use for reference

Now again, as per what you're trying to gain, there are two ways of assigning what the callbuttonfunction() can do.

  • Application.LoadLeve("Levelname")

This loads a new scene where the new object is loaded onto the same Image Target

  • Model.enabled(false) and Elephant.enabled(true)

This simply disabled the current model and enabled the Elepant Model. Setting these to public variables at the start of your class will make it easier to assign the models within Inspect element.

2. On-screen Buttons (OB)

  • If you want on-screen buttons, then just parent the button UI to the Camera and position in a particular corner of the screen or something.
  • Now if you function the button it will be within void Update() and you could use the scripting reference from Unity using the touch functions.

Really hope this helps you and everyone else who suffers from absolutely NO support from Qualcomm. Hit me up if you need more clarification.



来源:https://stackoverflow.com/questions/29522795/load-ar-model-through-button-tap-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!