Example #1
0
 /// <summary>
 /// Set Current Objective and set UI to match the current objective.
 /// </summary>
 private void Start()
 {
     currentObjective      = levelManager.currentLevel.objective;
     objectiveText.text    = currentObjective.amount.ToString();
     objectiveImage.sprite = currentObjective.objective.sprite;
     objectiveImage.color  = new Color(1, 1, 1, 255);
     progressBar.maxValue  = currentObjective.amount;
 }
Example #2
0
    void OnGUI()
    {
        string[] levelNames = GetLevelNames();
        GUILayout.Label("Level Publisher", EditorStyles.boldLabel);

        selectedIndex         = EditorGUILayout.Popup("Levels", selectedIndex, levelNames);
        previousSelectedIndex = selectedIndex;
        if (selectedIndex != 0)
        {
            levelName = levelNames[selectedIndex];
        }

        levelName = GUILayout.TextField(levelName);

        if (levelToEdit)
        {
            if (selectedIndex == 0 || levelToEdit.levelName != levelName)
            {
                levelToEdit           = null;
                existingLevelLoaded   = false;
                objectives            = GetAllObjectives();
                labelForCubeNetObject = new Dictionary <string, GameObject>();
                if (levelScene.IsValid())
                {
                    EditorSceneManager.CloseScene(levelScene, true);
                }
            }
        }
        if (levelToEdit == null && selectedIndex != 0)
        {
            foreach (LevelObject level in GetAllLevels())
            {
                if (level.levelName == levelName)
                {
                    levelToEdit = level;
                    break;
                }
            }
            if (levelToEdit)
            {
                InitFromLevel(levelToEdit);
            }
        }
        GUILayout.Label("Create Cube Nets", EditorStyles.boldLabel);
        GUILayout.BeginHorizontal();
        foreach (CubeNet cubeNet in cubeNets)
        {
            Texture2D croppedTexture = textureForCubeNet[cubeNet];

            if (GUILayout.Button(croppedTexture, GUILayout.Width(40), GUILayout.Height(40)))
            {
                CreateCubeNet(cubeNet);
            }
        }
        GUILayout.EndHorizontal();

        GUILayout.Label("Current Cube Nets", EditorStyles.boldLabel);
        unfoldedCubeNets = EditorGUILayout.Foldout(unfoldedCubeNets, "Current Cube Nets");
        string keyToDelete = "";

        if (unfoldedCubeNets)
        {
            foreach (string label in labelForCubeNetObject.Keys)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(label);
                if (GUILayout.Button("Select"))
                {
                    GameObject   parentObject = labelForCubeNetObject[label];
                    GameObject[] selection    = new GameObject[1];
                    selection[0]      = parentObject;
                    Selection.objects = selection;
                }
                if (GUILayout.Button("Remove"))
                {
                    keyToDelete = label;
                    DestroyImmediate(labelForCubeNetObject[label]);
                }
                EditorGUILayout.EndHorizontal();
            }
            if (keyToDelete != "")
            {
                labelForCubeNetObject.Remove(keyToDelete);
            }
        }

        foreach (SerializedObject objective in objectives)
        {
            bool toggled = false;
            if (toggleForObjective.ContainsKey(objective))
            {
                toggled = toggleForObjective[objective];
            }
            if (levelToEdit)
            {
                foreach (ObjectiveObject existingObjective in levelToEdit.objectives)
                {
                    if (objective.targetObject == existingObjective)
                    {
                        toggled = true;
                    }
                }
            }
            SerializedProperty props = objective.GetIterator();
            toggled = EditorGUILayout.BeginToggleGroup(objective.targetObject.ToString(), toggled);
            toggleForObjective[objective] = toggled;
            while (props.NextVisible(true))
            {
                if (objective.targetObject.GetType() == typeof(TotalCubesObjective))
                {
                    if (props.name == "totalCubes" && levelToEdit == null)
                    {
                        props.intValue = labelForCubeNetObject.Keys.Count;
                    }
                }
                if (props.name == "m_Script")
                {
                    continue;
                }
                EditorGUILayout.PropertyField(props, true);
            }
            EditorGUILayout.EndToggleGroup();
            objective.ApplyModifiedProperties();
        }
        if (GUILayout.Button("Publish Level"))
        {
            string path = "Assets/Levels";
            string guid = AssetDatabase.CreateFolder(path, String.Format("Level_{0}", levelName));
            path = AssetDatabase.GUIDToAssetPath(guid);
            string      levelAssetPath   = path + "/LevelObject";
            string      assetPathAndName = levelAssetPath + ".asset";
            LevelObject newLevel         = CreateInstance <LevelObject>();
            newLevel.levelName = levelName;

            string scenesPath   = "Assets/Scenes";
            string newSceneGuid = AssetDatabase.CreateFolder(scenesPath, String.Format("Level_{0}", levelName));
            scenesPath = AssetDatabase.GUIDToAssetPath(newSceneGuid);
            EditorSceneManager.SaveScene(new_scene, scenesPath + "/LevelScene.unity");
            newLevel.levelPath = new_scene.path;
            bool inEditorSettings = false;
            foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
            {
                if (scene.path == new_scene.path)
                {
                    inEditorSettings = true;
                }
            }
            if (!inEditorSettings)
            {
                List <EditorBuildSettingsScene> allScenes = EditorBuildSettings.scenes.ToList <EditorBuildSettingsScene>();
                allScenes.Add(new EditorBuildSettingsScene(new_scene.path, true));
                EditorBuildSettings.scenes = allScenes.ToArray();
            }
            foreach (KeyValuePair <string, GameObject> entry in labelForCubeNetObject)
            {
                foreach (Transform child in entry.Value.transform)
                {
                    Vector3 new_pos = child.position;
                    new_pos.x      = RoundToNearestHalf(new_pos.x);
                    new_pos.y      = RoundToNearestHalf(new_pos.y);
                    new_pos.z      = RoundToNearestHalf(new_pos.z);
                    child.position = new_pos;
                }
            }
            newLevel.cubeNetNames = labelForCubeNetObject.Keys.ToArray();
            List <ObjectiveObject> toggledObjectives = new List <ObjectiveObject>();
            foreach (SerializedObject objective in objectives)
            {
                if (toggleForObjective.ContainsKey(objective))
                {
                    if (toggleForObjective[objective])
                    {
                        ObjectiveObject instance           = serializedObjectForInstance[objective] as ObjectiveObject;
                        string          objectiveName      = instance.ToString().Replace(" (", "").Replace(")", "");
                        string          objectiveAssetPath = path + "/" + objectiveName + ".asset";
                        AssetDatabase.CreateAsset(instance, objectiveAssetPath);
                        ObjectiveObject objectiveAsset = AssetDatabase.LoadAssetAtPath(objectiveAssetPath, typeof(ObjectiveObject)) as ObjectiveObject;
                        toggledObjectives.Add(objectiveAsset);
                    }
                }
            }
            newLevel.objectives = toggledObjectives.ToArray();
            AssetDatabase.CreateAsset(newLevel, assetPathAndName);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.FocusProjectWindow();
            EditorSceneManager.CloseScene(new_scene, true);
            InitVars();
        }
    }