Demigiant Forum
Unity Assets => DOTween & DOTween Pro => Topic started by: DarkSlash on September 20, 2015, 12:23:03 AM
-
I have a float variable called balance and I show it in a 2d Toolkit TextMesh. Right now, Im doing something like this to change it:
balance += totalPayment;
UpdateUI();
But I don't want to change the value from A to B. Let's say balance = 50 and totalPayment = 150, I want to increase it 50,51,52,53 (or something like that) until 200 (balance + totalPayment). So the closest I got is this:
float to = balance + totalPayment;
DOTween.To(() => balance, x => balance = x, to, 2).OnComplete(UpdateUI);
Nevertheless, this doesn't work as expected. It doesn't do what I need, it just wait 2 seconds and then change the value of balance to balance+totalPayment. What I'm doing wrong?
EDIT: UpdateUI just take does this: txtBalance.text = balance.ToString("F2");
-
Hi,
You code is absolutely correct and should do the animation you intend. I'm sure there's no bugs in float tweens, so maybe there's something else blocking your tween from happening? Like a method somewhere setting "balance" continuously, or Time.timeScale being set to 0 (in which case you will have to chain a SetUpdate(true) to make the tween independent).
Let me know.
Cheers,
Daniele
-
Im not setting balance from anywhere else and my timeScale is the default. I have this on Start(): DOTween.Init(); do I have to do something else? Attach some script to the gameobject? Or initialice DOTween in another way? I'm calling DOTween.To(...) from a function, do I have to do it from Update() or somewhere special?
-
No you can call it wherever you want. Are you sure that "balance" is not animated? Are you calling something like UpdateUI every frame, to update the UI with the value of "balance"?
DOTween.To(() => balance, x => balance = x, to, 2).OnUpdate(UpdateUI).OnComplete(UpdateUI);
-
Oh, I though that OnComplete was called every time it completes doing every step in the tween but its called when the whole tween is complete. My mistake ;)
Also, there's a way to limit the decimals to just 2 in the floats? Or, if the number has not decimal part (1080), tween without decimals, if the number has decimals (1080.75) tween with decimals. Can this be done?
-
Ah, glad we solved the mystery.
About rounding floats, you could convert them to an int inside UpdateUI, before setting the string. Decimals can't be limited on a float otherwise. That said, when the tween stops the value should be exactly 1180 :O I checked and that works on my side.
-
Sorry, I edited my post while you were replying, I missed the last txtBalance = balance.ToString(); in the OnComplete function ;)
Ok, I can convert to int, tween them, and then add the decimal part. That's the only way?
-
Ah no then. If you want to keep "up to" two decimals, then leave the float as is, and just convert it when you cast it to string:
txtBalance = String.Format("{0:0.##}", balance);
-
Yes I think is the best solution. I got decimals when I tween a round value, but if only two decimals it doesn't look bad. I do ToString("F2") insted of Strinfromat
Thank you for your help!!
-
Great :) Though, if I'm not confused, with that String.Format you shouldn't get any decimal at all if the value is round, and up to two if it's not
-
Oh! Thanks!