OnGUI doesn't update
I have a problem with time progress bar, It's doesn't update every moment. Just when time is less than zero it's suddenly goes empty. It's always full and doesn't decrease. sorry for my terrible english.
full time progress.
empty time progress.
My code:
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
public float time = 10;
public float maxTime = 10;
public float timeBurnRate = 1;
public Texture2D bgTexture;
public Texture2D timeBarTexture;
public int iconWidth = 32;
public Vector2 timeOffset = new Vector2 (10, 10);
void Start(){
player = GameObject.FindObjectOfType<Player>();
}
void OnGUI(){
var percent = Mathf.Clamp01 (time / maxTime);
if (!player)
percent = 0;
DrawMeter (timeOffset.x, timeOffset.y, timeBarTexture, bgTexture, percent);
}
void DrawMeter(float x, float y, Texture2D texture, Texture2D background, float percent){
var bgW = background.width;
var bgH = background.height;
GUI.DrawTexture (new Rect (x, y, bgW, bgH), background);
var nW = ((bgW - iconWidth) * percent) * iconWidth;
GUI.BeginGroup (new Rect (x, y, nW, bgH));
GUI.DrawTexture (new Rect (0, 0, bgW, bgH), texture);
GUI.EndGroup ();
}
void Update () {
if (!player)
return;
if (time > 0) {
time -= Time.deltaTime * timeBurnRate;
} else {
Explode script = player.GetComponent<Explode>();
script.OnExplode();
}
}
}
I don't see how that will compile, since you never declare the player
variable.
Answer by Bunny83 · Feb 04, 2017 at 03:37 PM
This line:
var nW = ((bgW - iconWidth) * percent) * iconWidth;
doesn't make much sense. You calulate the width of the background minus the iconWidth which gives you the width of the remaining background. That amount you scale with your percentage. Now comes the strange thing, why do you multiply by iconWidth? You should remove that last multiplication.
Also you seem to only scale your image instead of actually "sliding" it. Shouldn't you use that variable as x-offset?
It would help to understand how your actual images look like (timeBarTexture and bgTexture). Also what exact behaviour you want. If the time is 10, should the bar be "full"? While it's decreasing, should it move to the right? In that case you have to invert your percentage.
Basically something like this:
void DrawMeter(float x, float y, Texture2D texture, Texture2D background, float percent){
var bgW = background.width;
var bgH = background.height;
GUI.BeginGroup (new Rect (x, y, bgW, bgH));
GUI.DrawTexture (new Rect (0, 0, bgW, bgH), background);
var xPos = ((bgW - iconWidth) * (1f-percent);
GUI.DrawTexture (new Rect (xPos, 0, bgW, bgH), texture);
GUI.EndGroup ();
}
Thanks, it's amazing. How can i do this from right and when rich to icon(square that hold "A" char) stop? I know percentage must invert but how about left icon. i want it stay.
Then it would be easier to seperate the icon and the bar. However there might be another way by reducing the clipping area on the right side.
var bgW = background.width;
var bgH = background.height;
GUI.DrawTexture (new Rect (x, y, bgW, bgH), background);
var nW = ((bgW - iconWidth) * percent + iconWidth;
GUI.BeginGroup (new Rect (x, y, nW, bgH));
GUI.DrawTexture (new Rect (0, 0, bgW, bgH), texture);
GUI.EndGroup ();
Now that i see the result i finally understood what you wanted to do ^^. Your only error was that you have a *
where you should have a +
I'm just wondering, why is the icon part of the slider when it should not move? Shouldn't it be part of the background?
I don't know, i see that way in this tutorial. btw you have a good solution for this problem. tnx again.
Are you sure you don't have confused a "+" with a "*"? If they really wrote * iconWidth
it's an error.