Unity3D根据模型动画制作2D帧动画图

Unity3D根据模型动画制作2D帧动画图_第1张图片


using UnityEngine;
using System.Collections;
using System.IO;

public class FrameAnimation : MonoBehaviour
{
    public Texture2D image;
    public int w;
    public int h;
    public float nextTime = 0.0f;
    public float rate = 0.3f;
    int i = 0;
    // Use this for initialization
    void Start()
    {
        w = Screen.width;
        h = Screen.height;
        image = new Texture2D(w,h);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0)&& Time.time > nextTime)
        {
            nextTime = Time.time + rate;
            i++;
            StartCoroutine(SaveImage(i));
        }
    }

    IEnumerator SaveImage(int i)
    {
        yield return new WaitForEndOfFrame();  
        image.ReadPixels(new Rect(0, 0, w, h), 0, 0, true);
        image.Apply();
        byte[] bytes = image.EncodeToPNG();
        File.WriteAllBytes(Application.streamingAssetsPath + "/"+i + ".png", bytes);
        yield return null;
    }
}


你可能感兴趣的:(unity3d,帧动画)