Using iTween effects with OnGUI functions

Hi,
I’ve just started playing with iTween. So far I can animate objects but I’m currently trying to animate a GUIbutton. An example is that I’m trying to scale a button with a tween effect. I know they have an example that you have to buy at the iTween site but I thought I should try to see if I can code it first. Here’s what I’ve managed so far:

var myGUI : GameObject;
var x: int = 180;
var y: int = 350;

function OnGUI() {

GUI.Box (Rect (10,10,x,y), "Tween ");
if (GUI.Button (Rect (20,130,160,20), "MenuSize")) {
	iTween.ScaleTo(myGUI, {"x": 80, "y": 200});
	}

}

There’s no errors but it changes the values of the gameObject’s tranform rather than the one in my script. Tried using myGUI = GetComponent(myGUI); but it does nothing really. How do I access a script’s value and call it from the iTween function?

Hope someone can help.

Cheers.
-Hakimo

I’ve managed to scale down the button window thingy using GUIUtility.ScaleAroundPivot but how do I apply this value using iTween.ValueTo? I’m a bit confused on what to put inside the hashtables…

  • Hakimo

Hey Hakimo,

I haven’t checked out the examples on the iTween site but I could make it work like this.
You create the iTween on the gameObject. You modify the GUI’s Rect value with ValueTo.
The documentation explains all the values you can use in the hash table.

public Rect rect;

void Start () {
	SlideLabel();
}

void OnGUI () {
	GUI.Label(rect, "Options");
}

void SlideLabel() {
	rect = new Rect(350f, 45f, 400f, 50f);
        iTween.ValueTo(gameObject, iTween.Hash("from", offScreenLeft(rect), "to", rect, "delay", 0.5f, "time", 1.25f, "easetype", iTween.EaseType.easeInOutSine, "onupdate", "updateRect"));
		
        // Fix iTween's 'from' position if using 'delay', as it seems to hold on the 'to' position until delay is over
        rect = offScreenLeft(rect);
}

// Set Rect to offscreen position
Rect offScreenLeft ( Rect input )
{
        return new Rect(-100f, input.y, input.width, input.height);
}
	
// Update callback for iTween
void updateRect ( Rect input )
{
        rect = input;
}

Another good reference i found was the iTweenPath tut, this has a great example of iTween.Hash.

http://pixelplacement.com/2010/12/03/visual-editor-for-itween-motion-paths/

Hope this helps.

Thanks Dakka :slight_smile: I got really confused with the hash table. It’s working fine.
-Hakimo