/// <summary>
    /// This method creates a Virtual Button and adds it to this Image Target as
    /// a direct child.
    /// </summary>
    public VirtualButtonBehaviour CreateVirtualButton(string vbName,
                                                      Vector2 position,
                                                      Vector2 size)
    {
        GameObject             virtualButtonObject = new GameObject(vbName);
        VirtualButtonBehaviour newVBB =
            virtualButtonObject.AddComponent <VirtualButtonBehaviour>();

        // Add Virtual Button to its parent game object
        virtualButtonObject.transform.parent = this.transform;

        // Set Virtual Button attributes
        IEditorVirtualButtonBehaviour newEditorVBB = newVBB;

        newEditorVBB.SetVirtualButtonName(vbName);
        newEditorVBB.transform.localScale    = new Vector3(size.x, 1.0f, size.y);
        newEditorVBB.transform.localPosition = new Vector3(position.x, 1.0f,
                                                           position.y);

        // Only register the virtual button with the qcarBehaviour at run-time:
        if (Application.isPlaying)
        {
            if (!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);
    }
Esempio n. 2
0
    private static void AddVirtualButton(ImageTargetBehaviour it,
                                         ConfigData.VirtualButtonData vb)
    {
        IEditorVirtualButtonBehaviour newVBBehaviour =
            it.CreateVirtualButton(vb.name, new Vector2(0.0f, 0.0f),
                                   new Vector2(1.0f, 1.0f));

        if (newVBBehaviour != null)
        {
            newVBBehaviour.SetPosAndScaleFromButtonArea(
                new Vector2(vb.rectangle[0], vb.rectangle[1]),
                new Vector2(vb.rectangle[2], vb.rectangle[3]));

            VirtualButtonEditor.CreateVBMesh(newVBBehaviour);

            // Load default material.
            VirtualButtonEditor.CreateMaterial(newVBBehaviour);

            newVBBehaviour.enabled = vb.enabled;

            // Add Component to destroy VirtualButton meshes at runtime.
            newVBBehaviour.gameObject.AddComponent <TurnOffBehaviour>();

            // Make sure Virtual Button is correctly aligned with Image Target
            newVBBehaviour.UpdatePose();
        }
        else
        {
            Debug.LogError("VirtualButton could not be added!");
        }
    }
    // creates the specified VirtualButtonBehaviour for this ImageTarget
    private void CreateVirtualButtonFromNative(VirtualButton virtualButton)
    {
        GameObject             virtualButtonObject = new GameObject(virtualButton.Name);
        VirtualButtonBehaviour newVBB =
            virtualButtonObject.AddComponent <VirtualButtonBehaviour>();

        // We need to set the Image Target as a parent BEFORE we set the size
        // of the Virtual Button.
        newVBB.transform.parent = transform;

        IEditorVirtualButtonBehaviour newEditorVBB = newVBB;

        Debug.Log("Creating Virtual Button with values: " +
                  "\n ID:           " + virtualButton.ID +
                  "\n Name:         " + virtualButton.Name +
                  "\n Rectangle:    " + virtualButton.Area.leftTopX + "," +
                  virtualButton.Area.leftTopY + "," +
                  virtualButton.Area.rightBottomX + "," +
                  virtualButton.Area.rightBottomY);

        newEditorVBB.SetVirtualButtonName(virtualButton.Name);
        newEditorVBB.SetPosAndScaleFromButtonArea(new Vector2(virtualButton.Area.leftTopX, virtualButton.Area.leftTopY),
                                                  new Vector2(virtualButton.Area.rightBottomX, virtualButton.Area.rightBottomY));
        // This button is part of a data set and should therefore not be
        // unregistered in native only because the Unity object is destroyed.
        newEditorVBB.UnregisterOnDestroy = false;
        newEditorVBB.InitializeVirtualButton(virtualButton);
        mVirtualButtonBehaviours.Add(virtualButton.ID, newVBB);
    }
    private bool CreateNewVirtualButtonFromBehaviour(VirtualButtonAbstractBehaviour newVBB)
    {
        Vector2 vector;
        Vector2 vector2;

        newVBB.CalculateButtonArea(out vector, out vector2);
        RectangleData area = new RectangleData {
            leftTopX     = vector.x,
            leftTopY     = vector.y,
            rightBottomX = vector2.x,
            rightBottomY = vector2.y
        };
        VirtualButton virtualButton = this.mImageTarget.CreateVirtualButton(newVBB.VirtualButtonName, area);

        if (virtualButton == null)
        {
            UnityEngine.Object.Destroy(newVBB.gameObject);
            return(false);
        }
        IEditorVirtualButtonBehaviour behaviour = newVBB;

        behaviour.InitializeVirtualButton(virtualButton);
        this.mVirtualButtonBehaviours.Add(virtualButton.ID, newVBB);
        return(true);
    }
    // Correct Virtual Button Poses.
    public static bool CorrectPoses(IEditorVirtualButtonBehaviour[] vbs)
    {
        bool posesUpdated = false;
        foreach (IEditorVirtualButtonBehaviour vb in vbs)
        {
            // Check if Virtual Button pose has changed in scene or has never
            // been updated
            if (vb.PreviousTransform != vb.transform.localToWorldMatrix ||
                (vb.transform.parent != null && vb.PreviousParent !=
                vb.transform.parent.gameObject) ||
                !vb.HasUpdatedPose)
            {
                // Update the buttons pose
                if (vb.UpdatePose())
                {
                    // The button area has changed, need to serialize:
                    posesUpdated = true;
                }

                vb.SetPreviousTransform(vb.transform.localToWorldMatrix);
                vb.SetPreviousParent(vb.transform.parent ?
                            vb.transform.parent.gameObject : null);
            }
        }
        return posesUpdated;
    }
Esempio n. 6
0
    public static void CreateVBMesh(IEditorVirtualButtonBehaviour vb)
    {
        GameObject gameObject = vb.gameObject;
        MeshFilter component  = gameObject.GetComponent <MeshFilter>();

        if (component == null)
        {
            component = gameObject.AddComponent <MeshFilter>();
        }
        Vector3 vector  = new Vector3(-0.5f, 0f, -0.5f);
        Vector3 vector2 = new Vector3(-0.5f, 0f, 0.5f);
        Vector3 vector3 = new Vector3(0.5f, 0f, -0.5f);
        Vector3 vector4 = new Vector3(0.5f, 0f, 0.5f);
        Mesh    mesh    = new Mesh();

        mesh.vertices  = new Vector3[] { vector, vector2, vector3, vector4 };
        mesh.triangles = new int[] { 0, 1, 2, 2, 1, 3 };
        mesh.uv        = new Vector2[] { new Vector2(0f, 0f), new Vector2(1f, 0f), new Vector2(0f, 1f), new Vector2(1f, 1f) };
        mesh.normals   = new Vector3[mesh.vertices.Length];
        mesh.RecalculateNormals();
        mesh.name            = "VBPlane";
        component.sharedMesh = mesh;
        MeshRenderer renderer = gameObject.GetComponent <MeshRenderer>();

        if (renderer == null)
        {
            renderer = gameObject.AddComponent <MeshRenderer>();
        }
        renderer.sharedMaterial = (Material)AssetDatabase.LoadAssetAtPath("Assets/Editor/QCAR/VirtualButtonTextures/VirtualButtonPreviewMaterial.mat", typeof(Material));
        EditorUtility.UnloadUnusedAssets();
    }
    private bool CreateNewVirtualButtonFromBehaviour(VirtualButtonBehaviour newVBB)
    {
        // Calculate the button area:
        Vector2 leftTop, rightBottom;

        newVBB.CalculateButtonArea(out leftTop, out rightBottom);
        RectangleData area = new RectangleData
        {
            leftTopX     = leftTop.x,
            leftTopY     = leftTop.y,
            rightBottomX = rightBottom.x,
            rightBottomY = rightBottom.y
        };

        VirtualButton virtualButton = mImageTarget.CreateVirtualButton(newVBB.VirtualButtonName, area);

        if (virtualButton == null)
        {
            Destroy(newVBB.gameObject);
            return(false);
        }

        IEditorVirtualButtonBehaviour newEditorVBB = newVBB;

        newEditorVBB.InitializeVirtualButton(virtualButton);
        mVirtualButtonBehaviours.Add(virtualButton.ID, newVBB);
        return(true);
    }
Esempio n. 8
0
    private ConfigData CreateDataSetFromTrackables(TrackableBehaviour[] trackables)
    {
        if (trackables == null)
        {
            return(null);
        }
        ConfigData data = new ConfigData();

        foreach (TrackableBehaviour behaviour in trackables)
        {
            if (behaviour is DataSetTrackableBehaviour)
            {
                IEditorDataSetTrackableBehaviour behaviour2 = (DataSetTrackableBehaviour)behaviour;
                string dataSetName   = behaviour2.DataSetName;
                string trackableName = behaviour2.TrackableName;
                if (((dataSetName == "--- EMPTY ---") || (dataSetName == "")) || ((trackableName == "--- EMPTY ---") || (trackableName == "")))
                {
                    UnityEngine.Debug.LogWarning("Ignoring default Trackable for export");
                }
                else if (behaviour2 is ImageTargetAbstractBehaviour)
                {
                    ImageTargetAbstractBehaviour behaviour3 = (ImageTargetAbstractBehaviour)behaviour2;
                    IEditorImageTargetBehaviour  behaviour4 = behaviour3;
                    ConfigData.ImageTargetData   item       = new ConfigData.ImageTargetData {
                        size = behaviour4.GetSize()
                    };
                    VirtualButtonAbstractBehaviour[] componentsInChildren = behaviour3.GetComponentsInChildren <VirtualButtonAbstractBehaviour>();
                    item.virtualButtons = new List <ConfigData.VirtualButtonData>(componentsInChildren.Length);
                    foreach (VirtualButtonAbstractBehaviour behaviour5 in componentsInChildren)
                    {
                        Vector2 vector;
                        Vector2 vector2;
                        if (behaviour5.CalculateButtonArea(out vector, out vector2))
                        {
                            ConfigData.VirtualButtonData  data3      = new ConfigData.VirtualButtonData();
                            IEditorVirtualButtonBehaviour behaviour6 = behaviour5;
                            data3.name        = behaviour6.VirtualButtonName;
                            data3.enabled     = behaviour6.enabled;
                            data3.rectangle   = new Vector4(vector.x, vector.y, vector2.x, vector2.y);
                            data3.sensitivity = behaviour6.SensitivitySetting;
                            item.virtualButtons.Add(data3);
                        }
                    }
                    data.SetImageTarget(item, behaviour4.TrackableName);
                }
                else if (behaviour2 is MultiTargetAbstractBehaviour)
                {
                    UnityEngine.Debug.Log("Multi Targets not exported.");
                }
                else if (behaviour2 is CylinderTargetAbstractBehaviour)
                {
                    UnityEngine.Debug.Log("Cylinder Targets not exported.");
                }
            }
        }
        return(data);
    }
    // Create a mesh with size 1, 1.
    public static void CreateVBMesh(IEditorVirtualButtonBehaviour vb)
    {
        GameObject vbObject = vb.gameObject;

        MeshFilter meshFilter = vbObject.GetComponent <MeshFilter>();

        if (!meshFilter)
        {
            meshFilter = vbObject.AddComponent <MeshFilter>();
        }

        // Setup vertex positions.
        Vector3 p0 = new Vector3(-0.5f, 0, -0.5f);
        Vector3 p1 = new Vector3(-0.5f, 0, 0.5f);
        Vector3 p2 = new Vector3(0.5f, 0, -0.5f);
        Vector3 p3 = new Vector3(0.5f, 0, 0.5f);

        Mesh mesh = new Mesh();

        mesh.vertices  = new Vector3[] { p0, p1, p2, p3 };
        mesh.triangles = new int[]  {
            0, 1, 2,
            2, 1, 3
        };

        // Add UV coordinates.
        mesh.uv = new Vector2[] {
            new Vector2(0, 0),
            new Vector2(1, 0),
            new Vector2(0, 1),
            new Vector2(1, 1)
        };

        // Add empty normals array.
        mesh.normals = new Vector3[mesh.vertices.Length];

        // Automatically calculate normals.
        mesh.RecalculateNormals();
        mesh.name = "VBPlane";

        meshFilter.sharedMesh = mesh;

        MeshRenderer meshRenderer = vbObject.GetComponent <MeshRenderer>();

        if (!meshRenderer)
        {
            meshRenderer = vbObject.AddComponent <MeshRenderer>();
        }

        meshRenderer.sharedMaterial = (Material)AssetDatabase.LoadAssetAtPath(
            QCARUtilities.GlobalVars.VIRTUAL_BUTTON_MATERIAL_PATH,
            typeof(Material));

        EditorUtility.UnloadUnusedAssets();
    }
    // Create a mesh with size 1, 1.
    public static void CreateVBMesh(IEditorVirtualButtonBehaviour vb)
    {
        GameObject vbObject = vb.gameObject;

        MeshFilter meshFilter = vbObject.GetComponent<MeshFilter>();
        if (!meshFilter)
        {
            meshFilter = vbObject.AddComponent<MeshFilter>();
        }

        // Setup vertex positions.
        Vector3 p0 = new Vector3(-0.5f, 0, -0.5f);
        Vector3 p1 = new Vector3(-0.5f, 0, 0.5f);
        Vector3 p2 = new Vector3(0.5f, 0, -0.5f);
        Vector3 p3 = new Vector3(0.5f, 0, 0.5f);

        Mesh mesh = new Mesh();
        mesh.vertices = new Vector3[] { p0, p1, p2, p3 };
        mesh.triangles = new int[]  {
                                        0,1,2,
                                        2,1,3
                                    };

        // Add UV coordinates.
        mesh.uv = new Vector2[]{
                new Vector2(0,0),
                new Vector2(1,0),
                new Vector2(0,1),
                new Vector2(1,1)
                };

        // Add empty normals array.
        mesh.normals = new Vector3[mesh.vertices.Length];

        // Automatically calculate normals.
        mesh.RecalculateNormals();
        mesh.name = "VBPlane";

        meshFilter.sharedMesh = mesh;

        MeshRenderer meshRenderer = vbObject.GetComponent<MeshRenderer>();
        if (!meshRenderer)
        {
            meshRenderer = vbObject.AddComponent<MeshRenderer>();
        }

        meshRenderer.sharedMaterial = (Material)AssetDatabase.LoadAssetAtPath(
                        QCARUtilities.GlobalVars.VIRTUAL_BUTTON_MATERIAL_PATH,
                        typeof(Material));

        EditorUtility.UnloadUnusedAssets();
    }
Esempio n. 11
0
    public override void OnInspectorGUI()
    {
        EditorGUIUtility.LookLikeInspector();
        base.DrawDefaultInspector();
        VirtualButtonAbstractBehaviour target     = (VirtualButtonAbstractBehaviour)base.target;
        IEditorVirtualButtonBehaviour  behaviour2 = target;

        behaviour2.SetVirtualButtonName(EditorGUILayout.TextField("Name", behaviour2.VirtualButtonName, new GUILayoutOption[0]));
        behaviour2.SetSensitivitySetting((VirtualButton.Sensitivity)EditorGUILayout.EnumPopup("Sensitivity Setting", behaviour2.SensitivitySetting, new GUILayoutOption[0]));
        if (GUI.changed)
        {
            EditorUtility.SetDirty(target);
        }
    }
    private void CreateVirtualButtonFromNative(VirtualButton virtualButton)
    {
        GameObject gameObject = new GameObject(virtualButton.Name);
        VirtualButtonAbstractBehaviour behaviour = BehaviourComponentFactory.Instance.AddVirtualButtonBehaviour(gameObject);

        behaviour.transform.parent = base.transform;
        IEditorVirtualButtonBehaviour behaviour2 = behaviour;

        Debug.Log(string.Concat(new object[] { "Creating Virtual Button with values: \n ID:           ", virtualButton.ID, "\n Name:         ", virtualButton.Name, "\n Rectangle:    ", virtualButton.Area.leftTopX, ",", virtualButton.Area.leftTopY, ",", virtualButton.Area.rightBottomX, ",", virtualButton.Area.rightBottomY }));
        behaviour2.SetVirtualButtonName(virtualButton.Name);
        behaviour2.SetPosAndScaleFromButtonArea(new Vector2(virtualButton.Area.leftTopX, virtualButton.Area.leftTopY), new Vector2(virtualButton.Area.rightBottomX, virtualButton.Area.rightBottomY));
        behaviour2.UnregisterOnDestroy = false;
        behaviour2.InitializeVirtualButton(virtualButton);
        this.mVirtualButtonBehaviours.Add(virtualButton.ID, behaviour);
    }
Esempio n. 13
0
    public static void CreateMaterial(IEditorVirtualButtonBehaviour vb)
    {
        string   assetPath = "Assets/Editor/QCAR/VirtualButtonTextures/VirtualButtonPreviewMaterial.mat";
        Material material  = (Material)AssetDatabase.LoadAssetAtPath(assetPath, typeof(Material));

        if (material == null)
        {
            Debug.LogError("Could not find reference material at " + assetPath + " please reimport Unity package.");
        }
        else
        {
            vb.renderer.sharedMaterial = material;
            EditorUtility.UnloadUnusedAssets();
        }
    }
Esempio n. 14
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);
    }
    private static void AddVirtualButton(ImageTargetAbstractBehaviour it, ConfigData.VirtualButtonData vb)
    {
        IEditorVirtualButtonBehaviour behaviour = it.CreateVirtualButton(vb.name, new Vector2(0f, 0f), new Vector2(1f, 1f));

        if (behaviour != null)
        {
            behaviour.SetPosAndScaleFromButtonArea(new Vector2(vb.rectangle[0], vb.rectangle[1]), new Vector2(vb.rectangle[2], vb.rectangle[3]));
            VirtualButtonEditor.CreateVBMesh(behaviour);
            VirtualButtonEditor.CreateMaterial(behaviour);
            behaviour.enabled = vb.enabled;
            BehaviourComponentFactory.Instance.AddTurnOffBehaviour(behaviour.gameObject);
            behaviour.UpdatePose();
        }
        else
        {
            Debug.LogError("VirtualButton could not be added!");
        }
    }
    public VirtualButtonAbstractBehaviour CreateVirtualButton(string vbName, Vector2 position, Vector2 size)
    {
        GameObject gameObject = new GameObject(vbName);
        VirtualButtonAbstractBehaviour newVBB = BehaviourComponentFactory.Instance.AddVirtualButtonBehaviour(gameObject);

        gameObject.transform.parent = base.transform;
        IEditorVirtualButtonBehaviour behaviour2 = newVBB;

        behaviour2.SetVirtualButtonName(vbName);
        behaviour2.transform.localScale    = new Vector3(size.x, 1f, size.y);
        behaviour2.transform.localPosition = new Vector3(position.x, 1f, position.y);
        if (Application.isPlaying && !this.CreateNewVirtualButtonFromBehaviour(newVBB))
        {
            return(null);
        }
        newVBB.UnregisterOnDestroy = true;
        return(newVBB);
    }
    void IEditorImageTargetBehaviour.AssociateExistingVirtualButtonBehaviour(VirtualButtonAbstractBehaviour virtualButtonBehaviour)
    {
        VirtualButton virtualButtonByName = this.mImageTarget.GetVirtualButtonByName(virtualButtonBehaviour.VirtualButtonName);

        if (virtualButtonByName == null)
        {
            Vector2 vector;
            Vector2 vector2;
            virtualButtonBehaviour.CalculateButtonArea(out vector, out vector2);
            RectangleData area = new RectangleData {
                leftTopX     = vector.x,
                leftTopY     = vector.y,
                rightBottomX = vector2.x,
                rightBottomY = vector2.y
            };
            virtualButtonByName = this.mImageTarget.CreateVirtualButton(virtualButtonBehaviour.VirtualButtonName, area);
            if (virtualButtonByName != null)
            {
                Debug.Log("Successfully created virtual button " + virtualButtonBehaviour.VirtualButtonName + " at startup");
                virtualButtonBehaviour.UnregisterOnDestroy = true;
            }
            else
            {
                Debug.LogError("Failed to create virtual button " + virtualButtonBehaviour.VirtualButtonName + " at startup");
            }
        }
        if ((virtualButtonByName != null) && !this.mVirtualButtonBehaviours.ContainsKey(virtualButtonByName.ID))
        {
            IEditorVirtualButtonBehaviour behaviour = virtualButtonBehaviour;
            behaviour.InitializeVirtualButton(virtualButtonByName);
            this.mVirtualButtonBehaviours.Add(virtualButtonByName.ID, virtualButtonBehaviour);
            Debug.Log(string.Concat(new object[] { "Found VirtualButton named ", virtualButtonBehaviour.VirtualButton.Name, " with id ", virtualButtonBehaviour.VirtualButton.ID }));
            virtualButtonBehaviour.UpdatePose();
            if (!virtualButtonBehaviour.UpdateAreaRectangle() || !virtualButtonBehaviour.UpdateSensitivity())
            {
                Debug.LogError("Failed to update virtual button " + virtualButtonBehaviour.VirtualButton.Name + " at startup");
            }
            else
            {
                Debug.Log("Updated virtual button " + virtualButtonBehaviour.VirtualButton.Name + " at startup");
            }
        }
    }
    // Lets the user set sensitivity and name of a Virtual Button.
    public override void OnInspectorGUI()
    {
        EditorGUIUtility.LookLikeInspector();

        DrawDefaultInspector();

        VirtualButtonBehaviour        vb       = (VirtualButtonBehaviour)target;
        IEditorVirtualButtonBehaviour editorVB = vb;

        editorVB.SetVirtualButtonName(EditorGUILayout.TextField(
                                          "Name", editorVB.VirtualButtonName));

        editorVB.SetSensitivitySetting((VirtualButton.Sensitivity)EditorGUILayout.EnumPopup(
                                           "Sensitivity Setting", editorVB.SensitivitySetting));

        if (GUI.changed)
        {
            // Let Unity know that there is new data for serialization.
            EditorUtility.SetDirty(vb);
        }
    }
    // Assign default material to Virtual Button.
    public static void CreateMaterial(IEditorVirtualButtonBehaviour vb)
    {
        // Load reference material
        string referenceMaterialPath =
            QCARUtilities.GlobalVars.VIRTUAL_BUTTON_MATERIAL_PATH;
        Material referenceMaterial =
            (Material)AssetDatabase.LoadAssetAtPath(referenceMaterialPath,
                                                    typeof(Material));
        if (referenceMaterial == null)
        {
            Debug.LogError("Could not find reference material at " +
                           referenceMaterialPath +
                           " please reimport Unity package.");
            return;
        }

        // If the texture is null we simply assign a default material
        vb.renderer.sharedMaterial = referenceMaterial;

        // Cleanup assets that have been created temporarily.
        EditorUtility.UnloadUnusedAssets();
    }
    // Assign default material to Virtual Button.
    public static void CreateMaterial(IEditorVirtualButtonBehaviour vb)
    {
        // Load reference material
        string referenceMaterialPath =
            QCARUtilities.GlobalVars.VIRTUAL_BUTTON_MATERIAL_PATH;
        Material referenceMaterial =
            (Material)AssetDatabase.LoadAssetAtPath(referenceMaterialPath,
                                                    typeof(Material));

        if (referenceMaterial == null)
        {
            Debug.LogError("Could not find reference material at " +
                           referenceMaterialPath +
                           " please reimport Unity package.");
            return;
        }

        // If the texture is null we simply assign a default material
        vb.renderer.sharedMaterial = referenceMaterial;

        // Cleanup assets that have been created temporarily.
        EditorUtility.UnloadUnusedAssets();
    }
    public static VirtualButtonAbstractBehaviour CreateVirtualButton(string vbName, Vector2 localScale, GameObject immediateParent)
    {
        GameObject gameObject = new GameObject(vbName);
        VirtualButtonAbstractBehaviour newVBB = BehaviourComponentFactory.Instance.AddVirtualButtonBehaviour(gameObject);
        ImageTargetAbstractBehaviour   componentInChildren = immediateParent.transform.root.gameObject.GetComponentInChildren <ImageTargetAbstractBehaviour>();

        if ((componentInChildren == null) || (componentInChildren.ImageTarget == null))
        {
            Debug.LogError("Could not create Virtual Button. immediateParent\"immediateParent\" object is not an Image Target or a child of one.");
            UnityEngine.Object.Destroy(gameObject);
            return(null);
        }
        gameObject.transform.parent = immediateParent.transform;
        IEditorVirtualButtonBehaviour behaviour3 = newVBB;

        behaviour3.SetVirtualButtonName(vbName);
        behaviour3.transform.localScale = new Vector3(localScale[0], 1f, localScale[1]);
        if (Application.isPlaying && !componentInChildren.CreateNewVirtualButtonFromBehaviour(newVBB))
        {
            return(null);
        }
        newVBB.UnregisterOnDestroy = true;
        return(newVBB);
    }
