Character Select Screen Creation


 

Scene Layout

Tonight I created a character selection screen for Luft. First, I created the layout in Photoshop, modifying assets provided by our artist. Then, I setup the scene with all the objects it needed.

Character Selection Script

I am also really proud of how far I’ve come with C# in the last 4 months. I’ve decided to post the code for the character selection here. It’s mostly self contained so you could get an idea of what is happening in the video in that scene by looking through it.

This should also give some insight into how I’m starting to deal with documentation within my code since we have multiple people that might end up working with it.

//	==========================================================
//			 Manages Character Selection
//	==========================================================
//	This script will exist on the CharacterSelect prefab.
//	It has several public variables that are all called in the
//	inspector. These are all children of CharacterSelect prefab.
//	The main function of this script is to take in an int from a
//	button press and use that to determine what image and stats
//	to display. On the more Technical end, it also Checks to see
//	if characters are unlocked before displaying them and upon
//	exit of the scene it will assigns the selected player to be 
//	used until this scene is loaded again.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CharacterSelect : MonoBehaviour {

	// image array to be adjusted for jumpPower
	public Image[] jumpPower;
	public Image charPortrait;
	public Text charName, charStats;
	// sprites in this array will be set to represent 
	// their character charNum used below.
	public Sprite[] portraitSprite;

	// set to static so it can be seen as changed in
	// the same scene
	private static int charSelected;

	// int used to compare if a button has been pressed
        // it is set to 5 which is outside charSelected range
        // so the NewPlayerSelected function will run after load
	private int currentChar = 5;
	private LevelManager levelManager;

	// Use this for initialization
	void Start () {
		levelManager = FindObjectOfType<LevelManager>();
		charSelected = PlayerPrefsManager.GetPlayer();
	}

	void Update()
	{
		if (currentChar != charSelected)
		{
			NewPlayerSelected();
			currentChar = charSelected;
		}
	}

	// things to happen after charSelected has changed.
	private void NewPlayerSelected()
	{
		// TODO: Player unlocks never happen, Make that happen before adding this if statement
		// checks if charNum represents a character that has been unlocked
		// bool unlocked = PlayerPrefsManager.IsPlayerUnlocked(charNum);
		// if (unlocked)
		// {
		charPortrait.sprite = portraitSprite[charSelected];
		TextName(charSelected);
		JumpPowerChange(charSelected);		
		// }
	}

        // This function is ran as part of NewPlayerSelected.
	// function changes the color on the jump power sprites in the scene.
	private void JumpPowerChange(int charNum)
	{
		int jumpPowerInt;
		int i = 0;
		switch (charNum)
		{
			case 0:
				jumpPowerInt = 5;
				break;
			case 1:
				jumpPowerInt = 3;
				break;
			case 2:
				jumpPowerInt = 1;
				break;
			case 3:
				jumpPowerInt = 1;
				break;
			default:
				jumpPowerInt = 1;
				print("Character Does not Exist");
				break;
		}
		// changes all their colors to gray
		foreach (Image power in jumpPower)
		{
			power.color = Color.gray;
		}
		// adds color back to all sprites up to jump power value
		foreach (Image power in jumpPower)
		{
			if(i < jumpPowerInt)
			{
				power.color = Color.white;
				i++;
			}
		}
	}

	// This function will be called on a button click.
	// The button will provide an integer representing
	// the character selected and it will be assigned to 
	// to a static variable so it can be used.
	public void CharacterSelection(int charNum)
	{
		print("button Clicked");
		charSelected = charNum;
	}

	// This function is ran as part of NewPlayerSelected.
	// It checks the charSelected to see what text
	// needs to be set in the stats display box.
	private void TextName(int charNum)
	{
		switch (charNum)
		{
			case 0:
				charName.text = "Katrina";
				charStats.text = "Jump: \nAbility: None";
				break;
			case 1:
				charName.text = "Green Guy";
				charStats.text = "Jump: \nAbility: he's green";
				break;
			case 2:
				charName.text = "Not Katrina";
				charStats.text = "Jump: \nAbility: None";
				break;
			case 3:
				charName.text = "Not Katrina 2";
				charStats.text = "Jump: \nAbility: None";
				break;
			default:
				print("Character Does not Exist");
				break;
		}
	}

	// TODO: add this to the exit button only AFTER player unlocks have been added.
	// This loads the currently selected player into memory to
	// be used when the game is started.
	public void ExitAndSelectCharacter(string level)
	{
		PlayerPrefsManager.SetPlayer(charSelected);
		levelManager = FindObjectOfType<LevelManager>();
		levelManager.LoadLevel(level);
	}
}

Other Information

The only two external scripts accessed by CharacterSelect.cs are the PlayerPrefsManager, that sets and retrieves data stored in memory and the LevelManager, which has functions to load new scenes.

Our player is identified by an integer between 0 and 3 (for 4 characters) this gets set by the ExitAndSelectCharacter() function which runs when you hit the back button.

When a game scene loads the Player prefab runs PlayerPrefsManager.GetPlayer() and stores that value in a local variable so that the game doesn’t need to constantly access stored data. There are “if” and “switch” statements that guide how the player functions and what substate of the animator they are sent to at the start of the scene.

There are probably better ways to have all of this work together, but this has worked really well for me. Everything I’ve shown here has been my personal work except the art direction and the functions that load different player abilities and buttons.

Leave a Reply

Your email address will not be published. Required fields are marked *