How to make GUI variables compatible with the UI system?
So I'm using a script for a REALLY old default Unity project, the FPS shooter one. I cannot get GUI textures to appear on my screen, so I've tried to use the UI feature in the newer versions in Unity and I simply cannot get it to work. What the script does is whenever damage is dealt through projectiles or colliders, it will resize the GUI texture as the health goes down. But this will not not work with the UI system oe GUI system in unity 5. The variable is:
var healthGUI : GUITexture;
and the part where the code refers to resizing the GUI
function UpdateGUI () {
// Update health gui
// The health gui is rendered using a overlay texture which is scaled down based on health
// - Calculate fraction of how much health we have left (0...1)
var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
// - Adjust maximum pixel inset based on it
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
Here is the entire code:
var maximumHitPoints = 100.0;
var hitPoints = 100.0;
var bulletGUI : GUIText;
var rocketGUI : DrawRockets;
var healthGUI : GUITexture;
var walkSounds : AudioClip[];
var painLittle : AudioClip;
var painBig : AudioClip;
var die : AudioClip;
var audioStepLength = 0.3;
private var machineGun : MachineGun;
private var rocketLauncher : RocketLauncher;
private var healthGUIWidth = 0.0;
private var gotHitTimer = -1.0;
var rocketTextures : Texture[];
function Awake () {
machineGun = GetComponentInChildren(MachineGun);
rocketLauncher = GetComponentInChildren(RocketLauncher);
PlayStepSounds();
healthGUIWidth = healthGUI.pixelInset.width;
}
function ApplyDamage (damage : float) {
if (hitPoints < 0.0)
return;
// Apply damage
hitPoints -= damage;
// Play pain sound when getting hit - but don't play so often
if (Time.time > gotHitTimer && painBig && painLittle) {
// Play a big pain sound
if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
GetComponent.<AudioSource>().PlayOneShot(painBig, 1.0 / GetComponent.<AudioSource>().volume);
gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
} else {
// Play a small pain sound
GetComponent.<AudioSource>().PlayOneShot(painLittle, 1.0 / GetComponent.<AudioSource>().volume);
gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);
}
}
// Are we dead?
if (hitPoints < 0.0)
Die();
}
function Die () {
if (die)
AudioSource.PlayClipAtPoint(die, transform.position);
// Disable all script behaviours (Essentially deactivating player control)
var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
for (var b in coms) {
var p : MonoBehaviour = b as MonoBehaviour;
if (p)
p.enabled = false;
}
Application.LoadLevel (3);
}
function LateUpdate () {
// Update gui every frame
// We do this in late update to make sure machine guns etc. were already executed
UpdateGUI();
}
function PlayStepSounds () {
var controller : CharacterController = GetComponent(CharacterController);
while (true) {
if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
GetComponent.<AudioSource>().clip = walkSounds[Random.Range(0, walkSounds.length)];
GetComponent.<AudioSource>().Play();
yield WaitForSeconds(audioStepLength);
} else {
yield;
}
}
}
function UpdateGUI () {
// Update health gui
// The health gui is rendered using a overlay texture which is scaled down based on health
// - Calculate fraction of how much health we have left (0...1)
var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
// - Adjust maximum pixel inset based on it
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
// Update machine gun gui
// Machine gun gui is simply drawn with a bullet counter text
if (machineGun) {
bulletGUI.text = machineGun.GetBulletsLeft().ToString();
}
// Update rocket gui
// This is changed from the tutorial PDF. You need to assign the 20 Rocket textures found in the GUI/Rockets folder
// to the RocketTextures property.
if (rocketLauncher) {
rocketGUI.UpdateRockets(rocketLauncher.ammoCount);
/*if (rocketTextures.Length == 0) {
Debug.LogError ("The tutorial was changed with Unity 2.0 - You need to assign the 20 Rocket textures found in the GUI/Rockets folder to the RocketTextures property.");
} else {
rocketGUI.texture = rocketTextures[rocketLauncher.ammoCount];
}*/
}
}
Does anyone know how to make this compatible with the UI system? or make GUI texture work?
Your answer
Follow this Question
Related Questions
Custom Slider Shapes causing fill to break. 0 Answers
My health bars image.fillAmount doesnt change. 4 Answers
Showing player health on UI 1 Answer
Unity3d UI ListBox Help 0 Answers
UI problem in build 1 Answer