Esempio n. 22
0
    /// <summary>
    /// Associates existing virtual button behaviour with virtualbuttons and creates new VirtualButtons if necessary
    /// </summary>
    void IEditorImageTargetBehaviour.AssociateExistingVirtualButtonBehaviour(VirtualButtonBehaviour virtualButtonBehaviour)
    {
        VirtualButton virtualButton = mImageTarget.GetVirtualButtonByName(virtualButtonBehaviour.VirtualButtonName);

        if (virtualButton == null)
        {
            Vector2 leftTop, rightBottom;
            virtualButtonBehaviour.CalculateButtonArea(out leftTop, out rightBottom);
            var area = new RectangleData
            {
                leftTopX     = leftTop.x,
                leftTopY     = leftTop.y,
                rightBottomX = rightBottom.x,
                rightBottomY = rightBottom.y
            };
            virtualButton = mImageTarget.CreateVirtualButton(virtualButtonBehaviour.VirtualButtonName, area);

            // Create the virtual button
            if (virtualButton != null)
            {
                Debug.Log("Successfully created virtual button " +
                          virtualButtonBehaviour.VirtualButtonName +
                          " at startup");

                virtualButtonBehaviour.UnregisterOnDestroy = true;
            }
            else
            {
                Debug.LogError("Failed to create virtual button " +
                               virtualButtonBehaviour.VirtualButtonName +
                               " at startup");
            }
        }

        if (virtualButton != null)
        {
            //  Duplicate check:
            if (!mVirtualButtonBehaviours.ContainsKey(virtualButton.ID))
            {
                // OK:
                IEditorVirtualButtonBehaviour editorVirtualButtonBehaviour = virtualButtonBehaviour;
                editorVirtualButtonBehaviour.InitializeVirtualButton(virtualButton);
                mVirtualButtonBehaviours.Add(virtualButton.ID, virtualButtonBehaviour);

                Debug.Log("Found VirtualButton named " +
                          virtualButtonBehaviour.VirtualButton.Name + " with id " +
                          virtualButtonBehaviour.VirtualButton.ID);

                // Handle any changes to the virtual button in the scene
                // that are not reflected in the config file
                virtualButtonBehaviour.UpdatePose();
                if (!virtualButtonBehaviour.UpdateAreaRectangle() ||
                    !virtualButtonBehaviour.UpdateSensitivity())
                {
                    Debug.LogError("Failed to update virtual button " +
                                   virtualButtonBehaviour.VirtualButton.Name +
                                   " at startup");
                }
                else
                {
                    Debug.Log("Updated virtual button " +
                              virtualButtonBehaviour.VirtualButton.Name +
                              " at startup");
                }
            }
        }
    }
