Unity和Vuforia:目標圖像ID和GUI Windows - Unity and Vuforia: Target Image ID and GUI Windows -开发者知识库

Unity和Vuforia:目標圖像ID和GUI Windows - Unity and Vuforia: Target Image ID and GUI Windows -开发者知识库,第1张

I'm using Unity and Vuforia to make a secret project for my tabletop RPG group. Right now I'm running into an issue of lack of references and removed web pages on Vuforia's site.


Currently, my issue is this: I want to track up to 5 target images and, when scanned, have each of them to create their own individual GUI Window that tracks with the image in the UI. I have part of it working, I can track all the images, but only 1 window appears at a time. I know the actual images work because I have placeholder 3d objects included for debugging.


The script I used for the base for the GUI Windows: https://developer.vuforia.com/forum/faq/unity-how-can-i-popup-gui-button-when-target-detected

我用作GUI Windows基礎的腳本:https://developer.vuforia.com/forum/faq/unity-how-can-i-popup-gui-button-when-target-detected

I think my problem lies in the OnTrackableStateChanged() (Lines 19-32 link; 28-43 my code below). I need to alter it so it can take into account multiple target images. Then again I could be SUPER wrong because any references on Vuforia's API site or forums has been removed. Does anyone have a possible idea on what I can do to accomplish my goal? I'm currently thinking using the getID() function and then, somehow, having OnTrackableStateChange() check if the changed ID was different than the original idea. But again, I could be overlooking a simpler solution.


If this isn't clear I can try to be elaborate a bit more. Below is the code I'm using that uses components from the link above. Thank you for the help. :

如果不清楚,我可以嘗試更詳細一點。下面是我正在使用的代碼,它使用上面鏈接中的組件。感謝您的幫助。 :

using UnityEngine;
using System.Collections;

public class ButtonPopup : MonoBehaviour, Vuforia.ITrackableEventHandler

    private Vuforia.TrackableBehaviour mTrackableBehaviour;

    private bool mShowGUIWindow = false;
    private TargetScreenCoords coordinateScript;
    public int ID;
    private Rect mWindowRect;
    public string Name;
    public int Health;
    public string Status;
    public bool Villain;

    void Start()
        mTrackableBehaviour = GetComponent<Vuforia.TrackableBehaviour>();
        if (mTrackableBehaviour)

    public void OnTrackableStateChanged(
                                    Vuforia.TrackableBehaviour.Status previousStatus,
                                    Vuforia.TrackableBehaviour.Status newStatus)

        if (newStatus == Vuforia.TrackableBehaviour.Status.DETECTED ||
            newStatus == Vuforia.TrackableBehaviour.Status.TRACKED)
            mShowGUIWindow = true;
            mShowGUIWindow = false;


    void OnGUI()
        coordinateScript = GetComponent<TargetScreenCoords>();
        mWindowRect = new Rect(coordinateScript.screenPoint.x, Mathf.Abs(coordinateScript.screenPoint.y), 120, 100);

        if (Villain == true)
            GUI.backgroundColor = Color.red;
        if (Villain == false)
            GUI.backgroundColor = Color.green;
        if (mShowGUIWindow)
            mWindowRect = GUI.Window(ID, mWindowRect, DoMyWindow, "ID:"   ID);

    void DoMyWindow(int windowID)
        GUI.Label(new Rect(10, 20, 100, 60), new GUIContent("Name: "   Name   "\nHealth: "   Health   "\nStatus: "   Status));
        if (GUI.Button(new Rect(10, 70, 100, 20), "Edit Button"))
            print("Got a click");
            Villain = !Villain;


Thanks again for the help/ideas.


1 个解决方案



The first question is, if you want to track those 5 targets simultaneously? If yes, remember that you need to specify it in the ARCamera configuration. Set Max Simultaneous Tracked Images to 5 under the ARCamer object in scene.

第一個問題是,如果你想同時追蹤這5個目標嗎?如果是,請記住您需要在ARCamera配置中指定它。在場景中的ARCamer對象下將Max Simultaneous Tracked Images設置為5。

Under each ImageTarget you should also have a Trackable Event Handler script. By default it is DefaultTrackableEventHandler. In this script you have 2 private methods OnTrackingFound() and OnTrackingLost(). Try to add there 3rd foreach loop where you will enable/disable your GUI object type (Canvas or something like that).

在每個ImageTarget下,您還應該有一個Trackable Event Handler腳本。默認情況下,它是DefaultTrackableEventHandler。在此腳本中,您有2個私有方法OnTrackingFound()和OnTrackingLost()。嘗試添加第3個foreach循環,您將啟用/禁用GUI對象類型(Canvas或類似的東西)。