- Home /
iTween ValueTo onupdateparam help
I am trying to make a generic ValueTo method using iTween where I can add a Text
, from
and to
value as params so I don't have to create a new method for the hundreds of text objects in my game. I tried this but run into several issues:
void Start() {
CountTo(myText1, 0, 50);
CountTo(myText2, 0, 25);
}
public void CountTo (Text text, float startValue, float endValue) {
iTween.ValueTo(text.gameObject, iTween.Hash(
"from", startValue,
"to", endValue,
"time", 2f,
"onupdatetarget", gameObject,
"onupdate", "OnCountTo",
"onupdateparams", iTween.Hash("gameObject", text.gameObject),
"easetype", iTween.EaseType.easeOutQuad
)
);
}
public void OnCountTo (object param) {
var hstbl = param as Hashtable; //doesn't work because param is always a float
Debug.Log("param: " + param); // prints 0-50 over 2 seconds
var go = (GameOject) hstbl["gameObject"];
go.GetComponent().text = param.toString();
}
Firstly, param
is always the object type needed, in this case always a float. I cannot for the life of me figure out how to add an additional variable to the object. In my mind "onupdateparams", iTween.Hash("gameObject", text.gameObject),
should work just fine. And even so, how do I then access it in my OnCountTo method if that param is always a float?
What am I doing wrong?
Answer by pixelplacement · Jul 16, 2018 at 03:30 PM
Hey! I'd STRONGLY recommend you upgrade from my iTween to my Surge framework. Surge has a brand new Tween system that is far superior to iTween - which is about 7 years old now. That said, here's how you would solve this painlessly with Surge:
using UnityEngine; using UnityEngine.UI; using Pixelplacement;
public class SurgeText : MonoBehaviour { public Text myText1; public Text myText2;
private void OnGUI()
{
if (GUILayout.Button("Count 1")) CountTo(myText1, 0, 50);
if (GUILayout.Button("Count 2")) CountTo(myText2, 0, 20);
}
private void CountTo(Text text, float start, float end)
{
Tween.Value(start, end, (float value) => text.text = value.ToString(), 1, 0);
}
}
Thanks so much! Never knew about Surge, it looks amazing and will replace it with iTween asap!