Ejemplo n.º 1
0
    private void OnDestroy()
    {
        if (target != null && ItemBeingConnected == (PVCItem)target)
        {
            ItemBeingConnected = null;
        }

        PersistentGizmos.CleanUp(target);
    }
Ejemplo n.º 2
0
    protected virtual void OnSceneGUI()
    {
        if (targets.Length != 1)
        {
            return;
        }

        var myItem = (PVCItem)target;

        //Only need to do scene GUI stuff if this item is being connected to something.
        if (myItem != ItemBeingConnected)
        {
            return;
        }

        //Intercept mouse events.
        if (Event.current.type == EventType.Layout)
        {
            HandleUtility.AddDefaultControl(0);
        }

        var mouth = myItem.Mouths[ConnectIndex];

        //Draw a sphere around the end being connected.
        PersistentGizmos.Sphere(myItem, 0, mouth.MyTr.position, 1.0f,
                                new Color(0.25f, 1.0f, 0.25f, 0.325f));

        //See if a compatible item is being moused over.
        PVCItem selectedPvcItem;
        int     selectedMouthI;

        FindMousedOverItem(myItem.CompatibleObjects, out selectedPvcItem, out selectedMouthI);
        if (selectedMouthI >= 0)
        {
            //Draw a sphere around the mouth.
            PersistentGizmos.Sphere(myItem, 1,
                                    selectedPvcItem.Mouths[selectedMouthI].MyTr.position, 1.0f,
                                    new Color(1.0f, 0.25f, 0.25f, 0.325f));

            //If the mouse clicks, choose the connector mouth.
            if (Event.current.type == EventType.MouseUp && Event.current.button == 0)
            {
                //Push the state onto the Undo stack before modifying it.
                var itemsBeingChanged = new List <UnityEngine.Object>();
                var parent1           = myItem.MyTr.parent;
                itemsBeingChanged.AddRange(GetHierarchyFromRoot(parent1));
                var parent2 = selectedPvcItem.MyTr.parent;
                if (parent2 != parent1)
                {
                    itemsBeingChanged.AddRange(GetHierarchyFromRoot(parent2));
                }

                //Make the connection.
                //TODO: Doesn't always work?
                selectedPvcItem.Mouths[selectedMouthI].OtherItem       = myItem;
                selectedPvcItem.Mouths[selectedMouthI].OtherItemMouthI = ConnectIndex;
                var prop_thisMouth = serializedObject.FindProperty("mouths").GetArrayElementAtIndex(ConnectIndex);
                prop_thisMouth.FindPropertyRelative("OtherItem").objectReferenceValue = selectedPvcItem;
                prop_thisMouth.FindPropertyRelative("OtherItemMouthI").intValue       = selectedMouthI;
                //myItem.Mouths[ConnectIndex].OtherItem = selectedPvcItem;
                //myItem.Mouths[ConnectIndex].OtherItemMouthI = selectedMouthI;

                //Merge the groups.
                if (parent1 != parent2)
                {
                    while (parent2.childCount > 0)
                    {
                        parent2.GetChild(0).SetParent(parent1, true);
                    }
                    DestroyImmediate(parent2.gameObject);
                }

                //Clean up.
                ItemBeingConnected = null;
                PersistentGizmos.CleanUp(myItem);
                serializedObject.ApplyModifiedProperties();
                myItem.UpdateTransform();
                Repaint();
            }
        }
    }