unity 通过图片的黑色识别,生成对应的txt文本
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using Random = System.Random;
public class Text1 : MonoBehaviour
{
public Texture2D texture;
void Start()
{
var str = "";
for (int i= texture.width; i>0; i--) {
for (int j= texture.height; j>0;j--) {
Color c = texture.GetPixel(i, j);
if (c.r < 0.1f)
{
str = str + GetRandomString();
}
else {
str = str + " ";
}
}
str = str + "\n";
}
StreamWriter sw;
FileInfo fi = new FileInfo(Application.dataPath + "txt.txt");
sw = fi.CreateText();
sw.WriteLine(str);
sw.Close(