Skip to content

Instantly share code, notes, and snippets.

@Avonexile
Last active October 30, 2018 08:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Avonexile/9c44d3d011368f413a0565add35b7627 to your computer and use it in GitHub Desktop.
UI Manager I made in Save The Trees
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class UIManager : MonoBehaviour {
//singleton
public static UIManager instance;
//UIState
public enum UIState {MainMenu, LoadingScreen, Ingame};
public UIState _UIState;
//ui elements lists
public List <RectTransform> allMenuItems = new List<RectTransform>();
public List <RectTransform> subMenus = new List<RectTransform>();
public RectTransform mainMenu, ingame, settings, pauseMenu, buildingsbar, eventlog, statistics, loadingScreen;
//time state and window states
public bool paused, settingsActive, creditsActive;
public List <Image> time = new List<Image>();
public List <Image> buildingCategory = new List<Image>();
public Color pressedButton, normalButton;
//timescale
private float currentTimeScale;
//animation
public List<Animator> animList = new List<Animator>();
public List<bool> animationActive = new List<bool>();
public List<RectTransform> buildingbars = new List<RectTransform>();
//eventlog
public TextMeshProUGUI eventButtonText;
public string texttest; //testing
public bool go; //testing
public Queue<string> queue = new Queue<string>();
public TextMeshProUGUI eventText;
public int textLimit;
//statistics
public TextMeshProUGUI woodText;
public TextMeshProUGUI stoneText;
public TextMeshProUGUI moneyText;
public TextMeshProUGUI foodText;
public TextMeshProUGUI yearText;
public TextMeshProUGUI citizenText;
public bool loading = true;
public void TextUpdate()
{
woodText.text = StatisticManager.instance.wood + " / " + StatisticManager.instance.woodStorage;
stoneText.text = StatisticManager.instance.stone + " / " + StatisticManager.instance.stoneStorage;
moneyText.text = StatisticManager.instance.money + " / " + StatisticManager.instance.moneyStorage;
foodText.text = StatisticManager.instance.food + " / " + StatisticManager.instance.foodStorage;
yearText.text = "Year: " + StatisticManager.instance.age;
citizenText.text = StatisticManager.instance.citizens + " / " + StatisticManager.instance.allCitizens;
}
void Awake ()
{
if(instance == null)
{
instance = this;
}
}
// Use this for initialization
public void MyStart ()
{
TextUpdate();
currentTimeScale = 1f;
SetTimeScale(1f);
SelectedTimeColor(1);
if(blockState)
{
return;
}
else
{
CheckState();
}
}
// Checks for input to pause the game and the hotkeys for time control
void Update ()
{
PressEscape();
HotKeys();
}
//has a string queue which puts a string on one line and foreach string in the queue it remembers the newest input
public void EventLog (string events)
{
eventButtonText.text = "Event: " + events;
if (queue.Count >= textLimit)
{
queue.Dequeue();
}
queue.Enqueue(events);
eventText.text = "";
foreach(string st in queue)
{
eventText.text = eventText.text + st + "\n";
}
}
void CheckState ()
{
switch(_UIState)
{
case UIState.MainMenu:
//main scene
LoadScene("Main menu");
List<RectTransform> mainMenuList = new List<RectTransform>() {mainMenu};
EnableMenuItems(mainMenuList);
break;
case UIState.LoadingScreen:
//loading screen
//load one of the scenes
List<RectTransform> loadingscreen = new List<RectTransform>() {loadingScreen};
EnableMenuItems(loadingscreen);
break;
case UIState.Ingame:
//ingame scene
LoadScene("LevelSceneThingie");
List<RectTransform> ingameList = new List<RectTransform>() {ingame};
EnableMenuItems(ingameList);
break;
}
}
void LoadScene (string name)
{
if(SceneManager.GetActiveScene().name == name)
{
return;
}
else
{
SceneManager.LoadScene(name);
}
}
//Sets the next state of the ui and then updating it
void SetState (UIState state)
{
_UIState = state;
CheckState();
}
//receives items and will make a list of them that will get send to another function
private void EnableMenuItems(RectTransform item) {
List<RectTransform> items = new List<RectTransform>() {item};
EnableMenuItems(items);
}
//gets a list that will in which the objects will get set true after everything is set false
private void EnableMenuItems(List<RectTransform> items) {
foreach(RectTransform rT in allMenuItems)
{
rT.gameObject.SetActive(false);
}
foreach(RectTransform rT in items)
{
rT.gameObject.SetActive(true);
}
}
#region ButtonFunctions
//receives number from button, with correct rectTransform and switches between states of rectTransform
public void SubMenuConverter (int number)
{
if(number == 4)
{
settingsActive = !settingsActive;
}
if(number == 5)
{
creditsActive = !creditsActive;
}
Submenus(subMenus[number]);
}
//Switches between the on and off of a RectTransform
private void Submenus (RectTransform element)
{
bool active = element.gameObject.activeSelf;
active = !active;
element.gameObject.SetActive(active);
}
//button function
public void MainMenu () {
paused = false;
SetState(UIState.MainMenu);
SetTimeScale(1f);
}
//button function
public void Ingame () {
SetState(UIState.Ingame);
SetTimeScale(1f);
}
//button function
public void Resume ()
{
paused = false;
pauseMenu.gameObject.SetActive(false);
SetTimeScale(currentTimeScale);
}
//button function
public void QuitGame ()
{
Application.Quit();
}
public void SetTimeScale (float scale)
{
Time.timeScale = scale;
if(!paused)
{
currentTimeScale = scale;
}
}
#endregion
//hotkeys for time speed
private void HotKeys ()
{
if(paused)
{
return;
}
if(Input.GetButtonDown("Pause"))
{
SelectedTimeColor(0);
SetTimeScale(0f);
}
if(Input.GetButtonDown("Normal Speed"))
{
SelectedTimeColor(1);
SetTimeScale(1f);
}
if(Input.GetButtonDown("Fast Speed"))
{
SelectedTimeColor(2);
SetTimeScale(1.5f);
}
if(Input.GetButtonDown("Faster Speed"))
{
SelectedTimeColor(3);
SetTimeScale(2f);
}
}
//makes you pause ingame or unpause
private void PressEscape () {
//Debug.Log("escape");
if(Input.GetButtonDown("Escape") && _UIState == UIState.Ingame && paused == false && loading == false)
{
paused = true;
SetTimeScale(0f);
pauseMenu.gameObject.SetActive(true);
}
else if(Input.GetButtonDown("Escape") && settingsActive)
{
SubMenuConverter(4);
}
else if(Input.GetButtonDown("Escape") && creditsActive)
{
SubMenuConverter(5);
}
else if(Input.GetButtonDown("Escape") && _UIState == UIState.Ingame && paused)
{
Resume();
}
}
//when going down it resets before the animation can play which causes it to disappear
public void Buildingbar (int number)
{
//sets other bars false
foreach (var item in buildingbars)
{
item.gameObject.SetActive(false);
}
buildingbars[number].gameObject.SetActive(true);
//does the correct animation value
for (int i = 0; i < animationActive.Count; i++)
{
if(i == number)
{
animationActive[number] = !animationActive[number];
}
else if(i != number)
{
animationActive[i] = false;
}
}
PlayAnimation(number);
}
//makes the animations play according to their state and number
void PlayAnimation (int number)
{
Animator anim = animList[number];
anim.speed = 1f;
anim.speed = anim.speed / currentTimeScale;
//set button color
foreach (var item in buildingCategory)
{
var image = item.GetComponent<Image>();
image.color = normalButton;
}
//sets right animation and button color
if(animationActive[number] && !anim.GetCurrentAnimatorStateInfo(0).IsName("Up"))
{
anim.SetBool("Up", true);
buildingCategory[number].GetComponent<Image>().color = pressedButton;
return;
}
if(!animationActive[number] && anim.GetCurrentAnimatorStateInfo(0).IsName("Up"))
{
anim.SetBool("Up", false);
buildingCategory[number].GetComponent<Image>().color = normalButton;
return;
}
}
//hotkeys and timescale feedback
public void SelectedTimeColor (int number)
{
foreach (var item in time)
{
var image = item.GetComponent<Image>();
image.color = normalButton;
}
time[number].color = pressedButton;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment