GUI Questions

Ok, I am new to C# so I apologize for the extremely newb questions, just trying to figure this out.

I know how to create GUI buttons and how to execute some action on clicked, basic stuff. However, for slightly more advanced things I am completely clueless.

Say I want to make a button move:

GUI.Button(new Rect(x, y, w, h), "Text")

I know the Rect x is what I want to affect, but I have no idea how to access it. I am more familiar with actionscript (wanting to learn C# btw) so I want to do something like

button = GUI.Button(new Rect(x, y, w, h), "Text")

button.x = 25;

I am thinking this is not correct. Further more I know there is a way to pre set the Rect information, but so far I have not locked down the syntax.

public new Rect myRect;

myRect = x,y,w,h;

GUI.Button(myRect, "Text")

Also, I am trying to create GUI elements during runtime, which simple tests with prefabs is working. However, I cannot figure how to check if I have already instantiated the prefab, so I do not make another.

I have further questions, such as the alpha of a button, but I am thinking if I can get some direction for the position I can learn to affect other parts.

Sorry again, I am reading and watching tutorials and learning a great deal – but the C# logic still confuses me, trying to get used to it.

Thanks

ther are several ways. You are close. What you missed is to have a persistent rectangle (a ‘member variable’) , and modiy it, not the Button.

class MovingButton : MonoBehavior {
 public Rect  myRect;

 public void Start() {
   //intialize the rectangle only once:
  myRect  = new Rect(10,10,100,20);
 }

 public void OnGUI() {
  myRect.x += 1;
  GUI.Button(myRect, "Text");
 }
}

Make sense?

Yea, that makes some sense, still a few questions if you don’t mind.

The initialization has to be on the Start() or can it be init somewhere else?

myRect += 1;

Will that move it 1 pixel over, or does the OnGUI call repeatedly?

Can I change myRect.x at another time in the code and it will affect the original button or should I destroy then create the button again?

Sorry for the near endless questions.

You can also use Time.deltaTime for moving the button for each frame.

        float x;
	float speed = 10;
	void OnGUI(){
	GUI.Button(new Rect(x,100, 50, 30), "Test");
	if(x<25)
	x = x+Time.deltaTime*speed;
	}

You can initialize it outside the Start() or in the Awake() function. To control the movement of the button, you can use an If condition.

float x;
float speed = 10;
bool isReady;
void OnGUI(){
if(isReady)
{
	GUI.Button(new Rect(x,100, 50, 30), "Test");
	if(x<25)
	x = x+Time.deltaTime*speed;
}
if(GUI.Button(new Rect(200,200,50,30),"Move other"))
isReady = true;
}

You can use the myRect.x at any other time and it will automatically update the current one. You should look into iTween library for such GUI animations.

Awesome, thank you both - very helpful. I will go over this snippets and take them apart and put them back together.

I need to spend some time reading about Awake(); and Start();

NOTE you mean myRect.x += 1;

Every time OnGUI is called, the rectangle will shift over one more pixel than it was. Since OnGUI is call once per frame IIRC it will move across pretty dang fast! As diabloroxx, multiplying by deltaTime is a better way to adjust the speed. I was just keeping it simple.

OnGUI creates and destroys the Button every frame. You do not need to do the destroy yourself. The Rect is not the same as the button, it just tells the Button where to draw. Do not destroy it.

Yes. But keep clear that this is because you changed Rect, then in OnGUI, unity used the altered Rect as a guide for where to draw the Button. In Unity you do not manipulate the buttons themselves, just the coordinates, textures and labels that are given to the button.

Note that this is different than how Unity handles 3d GameObjects and Components. Those actually persist from frame to frame.
I can see how it is confusing.

Instead of copying the codes posted above, try understanding what has been posted here and then come up with your own code. That way you will end up learning more than what you learnt now.

Maybe one of you can explain the logic of:

Time.deltaTime*speed

deltaTime is just normal time right? How does multiplying deltaTime against some other number cause slower/smoother motion than say an integer of 1 that causes object to…

Ok, I just answered my own question - am I correct at thinking an int is faster because its called every frame

Well, almost – I still don’t fully understand it.

Read up on Time in the docs; then if it still doesn;t make sense, come on back:
http://unity3d.com/support/documentation/ScriptReference/index.Keeping_Track_of_Time.html

deltaTime is not exactly normal time. It is 1/FPS. This ensures that your variable will increment @ a constant value per second rather than per frame because frames vary in length so you variable would increase/decrease faster if you game had a better FPS. So multiplying speed by Time.deltaTime will ensure that the x goes up by speed every second no matter what your frame rate is.

So if the speed is 1, and the FPS is 30, then every frame the object will move 1/30. So after 30 frames, it will have gone 1 unit. If the FPS is 60, then every frame the object will move 1/60. So after 60 frames, it will have still gone 1 unit making your game FPS independent.

And actually that part of the code should be put in a different function (likely the Update() function). Since OnGUI can be called multiple times per frame, multiplying by Time.deltaTime is not guaranteed to give you a constant speed.

Edit: pakfront beat me too it.

Looks like your question about Time.deltaTime has been answered already. I just realized that I have added the Time.deltaTime inside the OnGUI(). As stated earlier, you should avoid using it inside OnGUI() to get a better speed.