为了熟悉CaptureScreen方法,做一个艺术字系统。
首先声明一个公共变量toward,以便在书脚本Cature_Use中调用,然后给另一个变量delays赋予一个随机值,以便每个小球的运动显得更加真实
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Capture_Use_Sub : MonoBehaviour
{
//物体移动的目标位置
public Vector3 toward;
float delays;
void Start()
{
//获取一个随机值
delays = Random.Range(2.0f, 4.0f);
}
void Update()
{
//通过更改物体位置来达到物体运动的效果
transform.position = Vector3.MoveTowards(transform.position, toward, delays);
}
}
接着创作主程序
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Capture_Use : MonoBehaviour
{
//td:用来指向屏幕截图
Texture2D td = null;
//txt_bg:用来指向文本输入的北京图片
//可以指向一张纯色的图片,也可以自定义一个纯色的北京
//本程序自定义了一个纯色的背景
Texture2D txt_bg;
//txt_w和txt_h用来记录文本框的宽度和高度
int txt_w, txt_h;
//my_save_path:用来记录截图的保存路径
string my_save_path = "";
//show_txt:用来记录输入的文本
string show_txt = "在此输入";
//myh_colors:用来记录文本输入框的纹理颜色
Color[] colors;
// _w, _h;用来记录my_color的宽度和高度
int _w, _h;
//step:用来记录当前状态,step共有4种状态
//step=0时,是等待状态,等待在文本框中输入文本
//step=1时,即点击“确定”按钮后,生成截图并保存
//step=2时,读取截图信息
//step=3时,是对读取的截图信息进行有选择的提取,并生成想要展示的北荣
int step = 0;
//go:用来指向拼字所用物体对象
public GameObject go;
//gos:用来记录所有的go对象
GameObject[] gos;
//gos_max用来记录gos的最大数量
int gos_max;
//gos_cur用来记录gos当前数量
int gos_cur = 0;
//is_show:用来记录图像矩阵中某个点是否需要展示物体
bool[,] is_show;
void Start()
{
//初始化文本框的宽度和高度
txt_w = 200;
txt_h = 80;
//初始化截取区间的大小,维乐避免边界识别错误,其值应该比文本框的宽度和高度少几个像素
_w = txt_w;
_h = txt_h;
_w -= 5;
_h -= 5;
//自定义txt_bg纹理
txt_bg = new Texture2D(txt_w, txt_h);
Color[] tdc = new Color[txt_w * txt_h];
for (int i = 0; i < txt_w * txt_h; i++)
{
//所用像素点颜色相同
tdc[i] = Color.white;
}
txt_bg.SetPixels(0, 0, txt_w, txt_h, tdc);
is_show = new bool[_h, _w];
//初始化gos_max,其值大小为txt_w * txt_h的三分之一
gos_max = _w * _h / 3;
//实例化gos,使其随机分布_w, _h的区间内
gos = new GameObject[gos_max];
for (int i = 0; i < gos_max; i++)
{
gos[i] = (GameObject)Instantiate(go, new Vector3(Random.value * _w, Random.value * _h, 10.0f), Quaternion.identity);
gos[i].GetComponent<Capture_Use_Sub>().toward = gos[i].transform.position;
}
//存储出事界面截图
my_save_path = Application.persistentDataPath;
ScreenCapture.CaptureScreenshot(my_save_path + "/test02.png");
}
void Update()
{
switch (step)
{
case 0:
break;
case 1:
break;
step = 0;
//截图并保存
my_save_path = Application.persistentDataPath;
ScreenCapture.CaptureScreenshot(my_save_path + "/test02.png");
//给计算机一点时间用来保存新截图的图片
Invoke("My WaitForSeconds", 0.4f);
Debug.Log(my_save_path);
break;
case 2:
//由于在读取截图纹理的时候,一帧之内可能无法读完
//所以需要step=0,避免逻辑出现混乱
step = 0;
//读取截图信息
my_save_path = Application.persistentDataPath;
//StartCoroutine(WaitLoad(my_save_path + "/test02.png"));
break;
case 3:
//在计算并生成展示信息的时候,一帧之内可能无法读完
//所以需要step=0,避免逻辑出现混乱
step = 0;
//计算并生成展示信息
//Cum();
break;
}
}
}