准备资料:AE、视频素材
视频素材地址 https://cloud.189.cn/t/naAviyrY3QVr (访问码:eyz9)
新建合成,导入素材,添加效果颜色键,扣除白色。

为了效果明显,加点位移动画

ctrl +m 导出
格式选择png序列,降低采样,帧率为5,点击渲染




导入Unity

读取单张图片
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;
public class GameEnter : MonoBehaviour
{
const int width = 16*4;
const int height = 9*4;
public Transform parent;
public Image itemPrefab;
public Texture2D[] texture;
const float padding=3;
readonly Vector2 size = new Vector2(23, 23);
public int samplingStep = 1;
private Image[,] itemList = new Image[width, height];
// Start is called before the first frame update
void Start()
{
itemPrefab.rectTransform.sizeDelta = size;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
var img = GameObject.Instantiate(itemPrefab, new Vector3((i ) * (size.x + padding), (j) * (size.y + padding)),Quaternion.identity,parent);
img.gameObject.SetActive(true);
itemList[i, j] = img;
}
}
Caculate(texture[30]);
}
void Caculate(Texture2D texture,int idx)
{
for (int i = 0; i < colored.Count; i++)
{
colored[i].color = Color.white;
}
colored.Clear();
var widthStep = texture.width / samplingStep;
var heightStep = texture.height / samplingStep;
for (int i = 0; i <= heightStep; i += samplingStep)
{
for (int j = 0; j <= widthStep; j += samplingStep)
{
int colorPixelCnt = 0;
for (int ii = 0; ii <= samplingStep; ++ii)
{
for (int jj = 0; jj <= samplingStep; ++jj)
{
var color = texture.GetPixel(j * samplingStep + jj, i * samplingStep + ii);
if (color.r + color.g + color.b >= 1f)
{
++colorPixelCnt;
}
}
}
if (colorPixelCnt > samplingStep)
{
var x = j * (width-1) / widthStep;
var y = i * (height-1) / heightStep;
itemList[x, y].color = Color.red;
}
}
}
}
}
效果

读取连续图片
void Start()
{
itemPrefab.rectTransform.sizeDelta = size;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
var img = GameObject.Instantiate(itemPrefab, new Vector3((i ) * (size.x + padding), (j) * (size.y + padding)),Quaternion.identity,parent);
img.gameObject.SetActive(true);
itemList[i, j] = img;
}
}
StartCoroutine(LoadCorount());
}
IEnumerator LoadCorount()
{
for (int i = 0; i < texture.Length; i++)
{
Caculate(texture[i],i);
yield return new WaitForSeconds(0.3f);
}
}
private List<Image> colored = new List<Image>();
void Caculate(Texture2D texture,int idx)
{
for (int i = 0; i < colored.Count; i++)
{
colored[i].color = Color.white;
}
colored.Clear();
var widthStep = texture.width / samplingStep;
var heightStep = texture.height / samplingStep;
for (int i = 0; i <= heightStep; i += samplingStep)
{
for (int j = 0; j <= widthStep; j += samplingStep)
{
int colorPixelCnt = 0;
for (int ii = 0; ii <= samplingStep; ++ii)
{
for (int jj = 0; jj <= samplingStep; ++jj)
{
var color = texture.GetPixel(j * samplingStep + jj, i * samplingStep + ii);
if (color.r + color.g + color.b >= 1f)
{
++colorPixelCnt;
}
}
}
if (colorPixelCnt > samplingStep)
{
var x = j * (width-1) / widthStep;
var y = i * (height-1) / heightStep;
itemList[x, y].color = Color.red;
colored.Add(itemList[x, y]);
}
}
}
}
由于读写texture比较消耗性能,将读取到的信息存储为json
void Start()
{
itemPrefab.rectTransform.sizeDelta = size;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
var img = GameObject.Instantiate(itemPrefab, new Vector3((i ) * (size.x + padding), (j) * (size.y + padding)),Quaternion.identity,parent);
img.gameObject.SetActive(true);
itemList[i, j] = img;
}
}
//执行一次之后就不需要了
for (int i = 0; i < texture.Length; i++)
{
Caculate(texture[i],i);
}
File.WriteAllText(Application.dataPath+"/../info.json",JsonUtility.ToJson(newit));
newit = JsonUtility.FromJson<Items>(File.ReadAllText( Application.dataPath + "/../info.json"));
StartCoroutine(LoadCorount());
}
Items newit = new Items();
IEnumerator LoadCorount()
{
for (int i = 0; i < newit.items.Count; i++)
{
for (int l = 0; l < colored.Count; l++)
{
colored[l].color = Color.white;
}
colored.Clear();
for (int j = 0; j < newit.items[i].pixels.Count; j++)
{
var pix = newit.items[i].pixels[j];
itemList[pix.x, pix.y].color = Color.red;
colored.Add(itemList[pix.x, pix.y]);
}
yield return new WaitForSeconds(0.3f);
}
}
private List<Image> colored = new List<Image>();
void Caculate(Texture2D texture,int idx)
{
Item item = new Item
{
idx = idx
};
var widthStep = texture.width / samplingStep;
var heightStep = texture.height / samplingStep;
for (int i = 0; i <= heightStep; i += samplingStep)
{
for (int j = 0; j <= widthStep; j += samplingStep)
{
int colorPixelCnt = 0;
for (int ii = 0; ii <= samplingStep; ++ii)
{
for (int jj = 0; jj <= samplingStep; ++jj)
{
var color = texture.GetPixel(j * samplingStep + jj, i * samplingStep + ii);
if (color.r + color.g + color.b >= 1f)
{
++colorPixelCnt;
}
}
}
if (colorPixelCnt > samplingStep)
{
var x = j * (width-1) / widthStep;
var y = i * (height-1) / heightStep;
item.pixels.Add(new Pixel(x, y));
}
}
}
newit.items.Add(item);
}
[Serializable]
class Items
{
public List<Item> items = new List<Item>();
}
[Serializable]
class Item
{
public int idx;
public List<Pixel> pixels=new List<Pixel> ();
}
[Serializable]
class Pixel
{
public int x;
public int y;
public Pixel(int x, int y)
{
this.x = x;
this.y = y;
}
}
效果

网友评论