public override void OnInspectorGUI()
 {
     DrawDefaultInspector();
     if (GUILayout.Button("Save Level"))
     {
         Level level = (Level)target;
         level.transform.position = Vector3.zero;
         level.transform.rotation = Quaternion.identity;
         var levelRoot  = GameObject.Find("Level");
         var ldr        = new LevelDataRepresentation();
         var levelItems = new List <LevelItemRepresentation> ();
         foreach (Transform t in levelRoot.transform)
         {
             var sr = t.GetComponent <SpriteRenderer> ();
             var li = new LevelItemRepresentation()
             {
                 position = t.position,
                 rotation = t.rotation.eulerAngles,
                 scale    = t.localScale
             };
             if (t.name.Contains(" "))
             {
                 li.prefabName = t.name.Substring(0, t.name.IndexOf(" "));
             }
             else
             {
                 li.prefabName = t.name;
             }
             if (sr != null)
             {
                 li.spriteLayer = sr.sortingLayerName;
                 li.spriteColor = sr.color;
                 li.spriteOrder = sr.sortingOrder;
             }
             levelItems.Add(li);
         }
         ldr.levelItems          = levelItems.ToArray();
         ldr.playerStartPosition = GameObject.Find("SoyBoy").transform.position;
         var currentCamSettings = FindObjectOfType <CameraLerpToTransform> ();
         if (currentCamSettings != null)
         {
             ldr.cameraSettings = new CameraSettingsRepresentation()
             {
                 cameraTrackTarget = currentCamSettings.camTarget.name,
                 cameraZDepth      = currentCamSettings.cameraZDepth,
                 minX          = currentCamSettings.minX,
                 minY          = currentCamSettings.minY,
                 maxX          = currentCamSettings.maxX,
                 maxY          = currentCamSettings.maxY,
                 trackingSpeed = currentCamSettings.trackingSpeed
             };
         }
         var levelDataToJson = JsonUtility.ToJson(ldr);
         var savePath        = System.IO.Path.Combine(Application.dataPath, level.levelName + ".json");
         System.IO.File.WriteAllText(savePath, levelDataToJson);
         Debug.Log("Level saved to " + savePath);
     }
 }
示例#2
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        if (GUILayout.Button("Save level"))
        {
            //Get refrence to level component and set its origin pos and rot
            Level level = (Level)target;
            level.transform.position = Vector3.zero;
            level.transform.rotation = Quaternion.identity;
            //Get a reference to the level Game Object
            var levelRoot = GameObject.Find("Level");
            //create new instances of the classes to store the level data
            var ldr        = new LevelDataRepresentation();
            var levelItems = new List <LevelItemRepresentation>();

            foreach (Transform t in levelRoot.transform)
            {
                //Loop thorugh all the child objects in the level object and get a refrence to its sprite render and set its position, scale and rotation
                var sr = t.GetComponent <SpriteRenderer>();
                var li = new LevelItemRepresentation()
                {
                    position = t.position,
                    rotation = t.rotation.eulerAngles,
                    scale    = t.localScale
                };
                //Get the child objects name and remove any empty space or numbers from cloned objects
                if (t.name.Contains(" "))
                {
                    li.prefabName = t.name.Substring(0, t.name.IndexOf(" "));
                }
                else
                {
                    li.prefabName = t.name;
                }
                //If teh level object has a sprite render attached get all relevent information from it
                if (sr != null)
                {
                    li.spriteLayer = sr.sortingLayerName;
                    li.spriteColor = sr.color;
                    li.spriteOrder = sr.sortingOrder;
                }

                levelItems.Add(li);
            }

            ///convert teh list of level items to an array and store in the level representqatin object
            ldr.levelItems          = levelItems.ToArray();
            ldr.playerStartPosition = GameObject.Find("SoyBoy").transform.position;
            //Finds the camera lerp script and saves its settings to the camera settings representive object
            var currentCamSettings = FindObjectOfType <CameraLerpToTransform>();
            if (currentCamSettings != null)
            {
                ldr.cameraSettings = new CameraSettingsRepresentation()
                {
                    cameratrackingTarget = currentCamSettings.camTarget.name,
                    cameraZDepth         = currentCamSettings.cameraZDepth,
                    minX          = currentCamSettings.minX,
                    minY          = currentCamSettings.minY,
                    maxX          = currentCamSettings.maxX,
                    maxY          = currentCamSettings.maxY,
                    trackingSpeed = currentCamSettings.trackingSpeed
                };
            }

            //Write the data created with the LevelDataRepresentation class to a .json file
            var levelDataToJson = JsonUtility.ToJson(ldr);
            var savePath        = System.IO.Path.Combine(Application.persistentDataPath, level.levelName + ".json");
            System.IO.File.WriteAllText(savePath, levelDataToJson);
            Debug.Log("Level saved to " + savePath);
        }
    }
