Toast on Image Detection in Vuforia either using Unity or Java without C++

丶灬走出姿态 提交于 2019-12-24 13:43:22

问题


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


回答1:


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");
        }
    
    }
    



回答2:


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



来源:https://stackoverflow.com/questions/35503237/toast-on-image-detection-in-vuforia-either-using-unity-or-java-without-c

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