Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411500 Posts in 69373 Topics- by 58429 Members - Latest Member: Alternalo

April 25, 2024, 02:37:18 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Keep animation state
Pages: [1]
Print
Author Topic: Keep animation state  (Read 916 times)
davejones
Level 0
**


View Profile
« on: August 16, 2018, 03:05:44 AM »

I am developing a UI drop down list in Unity3D.  How do I make the animator stay on animation state rather than resetting to idle on re enable of GameObject?  
 
I have listed below the issue in points

- press UI button that plays drop down animation clip
- press same UI button again and fold up animation clip is played
- press UI button and drop down plays again
- set GameObject active to false when the drop down clip has been played
- Set GameObject to true whilst the list is dropped down
- press button to try and fold up but doesn't work like before GameObject was set to false.

I have attached the class used OnClick of the UI button below. I want the same behaviour that was apparent before the GameObject was set to false. The link is to a video that shows the issue.

https://gfycat.com/gifs/detail/skinnynegativechuckwalla
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class CustomDropDown : MonoBehaviour {
 
    public GameObject SubMenu;
    [HideInInspector]
    public int State = 0;
 
    Animator AniPlayer;
 
    public void BtnDropDown()
    {
        if (AniPlayer.GetInteger("MenuState") == 0)
        {
            AniPlayer.SetInteger("MenuState", 1);
        }
 
 
        else if (AniPlayer.GetInteger("MenuState") == 1)
        {
            AniPlayer.SetInteger("MenuState", 0);
        }
    }
        // Use this for initialization
        void Start() {
            AniPlayer = this.GetComponent<Animator>();
        }
 
    }
« Last Edit: August 16, 2018, 10:10:02 AM by ThemsAllTook » Logged
davejones
Level 0
**


View Profile
« Reply #1 on: August 18, 2018, 12:08:59 PM »

Psuedocode

onEnable

menu state = current state

OnDisable

menu state = current state

 

I suppose its just a case of finding out how to do this because I believe the issue is that the animation state machine  resets on disabling/ re enabling of the GameObject.
Logged
Ordnas
Level 10
*****



View Profile WWW
« Reply #2 on: August 19, 2018, 03:03:50 AM »

Yes, from the video it seems that everytime a GameObject enables, it will call some Reset function that will reset all the states of each component (in this case the Animator Controller). For sure in your code you should save your state everytime  BtnDropDown() is called:

private int MenuState = 0;

public void BtnDropDown()
    {
        if (AniPlayer.GetInteger("MenuState") == 0)
        {
            AniPlayer.SetInteger("MenuState", 1);
            MenuState = 1;
        }
   [...]

To set the cached value again every OnEnable, you could try to add it on the Start() function (I suppose it will be called every OnEnable, you could try and see if it works):

void Start() {
            AniPlayer = this.GetComponent<Animator>();

            if (AniPlayer != nullptr) AniPlayer.SetInteger("MenuState", MenuState);
        }
Logged

Games:

besttof
Level 0
**

besttof


View Profile WWW
« Reply #3 on: October 06, 2018, 12:01:11 PM »

OR you set
Code:
AniPlayer.keepAnimatorControllerStateOnDisable = true
. They only added that pretty recently, but it seems the best way to do it  Wink
Logged
TonyLi
Level 0
***


View Profile
« Reply #4 on: October 06, 2018, 04:15:58 PM »

This is more of a side note, but if you're concerned about performance, avoid putting Animators on UI elements unless they actually change state every single frame. Animators dirty the layout every frame, forcing a recalc every frame. (reference)

Instead, you could use your own tween code (or something like DOTween), which would also let you retain state between enables and disables.

Or use an Animation or Simple Animation component with the clip's wrap mode set to Once. They disable themselves when they're done playing the clip, so they don't keep dirtying the UI every frame.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic