- Home /
Indirect Coroutine Behaviors with UI Buttons, Animator, and SetActive
I've got a menu system setup using UI animations as transitions that overlap (becoming visible/invisible states when they're done), but since they're all still active while invisible so transitions can blend, they're using up a lot of processing power.
I thought using Coroutines initiated via a uGUI button's OnClick event (from inside my ShowMenu function) was the perfect solution to handle checking if the animation had finished, and if so, disable the invisible gameobject.
But, apparently, Coroutines return a value, making them unusable inside uGUI button events.
I worked around this by doing the following, but the animator is now only showing 0 for its normalizedtime and thus the prior menu's animator state transition isn't happening for some strange reason (yet, without the coroutine, it's fine!) :
public function ShowMenu(menu: Menu) {
if (CurrentMenu != null) {
CurrentMenu.IsOpen = false;
var OldMenu : GameObject = CurrentMenu.gameObject ;
var OldMenuAnimator : Animator = OldMenu.GetComponent.<Animator>() ;
}
CurrentMenu = menu;
if (CurrentMenu.gameObject.activeSelf == false) CurrentMenu.gameObject.SetActive(true) ;
CurrentMenu.IsOpen = true;
//Coroutine
MyCoroutine(OldMenu) ;
}
//Coroutine
private var isRunning: boolean = false;
private function MyCoroutine(oldmenu: GameObject): IEnumerator {
isRunning = true;
// Do your stuff over multiple frames
var oldmenuAnimator : Animator = oldmenu.GetComponent.<Animator>() ;
while (oldmenuAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1) {
Debug.Log(oldmenuAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime.ToString() );
yield ;
}
oldmenu.SetActive(false);
isRunning = false;
}
Javascript is a scary shit - I can not say whether Show$$anonymous$$enu is converted to a coroutine or not. Does $$anonymous$$yCoroutine get called at all, i.e. if you add Debug.Log to it does it print?
Debug.Log prints, but it always shows 0 and prints constantly (probably due to the "while" and the "SetActive(false);" which seems to be called immediately (completely ignoring the animation from the previous menu, leaving it's normalizedtime at 0).
$$anonymous$$aybe something's funky internally with the coroutine/animator in the context that the "Show$$anonymous$$enu" function's script being called from the UI button's OnClick event. Since it's a special calling case that accepts only 1 argument, argument has to be a unity gameobject, and it can't return a value (which makes this a non-standard way of calling a script), is there something else that could be bothering/stopping the coroutine/animator in there?
Your answer
Follow this Question
Related Questions
Unity 4.6 UI Button Click Animation? 4 Answers
How to make a button that opens and closes a tab? 1 Answer
RectTransform.Rotate with custom speed 1 Answer
Menu Animation 2 Answers
World Space not displaying my UI. Any suggestions? 2 Answers