Exemple #1
0
    public override void OnInspectorGUI()
    {
        SECTR_Sector mySector = (SECTR_Sector)target;

        base.OnInspectorGUI();
        List <SECTR_Member.Child> sharedChildren = mySector.GetSharedChildren();

        if (sharedChildren.Count > 0 && GUILayout.Button(new GUIContent("Fix Shared Children", "Adds Member components to any children that extend beyond this Sector and into other sectors.")))
        {
            MakeSharedChildrenMembers(mySector, sharedChildren, "Fix Shared Children");
        }

        if (mySector.GetComponentInChildren <Terrain>())
        {
            terrainFoldout = EditorGUILayout.Foldout(terrainFoldout, "Terrain Connections");
            if (terrainFoldout)
            {
                serializedObject.Update();
                DrawProperty("TopTerrain");
                DrawProperty("BottomTerrain");
                DrawProperty("LeftTerrain");
                DrawProperty("RightTerrain");

                if (GUILayout.Button("Reconnect Neighbors"))
                {
                    mySector.ConnectTerrainNeighbors();
                }
                serializedObject.ApplyModifiedProperties();
            }
        }
    }
Exemple #2
0
    public static void CreateSector()
    {
        List <GameObject> rootObjects     = new List <GameObject>(8);
        List <GameObject> selectedObjects = new List <GameObject>(Selection.gameObjects);
        int numSelectedObjects            = selectedObjects.Count;
        int selectedObjectIndex           = 0;

        while (selectedObjectIndex < numSelectedObjects)
        {
            GameObject gameObject = selectedObjects[selectedObjectIndex];
            // auto remove portals if selected.
            if (gameObject.GetComponent <SECTR_Portal>())
            {
                selectedObjects.RemoveAt(selectedObjectIndex);
                --numSelectedObjects;
            }
            else
            {
                // Check to make sure nothing selected is already in a Sector.
                Transform parent = gameObject.transform;
                while (parent != null)
                {
                    // Bail out if we encounter any classes that shouldn't be part of the list
                    if (parent.GetComponent <SECTR_Sector>() != null)
                    {
                        EditorUtility.DisplayDialog("Sector Safety First", "Some of the selected objects are already in a Sector. Please unparent them from the current Sector before putting them into a new Sector.", "Ok");
                        return;
                    }
                    parent = parent.parent;
                }
                ++selectedObjectIndex;
            }
        }

        // Build a list of common parents from the selection to try to preserve any existing heirarchy
        // but without walking all the way up the tree. Complex scenes may have many Sectors parented
        // under a single transform and we do not want to walk higher than the current selection.
        for (selectedObjectIndex = 0; selectedObjectIndex < numSelectedObjects; ++selectedObjectIndex)
        {
            GameObject gameObject = selectedObjects[selectedObjectIndex];
            Transform  parent     = gameObject.transform;
            while (parent != null)
            {
                if (parent.parent)
                {
                    int  numChildren         = parent.parent.childCount;
                    bool allChildrenSelected = true;
                    for (int childIndex = 0; childIndex < numChildren; ++childIndex)
                    {
                        if (!selectedObjects.Contains(parent.parent.GetChild(childIndex).gameObject))
                        {
                            allChildrenSelected = false;
                            break;
                        }
                    }
                    if (allChildrenSelected)
                    {
                        parent = parent.parent;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            if (parent != null && !rootObjects.Contains(parent.gameObject))
            {
                rootObjects.Add(parent.gameObject);
            }
        }

        int          numRootObjects = rootObjects.Count;
        string       newName        = "SECTR Sector";
        string       undoName       = "Create " + newName;
        SECTR_Sector newSector      = null;

        // If there is just one root, give the open to make that into a Sector. This helps
        // with scenes that are already well organized.
        Transform commonParent = numRootObjects == 1 ? rootObjects[0].transform : null;

        if (commonParent && EditorUtility.DisplayDialog("Common Parent Detected", "Selected objects are all a child of " + commonParent.name + ". " +
                                                        "Would you like to make " + commonParent.name + " into a Sector? If not, a new Sector will be created and " +
                                                        commonParent.name + " will be parented to it.", "Yes", "No"))
        {
            newSector = commonParent.gameObject.AddComponent <SECTR_Sector>();
            if (!commonParent.gameObject.isStatic &&
                EditorUtility.DisplayDialog("Make Sector Static?", "SECTR can perform additional optimizations on Sectors that are marked as static, provided they " +
                                            "do not need to move or change bounds at runtime. Would you like " + commonParent.name + " to be marked as static?", "Yes", "No"))
            {
                commonParent.gameObject.isStatic = true;
            }
            SECTR_Undo.Created(newSector, undoName);
        }
        else
        {
            commonParent = numRootObjects > 0 ? rootObjects[0].transform.parent : null;
            for (int rootIndex = 0; rootIndex < numRootObjects; ++rootIndex)
            {
                if (rootObjects[rootIndex].transform.parent != commonParent)
                {
                    commonParent = null;
                    break;
                }
            }

            GameObject newGameObject = CreateGameObject(newName);
            newGameObject.transform.parent = commonParent;
            newGameObject.isStatic         = true;
            SECTR_Undo.Created(newGameObject, undoName);

            List <Vector3> rootPositions = new List <Vector3>(numRootObjects);
            for (int rootObjectIndex = 0; rootObjectIndex < numRootObjects; ++rootObjectIndex)
            {
                GameObject gameObject = rootObjects[rootObjectIndex];
                rootPositions.Add(gameObject.transform.position);
                SECTR_Undo.Parent(newGameObject, gameObject, undoName);
            }

            newSector = newGameObject.AddComponent <SECTR_Sector>();
            SECTR_Undo.Created(newSector, undoName);

            newSector.transform.position = newSector.TotalBounds.center;
            for (int rootObjectIndex = 0; rootObjectIndex < numRootObjects; ++rootObjectIndex)
            {
                GameObject gameObject = rootObjects[rootObjectIndex];
                gameObject.transform.position = rootPositions[rootObjectIndex];
            }
        }

        List <SECTR_Member.Child> sharedChildren = newSector.GetSharedChildren();

        if (sharedChildren.Count > 0 && EditorUtility.DisplayDialog("Overlap Warning", "Some objects in this Sector overlap other Sectors, which may cause unexpected behavior. Would you like to make them Members instead of children?", "Yes", "No"))
        {
            SECTR_SectorEditor.MakeSharedChildrenMembers(newSector, sharedChildren, undoName);
        }

        Selection.activeGameObject = newSector.gameObject;
    }