Exemple #1
0
    void Check(Transform root)
    {
        // Load all tile prefab assets into a dict
        prefabDict = new Dictionary <string, GameObject>();
        List <GameObject> prefabs = EditorUtilityFunctions.GetTilePrefabs();

        foreach (GameObject prefab in prefabs)
        {
            string prefabKey = prefab.name.Substring(EditorUtilityFunctions.tilePrefix.Length); // Get the part after Tile_, like WALL
            prefabDict.Add(prefabKey, prefab);
        }

        // Parse input txt file
        string[] prefabByIndexArray = tilePrefabsMap.text.Split(new char[] { ' ', '\n' }, System.StringSplitOptions.RemoveEmptyEntries);

        // Check to see if any new prefabs are needed
        prefabTypesNeeded = new List <string>();
        foreach (string prefabType in prefabByIndexArray)
        {
            if (prefabType == "NULL" || prefabDict.ContainsKey(prefabType) || prefabTypesNeeded.Contains(prefabType))
            {
                continue;
            }
            prefabTypesNeeded.Add(prefabType);
        }

        changesToBeMade = new Dictionary <string, List <GameObject> >();
        foreach (Transform room in root)
        {
            foreach (Transform child in room)
            {
                if (!child.name.StartsWith(EditorUtilityFunctions.tilePrefix)) // Only worrying about tiles
                {
                    continue;
                }
                SpriteRenderer childSpriteRenderer = child.GetComponent <SpriteRenderer>();
                if (childSpriteRenderer == null)
                {
                    continue;
                }
                string idAsString = childSpriteRenderer.sprite.name.Substring(EditorUtilityFunctions.spriteSheetIDPrefix.Length);
                int    id         = int.Parse(idAsString);

                GameObject currentPrefab = (GameObject)PrefabUtility.GetPrefabParent(child.gameObject);
                string     currentType   = currentPrefab.name.Substring(EditorUtilityFunctions.tilePrefix.Length);
                string     targetType    = prefabByIndexArray[id];

                if (currentType == targetType)
                {
                    continue;
                }
                if (!changesToBeMade.ContainsKey(targetType))
                {
                    changesToBeMade.Add(targetType, new List <GameObject>());
                }
                changesToBeMade[targetType].Add(child.gameObject);
            }
        }
    }
    void OnGUI()
    {
        // Check for errors
        if (EditorUtilityFunctions.GetRoomPrefab() != null)// If room prefab exists
        {
            GUILayout.Label("Room prefab already exists!\nScript will not rerun in order to ensure this prefab isn't overwritten. Delete the prefab if you want to regenerate the map.", EditorStyles.boldLabel);
        }
        else if (EditorUtilityFunctions.GetTilePrefabs().Count > 0)// If any Tiles exist
        {
            GUILayout.Label("Tile prefabs already exist!\nScript will not rerun in order to ensure these prefabs aren't overwritten. Delete the prefabs if you want to regenerate the map.", EditorStyles.boldLabel);
        }
        else if (GameObject.Find("Level") != null)
        {
            GUILayout.Label("This scene already has a Level GameObject!\nThe script will not continue.", EditorStyles.boldLabel);
        }
        else
        {
            GUILayout.Label("Generate Map Into Current Scene", EditorStyles.boldLabel);
            GUILayout.Label("", EditorStyles.boldLabel);
            inputMap     = (Texture2D)EditorGUILayout.ObjectField("Input Map Texture:", inputMap, typeof(Texture2D), false);
            templateGame = (TemplateGame)EditorGUILayout.EnumPopup("Game:", templateGame);
            if (templateGame == TemplateGame.metroid)
            {
                metroidRooms = (TextAsset)EditorGUILayout.ObjectField("Room Grouping:", metroidRooms, typeof(TextAsset), false);
            }

            if (inputMap == null)
            {
                GUILayout.Label("Input map texture not provided!\nAdd an input map to run the script!", EditorStyles.boldLabel);
            }
            else if (templateGame == TemplateGame.metroid && metroidRooms == null)
            {
                GUILayout.Label("Room groupings not provided!\nAdd room groupings to continue.", EditorStyles.boldLabel);
            }
            else if (GUILayout.Button("Generate!"))
            {
                Texture2D newSpriteSheet;
                int[,] mapAsTileIndices;
                Parse(out newSpriteSheet, out mapAsTileIndices);

                Generate(newSpriteSheet, mapAsTileIndices);

                Debug.Log("Map Generation Successful! Check your hierarchy view and _GeneratedAssets folder for new content.");
                this.Close();
            }
        }
    }