コード例 #1
0
    /// <summary>
    /// 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.
    /// </summary>
    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 || parentImageTarget.ImageTarget == 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
        IEditorVirtualButtonBehaviour newEditorVBB = newVBB;

        newEditorVBB.SetVirtualButtonName(vbName);
        newEditorVBB.transform.localScale = new Vector3(localScale[0], 1.0f, localScale[1]);

        // Only register the virtual button with the qcarBehaviour at run-time:
        if (Application.isPlaying)
        {
            if (!parentImageTarget.CreateNewVirtualButtonFromBehaviour(newVBB))
            {
                return(null);
            }
        }

        // If we manually register the button it should be unregistered if the
        // Unity object is destroyed.
        newVBB.UnregisterOnDestroy = true;

        return(newVBB);
    }