- Home /
GUI opacity.
Two questions.
1) Does OnGUI() update every frame like Update();?
2) Is there a way I can remove a GUI element or even better set the opacity(I could not find anything in the scripting reference)
THANK YOU!
Answer by whydoidoit · Apr 03, 2013 at 06:25 PM
OnGUI updates multiple times per frame (one of the reasons it is slow and soon to be replaced). OnGUI gets called for layout, repaint and for each mouse messages, key press etc.
To remove an element, just don't include it! In other words put an if(something) {} around it.
You can set the GUI.color property to a transparent flavour of white before drawing the item and reset it afterwards to make it appear transparent/semi transparent (plus of course you could colour it something else entirely).
GUI.color = new Color(1,1,1,0.5f);
if(shouldDraw)
{
if(GUILayout.Button("I'm not all there"))
{
}
}
The problem is, what happens if it is an imported texture? Can you still change the color? Is it possible to make a circle with code? Also the if statement does not seem to work. Thanks
var crosshairTexture: Texture2D;
var crosshairPosition: Rect;
var crosshairActive = true;
var playerWalkScript: Player$$anonymous$$ovementScript;
var gunScript: GunScript;
var crosshairSize: float = 5;
function Start()
{
playerWalkScript = GameObject.FindWithTag("Player").GetComponent(Player$$anonymous$$ovementScript);
}
function Update()
{
if (Input.GetButtonDown("Fire2"))
{
crosshairActive = false;
}
else
{
crosshairActive = true;
}
gunScript = playerWalkScript.currentGun.GetComponent(GunScript);
crosshairPosition = Rect(((Screen.width ) / 2) - (crosshairPosition.width / 2), ((Screen.height ) / 2) - (crosshairPosition.height / 2), crosshairTexture.width * gunScript.currentAccuracy * crosshairSize, crosshairTexture.height * gunScript.currentAccuracy * crosshairSize);
}
function OnGUI()
{
if(crosshairActive == true)
{
GUI.DrawTexture(crosshairPosition, crosshairTexture);
}
if (gunScript.ammoClip == 0)
{
GUI.backgroundColor = Color.red;
}
}
Oh, wow, I forgot to change it from GetButtonDown to GetButton. Never $$anonymous$$d the if statement. Is it still possible to change the color of an imported img?
I'm pretty sure images draw with GUI.color (or perhaps GUI.backgroundColor), but I'm normally drawing icons.
No, both
GUI.backgroundColor = Color.red; and GUI.color = Color.red;
did not work. Anyways the most important thing was the removal of the object, Thanks!
Oh actually - I know why I was thinking that - you can display the texture in a GUILayout.BeginArea and then actually display it like this:
GUILayout.Label(theTexture);
I think that's how I've done it previously.
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
BCE0044: expecting (, found 'OnGUI'. 1 Answer
GUI Crosshair 2 Answers
turn off items from GUI 0 Answers
GUI Not updating 1 Answer