• 序列帧动画


    在Unity实现序列帧动画制作的方法:

    1. 编写C#脚本控制
    2. 用UnityShader变换UV

    第一种方法用C#脚本去编写:

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 
     5 public class FragmentAnim : MonoBehaviour {
     6     /// <summary>
     7     /// 动画帧
     8     /// </summary>
     9 
    10     public int fps;         //播放速度
    11 
    12     [SerializeField]
    13     private int rowCount;    //行数
    14     [SerializeField]
    15     private int columCount;  //列数
    16 
    17     private int currentIndex;//当前播放索引值
    18 
    19     void Start()
    20     {
    21         StartCoroutine(Fragment(fps));
    22     }
    23 
    24     IEnumerator Fragment(float fps)
    25     {
    26         Material mat = transform.GetComponent<Renderer>().material;
    27 
    28         currentIndex = rowCount * columCount;
    29         //播放顺序9——>1
    30         //3 2 1
    31         //6 5 4
    32         //9 8 7
    33 
    34         float itemWidth = 1.0f / rowCount;      //每一帧宽度
    35         float itemHeight = 1.0f / columCount;   //每一帧高度
    36 
    37         while (true)
    38         {
    39             float offset_x = currentIndex %columCount * itemWidth;//x偏移量=当前索引值%列数*每帧宽度
    40             float offset_y = currentIndex / rowCount * itemHeight;//y偏移量=当前索引值%行数*每帧高度
    41 
    42             mat.SetTextureScale("_MainTex", new Vector2(itemWidth, itemHeight));//片大小缩放
    43             mat.SetTextureOffset("_MainTex", new Vector2(offset_x, offset_y));//片偏移计算
    44 
    45             yield return new WaitForSeconds(1 / fps);
    46 
    47             currentIndex = (++currentIndex) % (rowCount * columCount);//完成一次循环后索引归零
    48         }
    49     }
    50 }

     注:我的Unity2017.1.0f3版本截图如下

    第二种方法用Shader去编写:

    参考:http://www.cnblogs.com/hellohuan/p/3512784.html,感谢!

     1 Shader "2D/FragmentAnim" {
     2     Properties{
     3         _Color("Main Color", Color) = (1,1,1,1)
     4         _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
     5         _SizeX("列数", Float)  =4
     6         _SizeY("行数", Float)  =4
     7         _Speed("播放速度", Float) = 200
     8     }
     9 
    10         SubShader{
    11         Tags{ "RenderType" = "Opaque" }
    12         LOD 200
    13 
    14         CGPROGRAM
    15 #pragma surface surf Lambert alpha
    16 
    17         sampler2D _MainTex;
    18         fixed4 _Color;
    19 
    20     uniform fixed _SizeX;
    21     uniform fixed _SizeY;
    22     uniform fixed _Speed;
    23 
    24     struct Input {
    25         float2 uv_MainTex;
    26     };
    27 
    28     void surf(Input IN, inout SurfaceOutput o) {
    29 
    30         int index = floor(_Time.x * _Speed);
    31         int indexY = index / _SizeX;
    32         int indexX = index - indexY*_SizeX;
    33         float2 testUV = float2(IN.uv_MainTex.x / _SizeX, IN.uv_MainTex.y / _SizeY);
    34 
    35         testUV.x += indexX / _SizeX;
    36         testUV.y += indexY / _SizeY;
    37 
    38         fixed4 c = tex2D(_MainTex, testUV) * _Color;
    39         o.Albedo = c.rgb;
    40 
    41         o.Alpha = c.a;
    42         //o.Albedo = float3( floor(_Time .x * _Speed) , 1.0, 1.0);
    43     }
    44     ENDCG
    45     }
    46 
    47         Fallback "Transparent/VertexLit"
    48 }

    注:我的Unity2017.1.0f3版本截图如下

  • 相关阅读:
    URL中的特殊字符 + % # & = ? /
    mvc 前端显示文本中的html标签处理
    URL中#号(井号)的作用
    自定义配置节 Section
    vshost.exe.config与.exe.config ;Debug目录与Release目录;bin目录与obj目录
    在MVC过滤器中获取触发的Controller、Action、参数 等
    js设置全局变量 ajax中赋值
    批处理 安装、卸载 window service
    [摘]HttpContext, HttpRequest, HttpResponse, HttpRuntime, HttpServerUtility
    摘要:ASP.NET的路由
  • 原文地址:https://www.cnblogs.com/craft0625/p/7364589.html
Copyright © 2020-2023  润新知