zoukankan      html  css  js  c++  java
  • 关于Unity中的帧动画组件的编写

    一、帧动画

    1: 美术准备好一个连续动作的离散图片;
    2: 程序在准确的时间来切换这个图片;
    3: 优点: 简单,速度快;
    缺点:资源占用相对过大;

    二、frame_anim组件编写

    1: 代码里面强制要求加入Image组件;[RequireComponent(typeof(Image))]
    2: 帧动画的参数:
    (1) Sprite数组;
    (2) 间隔时间;
    (3) 是否循环播放;
    (4) 是否在加载的时候播放;
    3: 根据时间让Image组件显示正确的帧;

    三 、实例步骤

    1.创建一个Canvas

    2.对Canvas进行初始化

    3.创建一个Image的UI节点作为Canvas的子节点,名字叫bg,拖进背景图片到这个节点中。

    4.把帧动画贴图放进一个anim的文件夹中,anim在Resources文件夹下

    5.创建一个空节点叫anim,在canvas节点下面。

    6.创建一个脚本frame_anim,挂载在anim节点下面。

    7.frame_anim脚本内容

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    
    // 我们当前代码强制要求要加入一个Image组件,
    // 如果没有Image组件,那么自动加上,如果有就使用;
    // 如果你的代码要求这个节点必须挂某个组件,那么
    // 使用RequireComponent
    [RequireComponent(typeof(Image))]
    
    public class frame_anim : MonoBehaviour {
        // 我们这个动画所需要的画面;
        public Sprite[] sprite_frames;
        // 帧动画的间隔时间
        public float duration = 0.1f;
        // 是否循环播放
        public bool is_loop = false;
        // 是否在加载的时候开始播放;
        public bool play_onload = false;
    
        private float played_time;
        private bool is_playing = false;
    
        private Image img;
        // Use this for initialization
        void Start () {
            this.img = this.GetComponent<Image>();
            if (this.play_onload) {
                if (this.is_loop) {
                    this.play_loop();
                }
                else {
                    this.play_once();
                }
            }
        }
    
        // 只播放一次
        void play_once() {
            if (this.sprite_frames.Length <= 1) {
                return;
            }
    
            this.played_time = 0;
            this.is_playing = true;
            this.is_loop = false;
        }
    
        // 循环播放
        void play_loop() {
            if (this.sprite_frames.Length <= 1) {
                return;
            }
            this.played_time = 0;
            this.is_playing = true;
            this.is_loop = true;
        }
        // 停止当前的动画播放
        void stop_anim() {
            this.is_playing = false;
        }
        // Update is called once per frame
        void Update () {
            if (this.is_playing == false) {
                return;
            }
    
            // 
            float dt = Time.deltaTime;
            this.played_time += dt;
            // 向下取整;
            int index = (int)(this.played_time / this.duration);
            if (this.is_loop == false) {
                // 结束了
                if (index >= this.sprite_frames.Length) { // 停止播放
                    this.is_playing = false;
                    this.played_time = 0;
                }
                else {
                    this.img.sprite = this.sprite_frames[index];
                }
            }
            else {
                // 超过了范围,减掉一个周期
                while (index >= this.sprite_frames.Length) {
                    this.played_time -= (this.duration * this.sprite_frames.Length);
                    index -= this.sprite_frames.Length;
                }
    
                this.img.sprite = this.sprite_frames[index];
            }
            // end 
        }
    }

    8.在Inspetor视图的frame_anim脚本里面展开Sprite_frame,size设置为10,把10张帧图片拖进去

    9.在Inspetor视图的frame_anim脚本下勾选play_onload,选择是否勾选is_loop循环播放,设置动画速率Duration,然后点击运行。

  • 相关阅读:
    HDU 2955 Robberies
    HDU 2546 饭卡
    poj 3628 Bookshelf 2
    poj 3624 Charm Bracelet
    celery -1
    ☀【Grunt】插件
    【MooTools】
    【Grunt】
    ↗☻【PHP与MySQL程序设计 #BOOK#】第3章 PHP基础
    【CSS】
  • 原文地址:https://www.cnblogs.com/HangZhe/p/6905847.html
Copyright © 2011-2022 走看看