HUD/GUI with UFPS

How to create a Hud menu to be used with UFPS. This is using the Unity UI, not the old legacy GUI. This script should also work with NGUI with some edits.

IF USING UNITY 5.2 = Unity made some changes to how Cursors work.  The last script is the updated Pause Detect script.

I also made some edits to some UFPS scripts.

Edits for UFPS Script vp_FPPlayerEventHandler – add this below row 43

public vp_Value<Texture2D> CurrentWeaponIcon;

Edits for UFPS Script vp_PlayerInventory – add this below row 986 – almost identical to above section

protected virtual Texture2D OnValue_CurrentWeaponIcon
{
get
{
if (CurrentWeaponInstance == null)
return null;
if (CurrentWeaponInstance.Type == null)
return null;

vp_ItemType u = CurrentWeaponInstance.Type as vp_ItemType;
if (u == null)
return null;
if (u.Icon == null)
return null;
return u.Icon;
}
}

Pause Detect Script

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
//NGUI STUFF = row 18, 20, 22, 24 - Haven't tested for NGUI yet
namespace red
{
public class red_PauseDetect : MonoBehaviour {

private GameObject FPS_Player;//UFPS Player - Grabbing this from the player
private vp_FPPlayerEventHandler m_Player = null;//UFPS Event Handler for Player
private vp_FPInput m_Input = null;//UFPS Input for Player

//UGUI or NGUI Stuff
public GameObject HUD;//HUD Always Showing Menu - In Game View
public GameObject Pause;//Pause Menu
public Text healthLabel=null;
// public UILabel healthLabel = null;//USE THIS FOR NGUI
public Slider healthSlider=null;
// public UISlider healthSlider = null;//USE THIS FOR NGUI
public Text ammoLabel=null;
// public UILabel ammoLabel = null;//USE THIS FOR NGUI
public Text clipsLabel=null;
// public UILabel clipsLabel = null;//USE THIS FOR NGUI
private RawImage ammoIcon=null;

void Awake()
{
FPS_Player = this.gameObject;
m_Player = FPS_Player.transform.GetComponent<vp_FPPlayerEventHandler>();
m_Input = FPS_Player.transform.GetComponent<vp_FPInput>();
ammoIcon=ammoLabel.gameObject.GetComponentInChildren<RawImage>();
}
void Start()
{
Pause.SetActive(false);
}
// Update is called once per frame
void Update () {

if(Time.timeScale == 0){
HUD.SetActive(false);
Pause.SetActive(true);
Screen.lockCursor=false;//This changes to mouse cursor
m_Input.MouseCursorForced=true;//This lets the screen be clicked on
}
else
{
HUD.SetActive(true);
Pause.SetActive(false);
}

//Adjust the Health Text - Should also work for NGUI
healthLabel.text = "Health: " + (int)(m_Player.Health.Get()*10.0f) + "%";//TEXT
//Adjust the Health slider - Should also work for NGUI
healthSlider.value=m_Player.Health.Get()/10f;//SLIDER

// display a simple 'Clips' HUD
int clips;
if(m_Player.CurrentWeaponClipCount.Get()>0)
clips=m_Player.CurrentWeaponClipCount.Get()/m_Player.CurrentWeaponMaxAmmoCount.Get();
else clips=0;
clipsLabel.text = "Clips: " + clips;
// display a simple 'Ammo' HUD
ammoLabel.text = "Ammo: " + m_Player.CurrentWeaponAmmoCount.Get();
// display ammo icon
if(ammoIcon!=null)
{
if(m_Player.CurrentAmmoIcon.Get()!=null)
ammoIcon.texture=m_Player.CurrentAmmoIcon.Get();
else
{
ammoIcon.texture=m_Player.CurrentWeaponIcon.Get();
}
ammoIcon.color=Color.white;
}
else
ammoIcon.color=Color.clear;

}
public virtual void HideMenu()
{
//UNPause The Game Here
m_Player.Pause.Set(!m_Player.Pause.Get());
m_Input.MouseCursorForced=false;//Change cursor back to normal in game mode

//Turn back on Hud Menu
HUD.SetActive(true);
//Turn off Pause Menu
Pause.SetActive(false);
}

//Not Currently using this. Basically it grabs the current level and restarts.
//Make sure build settings are correct.
public virtual void RestartGame()
{
//Restart the current Level
Application.LoadLevel(Application.loadedLevelName);
}
//Not Currently using this. Suggestion would be to create a public String mainMenuSceneName and plug it in
public virtual void MainMenu()
{
Application.LoadLevel("MainMenuContest");
}

}
}