示例#3
0
 public override void OnInspectorGUI()
 {
     //change the way the inspectro behaves
     DrawDefaultInspector();
     //add a save level button
     if (GUILayout.Button("Save Level"))
     {
         //get level info, make sure level root is at origin
         Level level = (Level)target;
         level.transform.position = Vector3.zero;
         level.transform.rotation = Quaternion.identity;
         var levelRoot = GameObject.Find("Level"); //get the root
                                                   //setup the structure to save info
         var ldr        = new LevelDataRepresentation();
         var levelItems = new List <LevelItemRepresentation>();
         //loop through the items to get the needed info
         foreach (Transform t in levelRoot.transform)
         {
             //get a reference to sprite renderer
             SpriteRenderer          sr = t.GetComponent <SpriteRenderer>();
             LevelItemRepresentation li = new LevelItemRepresentation()
             {
                 position = t.position,
                 rotation = t.rotation.eulerAngles,
                 scale    = t.localScale
             };
             //get the prefab name, try to hedge for spaces in duplicates
             if (t.name.Contains(" "))
             {
                 //get the name before the space
                 li.prefabName = t.name.Substring(0, t.name.IndexOf(" ", System.StringComparison.CurrentCulture));//very progressive
             }
             else if (t.name.Contains("(Clone)"))
             {
                 //make sure we aren't saving clones
                 li.prefabName = t.name.Substring(0, t.name.IndexOf("(", System.StringComparison.CurrentCulture));
             }
             else
             {
                 //if no spaces just get name
                 li.prefabName = t.name;
             }
             //get the sprite info if there is a sprite renderer
             if (sr != null)
             {
                 li.spriteColor = sr.color;
                 li.spriteLayer = sr.sortingLayerName;
                 li.spriteOrder = sr.sortingOrder;
             }
             //add item to list
             levelItems.Add(li);
         }
         //now we should have all the levelitems, we just need the player position and cam settings
         ldr.levelsItems = levelItems.ToArray();
         //player info
         ldr.playerStartLocation = GameObject.Find("SoyBoy").transform.position;
         //camera settings
         var currentCamSettings = FindObjectOfType <CameraLerpToTransform>();
         if (currentCamSettings != null)
         {
             ldr.cameraSettings = new CameraSettingsRepresentation()
             {
                 minX          = currentCamSettings.minX,
                 maxX          = currentCamSettings.maxX,
                 minY          = currentCamSettings.minY,
                 maxY          = currentCamSettings.maxY,
                 trackingSpeed = currentCamSettings.trackingSpeed,
                 cameraZDepth  = currentCamSettings.cameraZDepth,
                 camTarget     = currentCamSettings.camTarget.name
             };
         }
         //finally save the level to file
         var levelDataToJSON = JsonUtility.ToJson(ldr);
         var savePath        = System.IO.Path.Combine(Application.dataPath, level.levelName + ".json");
         //write out
         System.IO.File.WriteAllText(savePath, levelDataToJSON);
         Debug.Log("Level saved to " + savePath);
     }
     //Add a function to load a previosuly saved level
     if (GUILayout.Button("Load Level"))
     {
         //set the name of the file to load
         Level level = (Level)target;
         //get a string for the file load to reload it in the new Level script
         string loadedLevelName = level.SavedLevelName;
         //since there isnt a game manager in the levl template scene we load manually
         string fileName = Application.dataPath + "/" + level.SavedLevelName + ".json";
         //get the template's level root
         var existingLevelRoot = GameObject.Find("Level");
         DestroyImmediate(existingLevelRoot);
         GameObject levelRoot = new GameObject("Level");//clean house prior to load
         //add level to new levelRoot
         Level newLevel = levelRoot.AddComponent <Level>();
         newLevel.SavedLevelName = loadedLevelName;
         newLevel.levelName      = loadedLevelName;
         //get the level info
         var levelFileJsonContent = File.ReadAllText(fileName);
         var levelData            = JsonUtility.FromJson <LevelDataRepresentation>(levelFileJsonContent);
         //loop through the items
         foreach (var li in levelData.levelsItems)
         {
             //make sure the prefab name doesnt contain (Clone)
             if (li.prefabName.Contains("(Clone)"))
             {
                 li.prefabName = li.prefabName.Substring(0, li.prefabName.IndexOf("(", System.StringComparison.CurrentCulture));
             }
             var levelResource = Resources.Load("Prefabs/" + li.prefabName);
             if (levelResource == null)
             {
                 Debug.Log("Could not find prefab: " + li.prefabName);
             }
             GameObject levelObj = (GameObject)Instantiate(levelResource, li.position, Quaternion.identity);
             //get the sprite
             var objSprite = levelObj.GetComponent <SpriteRenderer>();
             if (objSprite != null)
             {
                 objSprite.sortingOrder     = li.spriteOrder;
                 objSprite.sortingLayerName = li.spriteLayer;
                 objSprite.color            = li.spriteColor;
             }
             //set the levelroot as parent
             levelObj.transform.SetParent(levelRoot.transform, false);
             levelObj.transform.position   = li.position;
             levelObj.transform.rotation   = Quaternion.Euler(li.rotation.x, li.rotation.y, li.rotation.z);
             levelObj.transform.localScale = li.scale;
         }
         //set soyboys position
         GameObject soyBoy = GameObject.Find("SoyBoy");
         soyBoy.transform.position = levelData.playerStartLocation;
         //set camera position
         Camera.main.transform.position = new Vector3(soyBoy.transform.position.x,
                                                      soyBoy.transform.position.y, Camera.main.transform.position.z);
         CameraLerpToTransform cameraSettings = FindObjectOfType <CameraLerpToTransform>();
         if (cameraSettings != null)
         {
             cameraSettings.minX          = levelData.cameraSettings.minX;
             cameraSettings.maxX          = levelData.cameraSettings.maxX;
             cameraSettings.minY          = levelData.cameraSettings.minY;
             cameraSettings.maxY          = levelData.cameraSettings.maxY;
             cameraSettings.trackingSpeed = levelData.cameraSettings.trackingSpeed;
             cameraSettings.cameraZDepth  = levelData.cameraSettings.cameraZDepth;
             cameraSettings.camTarget     = GameObject.Find(levelData.cameraSettings.camTarget).transform;
         }
     }
 }
