Ejemplo n.º 1
0
    /// <summary>
    /// Create or destroy the virtual button with the given name.
    /// </summary>
    public void ToggleVirtualButton(string name)
    {
        if (mImageTargetWood.ImageTarget != null)
        {
            // Get the virtual button if it exists.
            VirtualButton vb = mImageTargetWood.ImageTarget.GetVirtualButtonByName(name);

            if (vb != null)
            {
                // Destroy the virtual button if it exists.
                mImageTargetWood.DestroyVirtualButton(name);
            }
            else
            {
                // Get the position and scale originally used for this virtual button.
                Vector3 position, scale;
                if (mVBPositionDict.TryGetValue(name, out position) &&
                    mVBScaleDict.TryGetValue(name, out scale))
                {
                    // Deactivate the dataset before creating the virtual button.
                    ObjectTracker objectTracker = TrackerManager.Instance.GetTracker <ObjectTracker>();
                    DataSet       dataSet       = objectTracker.GetActiveDataSets().First();
                    objectTracker.DeactivateDataSet(dataSet);

                    // Create the virtual button.
                    VirtualButtonBehaviour vbb = mImageTargetWood.CreateVirtualButton(
                        name,
                        new Vector2(position.x, position.z),
                        new Vector2(scale.x, scale.z)) as VirtualButtonBehaviour;

                    if (vbb != null)
                    {
                        // Register the button with the event handler on the Wood target.
                        vbb.RegisterEventHandler(mImageTargetWood.GetComponent <VirtualButtonEventHandler>());

                        // Add a mesh to outline the button.
                        CreateVBMesh(vbb);

                        // If the Wood target isn't currently tracked hide the button.
                        if (mImageTargetWood.CurrentStatus == TrackableBehaviour.Status.NOT_FOUND)
                        {
                            vbb.GetComponent <Renderer>().enabled = false;
                        }
                    }

                    // Reactivate the dataset.
                    objectTracker.ActivateDataSet(dataSet);
                }
            }
        }

        base.CloseMenu();
    }