如何在菜单中制作动画按钮(统一)

时间:2018-03-27 22:30:16

标签: unity3d unityscript

我制作了一个游戏场景,其中包含游戏,设置等菜单基本选项,但是让这对用户更具吸引力。 我想要这个按钮的简单动画。 我不希望悬停或onclick动画我希望按钮重复动画,而用户在家庭场景中动画可以换色或按钮改变他在屏幕上的大小或位置(我不知道这个正确的名字,我想像一个眨眼效果) 我不知道它是否有用,但它是一个2D项目

2 个答案:

答案 0 :(得分:0)

如果您使用的是Unity UI(我认为您是这样),您只需更改过渡模式并为其添加动画制作。

查看关于此主题的官方Unity教程: https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-transitions

答案 1 :(得分:0)

花了我一段时间才能弄清楚如何从Unity UI中的动画制作动画。由于这是OP似乎要问的问题,因此我想为感兴趣的人发布我的解决方案。只需将其附加到带有动画和图像组件的游戏对象即可。动画制作者可以使用 Reset()功能进行交换和刷新,并可以使用 Flush()功能将其关闭。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEditor;

public class ImageAnimation : MonoBehaviour {

  public float speed;

  private Animator animator;
  private Image image;
  private int curIdx;
  private float curTime;
  private List<Sprite> sprites;
  private bool valid;

  void Awake() {
    image = GetComponent<Image> ();
    Color white = Color.white;
    white.a = 1;
    image.color = white;
    animator = GetComponent<Animator> ();
    if (animator.runtimeAnimatorController == null) {
      valid = false;
    } else {
      sprites = GetSpritesFromAnimator(animator);
      curIdx = 0;
      curTime = speed;
      valid = true;
    }
  }

  public void Reset(){
    Awake();
  }

  public void Flush(){
    Color white = Color.white;
    white.a = 0;
    image.color = white;
    valid = false;
  }

  void Update () {
     if (valid) {
       curTime -= Time.deltaTime;
       if ( curTime < 0 )
       {
          curTime = speed;
          if (curIdx >= sprites.Count){
            curIdx = 0;
          }
          image.sprite = sprites[curIdx];
          curIdx++;
       }
     }
  }

  #if UNITY_EDITOR
   public static List<Sprite> GetSpritesFromAnimator(Animator anim)
   {
       List<Sprite> _allSprites = new List<Sprite> ();
       foreach(AnimationClip ac in anim.runtimeAnimatorController.animationClips)
       {
           _allSprites.AddRange(GetSpritesFromClip(ac));
       }
       return _allSprites;
   }

   private static List<Sprite> GetSpritesFromClip(AnimationClip clip)
   {
       var _sprites = new List<Sprite> ();
       if (clip != null)
       {
           foreach (var binding in AnimationUtility.GetObjectReferenceCurveBindings (clip))
           {
               ObjectReferenceKeyframe[] keyframes = AnimationUtility.GetObjectReferenceCurve (clip, binding);
               foreach (var frame in keyframes) {
                   _sprites.Add ((Sprite)frame.value);
               }
           }
       }
       return _sprites;
   }
  #endif
}
相关问题