Toast on Image Detection in Vuforia either using U

2019-08-15 12:46发布

I have to Toast a message when an Image is detected. Which is the easiest way to do? Using Unity or just using JAVA without native calls?

I've tried all the ways given in the developer.vuforia.com site

I've tried 1. How To Extend the Unity Android Activity 2. How To Use Android Plugins in Unity Apps 3. How To Add Views Over Unity for Android 4. How To Display Toast on Target Detection and Open Website

But nothing works.. Please guide me or send a link of a working sample

2条回答
家丑人穷心不美
2楼-- · 2019-08-15 13:24

I really appreciate your efforts . but there is very simple way to do this:

  • Try this script

    using UnityEngine; using System.Collections;

    public class ShowToast : MonoBehaviour {
    
        // Use this for initialization
        void Start () {
            MyShowToastMethod ();
    
        }
    
        string toastString;
        AndroidJavaObject currentActivity;
    
        public void MyShowToastMethod ()
        {
            if (Application.platform == RuntimePlatform.Android) {
                showToastOnUiThread ("It Worked!");
            }
        }
    
        void showToastOnUiThread(string toastString){
            AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    
            currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            this.toastString = toastString;
    
            currentActivity.Call ("runOnUiThread", new AndroidJavaRunnable (showToast));
        }
    
        void showToast(){
            Debug.Log ("Running on UI thread");
            AndroidJavaObject context = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
            AndroidJavaClass Toast = new AndroidJavaClass("android.widget.Toast");
            AndroidJavaObject javaString=new AndroidJavaObject("java.lang.String",toastString);
            AndroidJavaObject toast = Toast.CallStatic<AndroidJavaObject> ("makeText", context, javaString, Toast.GetStatic<int>("LENGTH_SHORT"));
            toast.Call ("show");
        }
    
    }
    
查看更多
Explosion°爆炸
3楼-- · 2019-08-15 13:24

I am using unity extension but i m pretty sure core codabase is the same. Image target uses the class called DefaultTrackableEventHandler and there is OnTrackingFound() function. This is called once the image is detected so you can implement your message in there! This is how I do it anyways. Good luck

查看更多
登录 后发表回答