- Home /
Making iTween work with OnGUI functions
Hi, I've just downloaded iTween and have been playing around with it. At the moment, I'm trying to animate GUI buttons but not sure how to approach it. I've tried using: ... if (GUI.Button (Rect (310,40,280,20), "First Button")) { print("Do somethin"); iTween.ValueTo(myGUI, {"from":(Rect(310,40,280,20)), "to":(Rect(250,20,120,10)), "onUpdate":"myButton"}); }
function myButton(Rect){ print("myButton"); }
It didn't have any errors but nothing is happening. Hope someone can help.
Thanks in advance. -Hakimo
Answer by 3DMagicVR · Jan 27, 2011 at 02:13 PM
Hi Hakimo, I suggest this:
var buttonRect : Rect = Rect(pos.x,pos.y,width,height);
GUI.Button(buttonRect, "Text");
iTween.ValueTo(myGUI, {"from",(Rect(310,40,280,20)), "to",(Rect(250,20,120,10)),"onupdate","myButton","onupdateparams","Rect","time",1,"easetype","spring"});
function myButton(size : Rect){ buttonRect = size; } // Check the syntax use "," rather then ":" for the values
This way you can manipulate the size and position of your GUIButton, you need to use the Rect value from the iTween (from, to), I have the same problem many days a go.
Hopes this help to you, have a great programming day.
Thanks very much 3D$$anonymous$$agicVr! It works fine now, although I'm not sure if it's a version thing but I had to make some changes. I still have to use the ':' symbol for the hashtables and I changed the the "Rect" in Valueto to "Size". Thanks again :)
Answer by Robindoo · Jan 24, 2011 at 08:38 AM
You can try
if(GUI.Button(Rect(x,y,280,20), "First Button"))
{
iTween.MoveTo(myGUI, {"x": 250, "y":20,});
}
By setting the GUI.Button x and y value to a variable, you can then move the GUI Button with the script. Hopefully this is what you wanted.
Thanks Robindoo. I tried using the code you gave me but it only affects the x and y values for the gameObject rather than the values in the script itself.
Haha. I just tried out iTweens not too long ago as well. $$anonymous$$anaged to do just some simple move with the script. Funny thing is anything in the hashtable doesnt show any errors even if you type anything wrong. You can check this link out on how to use most of the other functions.
Thanks. There's no errors but I guess the problem with my code is that it's not accessing the components I want. It keeps changing the values of the transform values rather than in the script. Thanks again. I'll try and figure it out.
Answer by dentedpixel · Feb 12, 2013 at 02:37 PM
If you are looking to animate GUI elements I would recommend using the tweening engine LeanTween instead. It makes this task very easy. Here is an example of how you can tween a button with very little code:
// Javascript Code
private var bRect:LTRect = new LTRect( 0, 0, 100, 50 );
function OnGUI(){
if(GUI.Button(bRect.rect, “Scale”)){
LeanTween.scale( bRect, Vector2(bRect.rect.width, bRect.rect.height) * 1.3, 0.25 );
}
}
// C# Code
private LTRect bRect = new LTRect( 0f, 0f, 100f, 50f );
void OnGUI(){
if(GUI.Button(bRect.rect, “Scale”)){
LeanTween.scale( bRect, new Vector2(bRect.rect.width, bRect.rect.height) * 1.3f, 0.25f );
}
}
You can find more detailed examples of how to tween GUI Elements here: http://dentedpixel.com/developer-diary/animate-unity-gui-elements-with-ease/
Your answer
Follow this Question
Related Questions
GUI GameObject Button? 1 Answer
iTween.FadeTo - Prevent Going to 100% Alpha? 0 Answers
iTween multiple GUI elements with one callback 1 Answer
iTween and texture problem 0 Answers
New Gui and iTween 1 Answer