HeaderWP

Pause Detect Script For UNITY 5.2+

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
namespace red
{
 public class red_PauseDetect : MonoBehaviour {
 
 private GameObject FPS_Player;//UFPS Player - Grabbing this from the player
 private vp_FPPlayerEventHandler m_Player = null;//UFPS Event Handler for Player
 private vp_FPInput m_Input = null;//UFPS Input for Player
 
 public GameObject HUD;//HUD Always Showing Menu - In Game View
 public GameObject Pause;//Pause Menu
 public Text healthLabel=null;
 public Slider healthSlider=null;
 public Text ammoLabel=null;
 public Text clipsLabel=null;
 private RawImage ammoIcon=null;
 
 void Awake()
 {
 FPS_Player = this.gameObject;
 m_Player = FPS_Player.transform.GetComponent<vp_FPPlayerEventHandler>();
 m_Input = FPS_Player.transform.GetComponent<vp_FPInput>();
 ammoIcon=ammoLabel.gameObject.GetComponentInChildren<RawImage>();
 }
 void Start()
 {
 Pause.SetActive(false);//Turns off the Pause Menu
 }

 void Update () {

 if (m_Player.Pause.Get() == true)
 PauseMenu();

 healthLabel.text = "Health: " + (int)(m_Player.Health.Get()*10.0f) + "%";//TEXT
 healthSlider.value=m_Player.Health.Get()/10f;//SLIDER
 
 // display a simple 'Clips' HUD
 int clips; 
 if(m_Player.CurrentWeaponClipCount.Get()>0)
 clips=m_Player.CurrentWeaponClipCount.Get()/m_Player.CurrentWeaponMaxAmmoCount.Get();
 else clips=0;
 clipsLabel.text = "Clips: " + clips;
 // display a simple 'Ammo' HUD
 ammoLabel.text = "Ammo: " + m_Player.CurrentWeaponAmmoCount.Get();
 // display ammo icon
 if(ammoIcon!=null)
 {
 if(m_Player.CurrentAmmoIcon.Get()!=null)
 ammoIcon.texture=m_Player.CurrentAmmoIcon.Get();
 else
 {
 ammoIcon.texture=m_Player.CurrentWeaponIcon.Get();
 }
 ammoIcon.color=Color.white;
 }
 else 
 ammoIcon.color=Color.clear; 
 }
 public virtual void HideMenu()
 {
 //UNPause The Game Here
 m_Player.Pause.Set(!m_Player.Pause.Get());
 Cursor.lockState = CursorLockMode.Locked;//Unity 5.2 lock the cursor
 Cursor.visible = false;//Unity 5.2 turn off the cursor
 m_Input.MouseCursorForced=false;//Change cursor back to normal in game mode
 
 HUD.SetActive(true);//Turn back on Hud Menu
 Pause.SetActive(false);//Turn off Pause Menu
 }
 public virtual void PauseMenu()
 {
 HUD.SetActive(false);
 Pause.SetActive(true);
 m_Input.MouseCursorForced = true;
 Cursor.visible = true;
 }
 //Not Currently using this. Basically it grabs the current level and restarts. 
 //Make sure build settings are correct.
 public virtual void RestartGame()
 {
 //Restart the current Level
 Application.LoadLevel(Application.loadedLevelName);
 }
 //Not Currently using this. Suggestion would be to create a public String mainMenuSceneName and plug it in
 public virtual void MainMenu()
 {
 Application.LoadLevel("MainMenuContest");
 }
 
 }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s