Unity 获取Animation,Animator,SkeletonAnimation的状态

Animation

#region  //控制Animation的播放
    private void onAnimationGui()//(GameObject obj)
    {
        Animation animation = gameObject.transform.GetComponentInChildren<Animation>();  
        // Animator animator = obj.transform.GetComponentInChildren<Animator>(); 
        int count = 0;
        if(animation.gameObject.transform.parent.gameObject.activeSelf)
        {
            foreach(AnimationState _state in animation)
            {                
                if (GUI.Button(new Rect(Vector2.up * 60*(count+1), new Vector2(150, 50)), "Play  "+_state.name))
                {
                    if(null != _state)
                    {
                        animation.enabled = true;
                        animation.Play(_state.name);
                        Debug.Log("当前播放的是"+animation.name+"--------------"+_state.name+"----动画");                
                    }
                }                
                count += 1;
                Debug.Log("clips.lenght=======333333333333333333333333333333333333333333=====animation"+animation);
            }                 
            if (GUI.Button(new Rect(Vector2.up * 60*(count+1), new Vector2(150, 50)), "重置Reset"))                
            {
                gameObject.transform.localPosition = originPos;
                Debug.Log("重置回初始状态");
            }   
            Debug.Log("clips.lenght=======44444444444444444444444444444444444444444444444444=====");

            setScaleController(count +2,animation.gameObject);
            Debug.Log("clips.lenght=======44444444444444444444444444444444444444444444444444====="); 
        }                     
    }
    #endregion

Animator


#region  //控制Animator的播放
    private void onAnimatorGui()//(GameObject obj)
    {
        Animator animator = gameObject.transform.GetComponentInChildren<Animator>();  
        // Animator animator = obj.transform.GetComponentInChildren<Animator>(); 
        if(animator.gameObject.transform.parent.gameObject.activeSelf)
        {
            AnimationClip[] clips = animator.runtimeAnimatorController.animationClips;                    
            for(int i = 0;i<clips.Length;i++)
            {                
                //根据动画名字 添加对应的动画按钮
                if (GUI.Button(new Rect(Vector2.up * 60*(i+1), new Vector2(150, 50)), "Play  "+clips[i].name))
                {
                    // GUI.GUIStyle.fontSize = 60;
                    if(null != animator)
                    {
                        animator.enabled = true;
                        animator.Play(clips[i].name);                        
                    }
                }                                
            }        
            if (GUI.Button(new Rect(Vector2.up * 60*(clips.Length+1), new Vector2(150, 50)), "重置Reset"))                
            {
                gameObject.transform.localPosition = originPos;
                Debug.Log("重置回初始状态");
            }   
            setScaleController(clips.Length,animator.gameObject);
            Debug.Log("clips.lenght=======44444444444444444444444444444444444444444444444444====="); 
        }                     
    }
    #endregion

SkeletonAnimation

#region 控制spine的播放
    private void onSpineGui()
    {
        SkeletonAnimation spineAni = gameObject.transform.GetComponentInChildren<SkeletonAnimation>();          
        int count = 0;
        if(spineAni.gameObject.transform.parent.gameObject.activeSelf)
        {
            var aa = spineAni.skeleton.data.Animations.ToArray();
            for(int i = 0;i<aa.Length;i++)
            {
                if (GUI.Button(new Rect(Vector2.up * 60*(count+1), new Vector2(150, 50)), "Play  "+aa[i].name))
                {
                    if(null != spineAni)
                    {
                        spineAni.GetComponent<SkeletonAnimation>().enabled = true;
                        spineAni.state.SetAnimation(0, aa[i].name, true);
                        Debug.Log("当前播放的是"+spineAni.GetComponent<SkeletonAnimation>().name+"--------------"+aa[i].name+"----动画");                
                    }
                }                
                count += 1;                
            }
                         
            if (GUI.Button(new Rect(Vector2.up * 60*(count+1), new Vector2(150, 50)), "重置Reset"))                
            {
                gameObject.transform.localPosition = originPos;
                Debug.Log("重置回初始状态");
            }               
            setScaleController(count +2,spineAni.gameObject);            
        }                     
    }
    #endregion

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值