private DataSet GetDataSet() { ImageTargetBehaviour itb = GetImageTarget(); if (itb == null) { Debug.LogError("Virtual Button is not part of a data set."); return(null); } DataSet container = null; ImageTracker imageTracker = (ImageTracker) TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER); if (imageTracker != null) { int numDataSets = imageTracker.GetNumDataSets(); for (int i = 0; i < numDataSets; ++i) { DataSet dataSet = imageTracker.GetDataSet(i); if (itb.References(dataSet)) { container = dataSet; } } } return(container); }
// This methods adds the Virtual Button as a child of "immediateParent". // Returns null if "immediateParent" is not an Image Target or a child of an // Image Target. public static VirtualButtonBehaviour CreateVirtualButton(string vbName, Vector2 localScale, GameObject immediateParent) { GameObject virtualButtonObject = new GameObject(vbName); VirtualButtonBehaviour newVBB = virtualButtonObject.AddComponent <VirtualButtonBehaviour>(); GameObject rootParent = immediateParent.transform.root.gameObject; ImageTargetBehaviour parentImageTarget = rootParent.GetComponentInChildren <ImageTargetBehaviour>(); if (parentImageTarget == null) { Debug.LogError("Could not create Virtual Button. " + "immediateParent\"immediateParent\" object is not " + "an Image Target or a child of one."); GameObject.Destroy(virtualButtonObject); return(null); } // Add Virtual Button to its parent game object virtualButtonObject.transform.parent = immediateParent.transform; // Set Virtual Button attributes newVBB.InitializeName(vbName); newVBB.transform.localScale = new Vector3(localScale[0], 1.0f, localScale[1]); ImageTracker imageTracker = (ImageTracker) TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER); int numDataSets = imageTracker.GetNumDataSets(); bool registerSuccess = false; for (int i = 0; i < numDataSets; ++i) { DataSet dataSet = imageTracker.GetDataSet(i); if (parentImageTarget.References(dataSet)) { registerSuccess = dataSet.RegisterVirtualButton(newVBB, parentImageTarget.TrackableName); } } if (!registerSuccess) { Debug.LogError("Could not register Virtual Button."); GameObject.Destroy(virtualButtonObject); return(null); } else { Debug.Log("Registered Virtual Button successfully."); } // If we manually register the button it should be unregistered if the // Unity object is destroyed. newVBB.UnregisterOnDestroy = true; return(newVBB); }
private void AddVirtualButtons(VirtualButtonBehaviour[] vbBehaviours) { for (int i = 0; i < vbBehaviours.Length; ++i) { VirtualButtonBehaviour virtualButton = vbBehaviours[i]; if (virtualButton.VirtualButtonName == null) { Debug.LogError("VirtualButton at " + i + " has no name."); continue; } ImageTargetBehaviour imageTarget = virtualButton.GetImageTarget(); if (imageTarget == null) { Debug.LogError("VirtualButton named " + virtualButton.VirtualButtonName + " is not attached to an ImageTarget."); continue; } // Image Target is not part of this data set. if (!imageTarget.References(this)) { continue; } int id = virtualButtonGetId(DataSetPtr, imageTarget.TrackableName, virtualButton.VirtualButtonName); if (id == -1) { // Create the virtual button if (RegisterVirtualButton(virtualButton, imageTarget.TrackableName)) { Debug.Log("Successfully created virtual button " + virtualButton.VirtualButtonName + " at startup"); virtualButton.UnregisterOnDestroy = true; id = virtualButtonGetId(DataSetPtr, imageTarget.TrackableName, virtualButton.VirtualButtonName); } else { Debug.LogError("Failed to create virtual button " + virtualButton.VirtualButtonName + " at startup"); } } if (id != -1) { // Duplicate check: if (!mVBBehaviourDict.ContainsKey(id)) { // OK: virtualButton.InitializeID(id); mVBBehaviourDict.Add(id, virtualButton); Debug.Log("Found VirtualButton named " + virtualButton.VirtualButtonName + " with id " + virtualButton.ID); // Handle any changes to the virtual button in the scene // that are not reflected in the config file virtualButton.UpdatePose(); if (!virtualButton.UpdateAreaRectangle(this) || !virtualButton.UpdateSensitivity(this)) { Debug.LogError("Failed to update virtual button " + virtualButton.VirtualButtonName + " at startup"); } else { Debug.Log("Updated virtual button " + virtualButton.VirtualButtonName + " at startup"); } } } } }