Esempio n. 23
0
    // This method creates a single data set from the trackables provided.
    // The method ignores the data set property in TrackableBehaviour and
    // adds all Trackables to a single file.
    // Default Trackables are not added to the data set.
    private ConfigData CreateDataSetFromTrackables(TrackableBehaviour[] trackables)
    {
        // Sanity check.
        if (trackables == null)
        {
            return(null);
        }

        ConfigData sceneData = new ConfigData();

        foreach (TrackableBehaviour tb in trackables)
        {
            // Ignore non-data set trackables.
            if (!(tb is DataSetTrackableBehaviour))
            {
                continue;
            }

            IEditorDataSetTrackableBehaviour trackable = (DataSetTrackableBehaviour)tb;

            string dataSetName   = trackable.DataSetName;
            string trackableName = trackable.TrackableName;

            // We ignore default Trackables or undefined Trackables.
            if (dataSetName == QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME ||
                dataSetName == "" ||
                trackableName == QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME ||
                trackableName == "")
            {
                Debug.LogWarning("Ignoring default Trackable for export");
                continue;
            }

            if (trackable.GetType() == typeof(ImageTargetBehaviour))
            {
                ImageTargetBehaviour        it       = (ImageTargetBehaviour)trackable;
                IEditorImageTargetBehaviour editorIt = it;

                ConfigData.ImageTargetData itConfig = new ConfigData.ImageTargetData();

                itConfig.size = editorIt.GetSize();

                // Process Virtual Button list.
                VirtualButtonBehaviour[] vbs =
                    it.GetComponentsInChildren <VirtualButtonBehaviour>();
                itConfig.virtualButtons = new List <ConfigData.VirtualButtonData>(vbs.Length);
                foreach (VirtualButtonBehaviour vb in vbs)
                {
                    Vector2 leftTop;
                    Vector2 rightBottom;
                    if (!vb.CalculateButtonArea(out leftTop,
                                                out rightBottom))
                    {
                        // Invalid Button
                        continue;
                    }

                    ConfigData.VirtualButtonData vbConfig =
                        new ConfigData.VirtualButtonData();

                    IEditorVirtualButtonBehaviour editorVB = vb;
                    vbConfig.name      = editorVB.VirtualButtonName;
                    vbConfig.enabled   = editorVB.enabled;
                    vbConfig.rectangle = new Vector4(leftTop.x,
                                                     leftTop.y,
                                                     rightBottom.x,
                                                     rightBottom.y);
                    vbConfig.sensitivity = editorVB.SensitivitySetting;

                    itConfig.virtualButtons.Add(vbConfig);
                }

                sceneData.SetImageTarget(itConfig, editorIt.TrackableName);
            }
            else if (trackable.GetType() == typeof(MultiTargetBehaviour))
            {
                Debug.Log("Multi Targets not exported.");
            }
        }

        return(sceneData);
    }