Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Unity Game Development Blueprints
  • Table Of Contents Toc
  • Feedback & Rating feedback
Unity Game Development Blueprints

Unity Game Development Blueprints

By : John P. Doran
4 (6)
close
close
Unity Game Development Blueprints

Unity Game Development Blueprints

4 (6)
By: John P. Doran

Overview of this book

If you want to build enticing projects with Unity, this book is for you. Readers who are familiar with the basics of how to create simple projects in Unity will have an easier time.
Table of Contents (11 chapters)
close
close
10
Index

Level editor – saving/loading levels to file


Now that we have the groundwork all placed and ready, let's get to the real meat of the level editor: saving and loading! Perform the following steps:

  1. Open our LevelEditor class in MonoDevelop. The first step will be to include some additional functionality at the beginning of our file:

    //You must include these namespaces
    //to use BinaryFormatter
    using System;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
  2. The first thing we'll want to add is a variable, as follows:

    string levelName = "Level1";
  3. Now, we'll need to add the following code to the OnGUI function:

    GUILayout.BeginArea(new Rect(10, 20, 100, 100));
    levelName = GUILayout.TextField(levelName);
    if (GUILayout.Button ("Save"))
    {
      SaveLevel();
    }
    if (GUILayout.Button ("Load"))
    {
      //If we have a file with the name typed in, load it!
      if(File.Exists(Application.persistentDataPath + "/" + levelName + ".lvl"))
      {
        LoadLevelFile(levelName);
        PlayerStart.spawned = false...

Unlock full access

Continue reading for free

A Packt free trial gives you instant online access to our library of over 7000 practical eBooks and videos, constantly updated with the latest in tech

Create a Note

Modal Close icon
You need to login to use this feature.
notes
bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY