Unity 序列帧补帧

因为最近项目使用序列帧做动画,然后美术提供的图片数量较少图片较大,图集放不下。所以写一小段代码用来生成中间插帧。 效果感觉还可以有种动态模糊的感觉。但是生成时性能堪忧。如果真的要使用的话可以提前在游戏读条的时候生成出来存着。播放序列帧的时候正常播放就行。

逻辑就是根据两张图片的像素颜色做颜色插值,太复杂做法的我也不懂,做到现在这个程度又正好满足我的需求  。          ;)

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

public class TestScript : MonoBehaviour
{

    public Sprite sprite1;
    public Sprite sprite2;

    public Image image;

    // Start is called before the first frame update
    void Start()
    {
        image.sprite = CreatorSprite(sprite1, sprite2);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public Sprite CreatorSprite(Sprite spriteA, Sprite spriteB)
    {
        Color[] GetPixelsA = spriteA.texture.GetPixels();

        Color[] GetPixelsB = spriteB.texture.GetPixels();

        int GetPixelsA_Length = GetPixelsA.Length;
        int GetPixelsB_Length = GetPixelsB.Length;

        int maxCount = GetPixelsA_Length;

        Color[] colors = new Color[maxCount];

        Color begin;
        Color end;
        for (int i = 0; i < maxCount; i++)
        {
            if (GetPixelsA_Length > i)
            {
                begin = GetPixelsA[i];
            }
            else
            {
                begin = Color.clear;
            }

            if (GetPixelsB_Length > i)
            {
                end = GetPixelsB[i];
            }
            else
            {
                end = Color.clear;
            }

           colors[i] = Color.Lerp(begin, end, 0.5f);
        }

        int w = spriteA.texture.width;
        int h = spriteA.texture.height;

        Rect rect = spriteA.rect;


        Texture2D texture2D = new Texture2D(w, h);
        texture2D.SetPixels(colors);
        texture2D.Apply();

        Sprite sprite = Sprite.Create(texture2D, rect, new Vector2(0.5f, 0.5f));

        return sprite;
    }


}


 

运行时如果有以下报错,点击对应图片勾选Read/Write Enable。

UnityException: Texture '1' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.】解决方法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值