Archive for May 2017

3 "S"’s RPG Sample – Step 1

So like any game development project, the first steps we’ll take in implementing our 3 “S”‘s game will be putting together a design document and starting the shell of our actual game. Since we’ll be developing the game using Unity (shocker, I know) the first steps are pretty easy:

  • Create the project
  • Set up the Assets structure
  • Create the typical scenes

I have to assume that you have at least some basic knowledge about using Unity. If I had to start from the very beginning, we’d never actually finish building a game. The game interface and visuals of the game will be very simple as that’s not what we’re concentrating on here. Getting a working game completed is more important than a visually stunning game that never gets finished.

Start a new Unity project, select the 2D mode, give it a name. After Unity starts, create the following folders:

  • Audio
  • Prefabs
  • Scenes
  • Scripts
  • Sprites

In the Scenes folder create the following scenes:

  • CreateCharacter
  • Game
  • MainMenu

Open the Menu scene and add a couple of buttons:

  • New Game
  • Load Game
  • Options
  • Exit

We’ll look into what it takes to start a new game in a bit. For the Load Game menu, it’s up to you how you want to handle saving games and this will influence how the loading of games works. If you want to allow the player to save at any time with an unlimited number of saves, you could potentially have a lot of save files to deal with. You probably don’t want to allow this however. It’s more typical to give the player a limited amount of saves that he can overwrite. This is much easier to deal with when constructing your Load Game scene, although the theory is the same as if you were allowing an unlimited amount of save files. The player just ends up with a smaller list to scroll through.

What options you allow the player to tweak is up to you. At the very least, however, you’ll want to give the player some options for audio and video. Turning on and off and adjusting the volume level of audio is pretty standard. Setting the resolution and allowing the player to toggle between full-screen and windowed mode is the other usual group of option settings.

We need to add some scripts to hook up the basic UI elements. I usually name my scripts for this the same as the scene they’re used in. If you want to append “Scene” to a script to be more descriptive that’s fine. Add C# files for the MainMenu, Options, and Game scene.

The MainMenu script is pretty simple. It just transitions to the appropriate scene for the button clicked:

using UnityEngine.SceneManagement;
using UnityEngine;
using UnityEngine.UI;

public class MainMenu : MonoBehaviour {

    public GameObject ButtonPanel;
    public GameObject LoadGamePanel;

        void Start ()
    {
            //check to see if game has already been started and adjust buttons accordingly
       
        }
       
    public void NewGameButtonClick()
    {
        SceneManager.LoadScene("CreateCharacter");
    }

    public void LoadGameButtonClick()
    {
        LoadGamePanel.SetActive(true);
        ButtonPanel.SetActive(false);
    }

    public void OptionsButtonClick()
    {
        SceneManager.LoadScene("Options");
    }

    public void ExitButtonClick()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
    }

    public void SavedGamesCancelButtonClick()
    {
        LoadGamePanel.SetActive(false);
        ButtonPanel.SetActive(true);
    }

    public void SavedGamesOKButtonClick()
    {

    }
}

Hook each button to the appropriate method. We’ll be adding more to this for loading saved games, but this will do for now.

Open the Options scene and add an OK and Cancel button, add the following to the Options script, then hook the buttons to the methods:

using UnityEngine;
using UnityEngine.SceneManagement;

public class Options : MonoBehaviour {

    private GameOptions _options;

    void Start()
    {
        //load current settings

    }

    public void OKButtonClick()
    {
        //save changed settings

        SceneManager.LoadScene("Menu");
    }

    public void CancelButtonClick()
    {
        SceneManager.LoadScene("Menu");
    }
}

We’ll need to do the same thing for the CreateCharacter scene. As the player will be performing a number of steps to fill in all of the character info, we’ll add Next and Previous buttons as well:

Note that the OK button has been disabled. It will only enable once all of the steps for creating the character have been completed.

The code to handle navigation for the buttons is pretty simple at this point:

using UnityEngine;
using UnityEngine.UI;

public class CreateCharacter : MonoBehaviour {

    private int _curStep;

    public Button NextButton;
    public Button PreviousButton;

        void Start ()
    {
        _curStep = 0;  
        }
       
        public void NextStepButtonClick()
    {
        _curStep++;
        if(_curStep == 3)
        {
            PreviousButton.interactable = true;
            NextButton.interactable = false;
        }

        ProcessStep();
    }

    public void PreviousStepButtonClick()
    {
        _curStep--;
        if (_curStep == 0)
        {
            PreviousButton.interactable = false;
            NextButton.interactable = true;
        }

        ProcessStep();
    }

    private void ProcessStep()
    {
        switch(_curStep)
        {
            case 0:
                {

                    break;
                }
            case 1:
                {

                    break;
                }
           case 2:
                {

                    break;
                }
            case 3:
                {

                    break;
                }      
        }
    }
}

We’ll start with 4 steps and can add more later if needed. The ProcessSteps method will simply toggle panels for each step as needed.

The last piece we’ll start on is the pause menu so we have a way to get out of the Game scene. This is a simple Panel with some buttons:

In the Game script is the typical button handler:

public class Game : MonoBehaviour
{

    public GameObject PausePanel;
    private bool _gamePaused;


        // Use this for initialization
        void Start ()
    {
               
        }
       
        // Update is called once per frame
        void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if(_gamePaused)
            {
                ResumeButtonClick();
            }
            else
            {
                PausePanel.SetActive(true);
                _gamePaused = true;
            }
        }
    }

    public void ResumeButtonClick()
    {
        PausePanel.SetActive(false);
        _gamePaused = false;
    }

    public void SaveGameButtonClick()
    {

    }

    public void ExitButtonClick()
    {
        SceneManager.LoadScene("MainMenu");
    }
}

We check each frame to see if the Esc key has been pressed and, if so, toggle the PausePanel based on whether or not the game is already paused.

This is the basic skeleton for our game that allows navigation between the current scenes. We’ll add more to this soon, but we need to do some work on the first two “S”’s before that happens. That’s coming up next.