赞
踩
因为最近项目使用序列帧做动画,然后美术提供的图片数量较少图片较大,图集放不下。所以写一小段代码用来生成中间插帧。 效果感觉还可以有种动态模糊的感觉。但是生成时性能堪忧。如果真的要使用的话可以提前在游戏读条的时候生成出来存着。播放序列帧的时候正常播放就行。
逻辑就是根据两张图片的像素颜色做颜色插值,太复杂做法的我也不懂,做到现在这个程度又正好满足我的需求 。 ;)
- 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.】解决方法
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。