示例#4
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        if (GUILayout.Button("Save level"))
        {
        }

        // grabs a handle on the Level component and sets the Level Gameobject's position and rotation to 0.
        Level level = (Level)target;

        level.transform.position = Vector3.zero;
        level.transform.rotation = Quaternion.identity;
        // establishes levelRoot as a reference to the Level GameObject
        var levelRoot = GameObject.Find("Level");
        // creates new instances of LevelDataRepresentation and LevelItemRepresentation
        var ldr        = new LevelDataRepresentation();
        var levelItems = new List <LevelItemRepresentation>();

        foreach (Transform t in levelRoot.transform)
        {
            // loops through every child Transform object of levelRoot.
            var sr = t.GetComponent <SpriteRenderer>();
            var li = new LevelItemRepresentation()
            {
                position = t.position,
                rotation = t.rotation.eulerAngles,
                scale    = t.localScale
            };

            // bit of a hack to store the name of each level item in the prefabName field.
            if (t.name.Contains(" "))
            {
                li.prefabName = t.name.Substring(0, t.name.IndexOf(" "));
            }
            else
            {
                li.prefabName = t.name;
            }

            // Performs a check t ensure that a valid SpriteRenderer was found
            if (sr != null)
            {
                li.spriteLayer = sr.sortingLayerName;
                li.spriteColor = sr.color;
                li.spriteOrder = sr.sortingOrder;
            }

            // the item that has all the collected information is added to the LevelItems list
            levelItems.Add(li);

            // converts the entire list of level items to an array of LevelItemRepresentation objects and stores them in the LevelItems field on the LevelDataRepresentation object.
            ldr.levelItems          = levelItems.ToArray();
            ldr.playerStartPosition =
                GameObject.Find("SoyBoy").transform.position;
            // Locates the CameraLerpToTranform script in the scene then maps its settings against a new CameraSettingsRepresentation object.
            var currentCamSettings = FindObjectOfType <CameraLerpToTransform>();

            if (currentCamSettings != null)
            {
                ldr.cameraSettings = new CameraSettingsRepresentation()
                {
                    cameraTrackTarget = currentCamSettings.camTarget.name,
                    cameraZDepth      = currentCamSettings.cameraZDepth,
                    minX          = currentCamSettings.minX,
                    minY          = currentCamSettings.minY,
                    maxX          = currentCamSettings.maxX,
                    maxY          = currentCamSettings.maxY,
                    trackingSpeed = currentCamSettings.trackingSpeed
                };
            }

            var levelDataToJson = JsonUtility.ToJson(ldr);
            var savePath        = System.IO.Path.Combine(Application.dataPath, level.levelName + ".json");
            System.IO.File.WriteAllText(savePath, levelDataToJson);
            Debug.Log("Level saved to " + savePath);
        }
    }
