- Home /
Best way to tween Gui Elements?
Im working right now on my GUI and i want to create a Accordion like Select Menu.
I search for the best way to tween GUI Elements.
Are there any existing scripts wich provide this issue? All i found was the iTween script which only provides GameObjects.
Answer by Case23 · Jun 20, 2010 at 07:16 AM
So after thinking about this problem all day long, i came to a solution similar i did with the Tweener Library in FLash sometimes.
Here is the example code "GuiTweenTest.cs":
using UnityEngine; using System.Collections;
public class GuiTweenTest : MonoBehaviour {
private GameObject tweenObject;
// Use this for initialization
void Start ()
{
tweenObject = new GameObject();
}
// Update is called once per frame
void Update () {
}
public void OnGUI()
{
float xPos = tweenObject.transform.localPosition.x;
float yPos = tweenObject.transform.localPosition.y;
if (GUI.Button(new Rect(xPos, yPos, 100, 40), new GUIContent("click me")))
{
iTween.moveTo(tweenObject, 1, null, Random.Range(0, Screen.width-100), Random.Range(0, Screen.height-40), null);
}
}
}
What i basically do is, to create a empty GameObject which holds my Gui Element Position, and with this Position container which is a GameObject, i can use the full advantage of the iTween Library.
I hope you like the idea! :)
No Comments till now. So i mark this as answered with my answer. :)
fine idea....not moveTo...its $$anonymous$$oveTo........it shows an error like Assets/iTween_iTweenPath/Assets/iTweenScripts/GuiTweenTest.cs(25,20): error CS1501: No overload for method $$anonymous$$oveTo' takes
6' arguments
its working now..thank u for ur idea iTween.$$anonymous$$oveTo(tweenObject, iTween.Hash("x",250, "y",50, "time", 6, "easeType", iTween.EaseType.easeInOutSine));
Answer by Tetrad · Jun 19, 2010 at 04:50 PM
There are basically three ways to do it.
1) the hard custom way: use something like AniMate and tween properties in your class that does an OnGUI call.
So without writing the code, have a public offset variable, and use AniMate to tween that offset variable, and in your Rect creation for your buttons, use that offset variable.
2) the hard encapsulated way: Instead of using OnGUI in your own classes, write a UI system that encapsulates everything that Unity GUI does that your project needs. The discussion on this is outside the scope of this question, but the way we did it was basically have a UI Manager that had our single OnGUI call, and then had custom classes like UIWindow and UIButton that handled everything themselves. And inside those buttons they would tween themselves like in 1), but you didn't have to worry about it on the client side.
This might be worth while to do if you have a lot of UI elements as it lets you do things like encapsulate click button sounds, etc.
3) the bypass OnGUI since it doesn't really allow for a lot of advanced features way: Instead of using the UnityGUI, perhaps it would be better to do it in 3D. This is basically what EZ GUI does. Or maybe you do it in 3D with an animation and just use the OnMouseDown method on your button gameObjects as if they were UI elements.
Hi Tetrad, thanks for your answer! What you suggest in your second answer is kind of what i`m working on. I have one single "$$anonymous$$ainGui.cs" clas which will call the onGui() funtion and im creating all my single Gui Conomponents the way i want it so I just have to call them with a simple function call. To the Ani$$anonymous$$ate solution. I took a quick look at this class, but for my taste it is too detailed for what i want. I found a solution which works for me. Have a look at it here.
Answer by pixelplacement · Jun 30, 2010 at 08:32 PM
Interesting, I like the approach. I'm trying to finish up iTween 2.0 now and I'm trying to find a way to make this even easier with the new version If I figure something better for the next release I'll let you know!
As i wrote the example i always thougt. Why can i not simple put a Rect.instance into the iTween $$anonymous$$ethod. Tweening the Rect and refernce it to the Gui element would made i alot easier to use. Just my thoughts. :)
http://unity3d.com/support/documentation/ScriptReference/Rect.html
Answer by pixelplacement · Jul 19, 2010 at 05:13 AM
iTween 2.0 will allow you to do generic value tweens iTween.ValueTo() and it can do everything from rects and colors to just float doubles and vectors you combine the with a callback it paves the way for easier GUI animations. Sound good?
Answer by pixelplacement · Aug 11, 2010 at 06:22 PM
There's a new example on the iTween support site which shows how to animate GUI elements: here
I will check out your new version as fast as possible. :) $$anonymous$$eep up the good Work! I realy like iTween!