- Home /
Moving gui texture
I know this might seem trivial, but I couldn't find it anywhere, so how would I script for a gui texture change in position after I click on it.
Answer by Flynn · Jan 18, 2011 at 06:05 AM
That depends on the kind of motion, but the general idea is simple
If you define the rect as so (100 being an example size, do whatever you like):
new Rect(x,y,100,100);
Then you can have instance variables elsewhere in the code what change the position of whatever uses that rect. Changing these variables changes the position of the texture.
You can then call
x += 100*Time.deltaTime
This will make the button move right by 100 pixels each second.
EDIT: Example code:
c#
public class ByeByeButton : Monobenaviour
{
public int x;
public int y;
public bool gone = false;
public void OnGUI()
{
if (GUI.Button(new Rect(x,y,100,100),"Click me and watch me whoosh!"))
{
gone = true;
}
if (gone)
{
x += Time.deltaTime * 100;
y = x;
}
}
}
JS:
var x : int;
var y : int;
var gone : bool = false;
public function OnGUI()
{
if (GUI.Button(new Rect(x,y,100,100),"Click me and watch me whoosh!"))
{
gone = true;
}
if (gone)
{
x += Time.deltaTime * 100;
y = x;
}
}
would this work the same for GUIs drawn in the OnGUI function?
Yep, in fact that's what I designed it for. For instance, calling GUI.Label(new Rect(x,y,100,100),"Hello, World"); would work just fine. You just need to define the variables x and y. ;)
$$anonymous$$y texture is a circle, I could apply it to the rectangle, but when I do, the edges of the rectangle are visible. how do I fix this?
Actually, I figured out how to apply a GUI skin, and I am good to go. Thank you for your help.