示例#5
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        if (GUILayout.Button("Save level"))
        {
        }

        /* 1 Grabs a handle on the Level component and sets the Level GameObject’s position
         * and rotation to 0, ensuring everything is positioned from the origin point in the
         * scene. */
        Level level = (Level)target;

        level.transform.position = Vector3.zero;
        level.transform.rotation = Quaternion.identity;
        // 2 Establishes levelRoot as a reference to the Level GameObject, which is the parent of all the level items.
        var levelRoot = GameObject.Find("Level");
        // 3 Creates new instances of LevelDataRepresentation and LevelItemRepresentation.
        var ldr        = new LevelDataRepresentation();
        var levelItems = new List <LevelItemRepresentation>();

        foreach (Transform t in levelRoot.transform)
        {
            /* 4 . Loops through every child Transform object of levelRoot. Then it takes a reference
             * to any potential SpriteRenderer component and assigns it to the sr variable. It then
             * creates a new LevelItemRepresentation to hold the position, rotation and scale of
             * each item. */
            var sr = t.GetComponent <SpriteRenderer>();
            var li = new LevelItemRepresentation()
            {
                position = t.position,
                rotation = t.rotation.eulerAngles,
                scale    = t.localScale
            };

            /* 5 This is a bit of a hack to store the name of each level item in the prefabName field.
             * When loading a level later on, your code will need to know which prefabs to use to
             * construct the level based on the name stored in the JSON file. */
            if (t.name.Contains(" "))
            {
                li.prefabName = t.name.Substring(0, t.name.IndexOf(" "));
            }
            else
            {
                li.prefabName = t.name;
            }

            /* 6 . Performs a check to ensure that a valid SpriteRenderer was found — because a level
             * item might not have an attached sprite. If the sr variable is not null, then
             * information about the sprite’s sorting, order and color stores in the
             * LevelItemRepresentation object. */
            if (sr != null)
            {
                li.spriteLayer = sr.sortingLayerName;
                li.spriteColor = sr.color;
                li.spriteOrder = sr.sortingOrder;
            }
            /* 7 Lastly, the item that has all the collected information is added to the levelItems list */
            levelItems.Add(li);
        }

        /* 8 Converts the entire list of level items to an array of LevelItemRepresentation
         * objects and stores them in the levelItems field on the LevelDataRepresentation
         * object. */
        ldr.levelItems          = levelItems.ToArray();
        ldr.playerStartPosition =
            GameObject.Find("SoyBoy").transform.position;

        /* 9 . Locates the CameraLerpToTransform script in the scene. Then it maps its settings
         * against a new CameraSettingsRepresentation object and assigns that object to the
         * LevelDataRepresentation object’s cameraSettings field. */
        var currentCamSettings = FindObjectOfType <CameraLerpToTransform>();

        if (currentCamSettings != null)
        {
            ldr.cameraSettings = new CameraSettingsRepresentation()
            {
                cameraTrackTarget = currentCamSettings.camTarget.name,
                cameraZDepth      = currentCamSettings.cameraZDepth,
                minX          = currentCamSettings.minX,
                minY          = currentCamSettings.minY,
                maxX          = currentCamSettings.maxX,
                maxY          = currentCamSettings.maxY,
                trackingSpeed = currentCamSettings.trackingSpeed
            };
        }

        /* This uses JsonUtility to serialize the LevelDataRepresentation object, and all of its
         * nested level item fields and values, to a JSON string named levelDataToJson.
         * Then it combines the level name specified in the editor with the game’s app data path
         * (pointing to our Assets folder). This path is used to write the JSON string out to a file.*/
        var levelDataToJson = JsonUtility.ToJson(ldr);
        var savePath        = System.IO.Path.Combine(Application.dataPath,
                                                     level.levelName + ".json");

        System.IO.File.WriteAllText(savePath, levelDataToJson);
        Debug.Log("Level saved to " + savePath);
    }