Unity学习笔记–读取txt数据并绘制
unity的场景制作中,我们有时候需要使用大量的数据来生成我们的场景,当数据行为几千行至几万行的时候,直接调用json与txt数据更为方便,今天做下txt文档数据的使用总结。
1. 文档内容格式
文档是我们某处的位置数据,1@表示第一组,后面跟的是X、Y坐标,然后是高度范围。最后的字母表示形状标识如第一行表示第一组数据,坐标为(47056,-15464)高度为20-30之间形状为P类型的一块地方。
在unity场景下画出类似地图一样的网格
##2. 内容读取与处理
新建TextAsset然后自己起个名字。调用的内容最好都放在Resources里,方便使用:
TextAsset text = Resources.Load(“ENC_DEPARE_POLYGON”);
按自己的需求使用Split将文档拆分并存储,
string[] arr = cowNo[i].Split(’@’);
string[] posArr = arr[1].Split(’,’);
float x = float.Parse(posArr[0]);
float z = float.Parse(posArr[1]);
float depth = float.Parse(posArr[3]);
string typeStr = posArr[4].Trim();
##3. 使用LineRenderer绘制
lineRenderer.startColor = Color.blue;//设置起始颜色
lineRenderer.endColor = Color.blue;//设置结束颜色
lineRenderer.startWidth = 20;//设置起始宽度
lineRenderer.endWidth = 20;//结束宽度
lineRenderer.material= SetColor(depth);//根据高度(深度)值选择颜色,这句是自己定义的函数
lineRenderer.positionCount = n;//设置点的全部数量
lineRenderer.SetPosition();//设置位置
##4. 补充Dictionary的使用
当我们有很多数据要频繁调用的时候,使用Dictionary可以大大方便我们的工作:
可以理解为key到value的映射。
Dictionary<key, value> Dict = new Dictionary<key, value>();
当读取到Dict[key]时,便可直接调用映射的value值;
##5. 完整代码
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class DrawLine2 : MonoBehaviour
{
private List<Material> lineRdrMaterials = new List<Material>();
Dictionary<int, Dictionary<LineType, LineRenderer>> lineRdrDict = new Dictionary<int,
Dictionary<LineType, LineRenderer>>();//使用了二级Dictionary;
// Start is called before the first frame update
void Start()
{
lineRdrMaterials.Add(Resources.Load<Material>("line0"));
lineRdrMaterials.Add(Resources.Load<Material>("line1"));
lineRdrMaterials.Add(Resources.Load<Material>("line2"));
lineRdrMaterials.Add(Resources.Load<Material>("line3"));
lineRdrMaterials.Add(Resources.Load<Material>("line4"));
lineRdrMaterials.Add(Resources.Load<Material>("line5"));
DealText();
}
void DealText()
{
TextAsset text = Resources.Load<TextAsset>("ENC_DEPARE_POLYGON");
string[] cowNo = text.ToString().Split('\n');
for (int i = 0; i < cowNo.Length; i++)
{
string[] arr = cowNo[i].Split('@');
int ID = int.Parse(arr[0]);
string[] posArr = arr[1].Split(',');
float x = float.Parse(posArr[0]);
float z = float.Parse(posArr[1]);
float depth = float.Parse(posArr[3]);
string typeStr = posArr[4].Trim();//Trim:去掉前后空格;
LineType lineType = GetLintType(typeStr);
// pos.Add(new Vector3(x, 0, z));
List<Vector3> pos = new List<Vector3>();
if (lineRdrDict.ContainsKey(ID - 1))
{
if (lineRdrDict[ID - 1].ContainsKey(lineType))
{
lineRdrDict[ID - 1][lineType].positionCount++;
lineRdrDict[ID - 1][lineType].SetPosition(lineRdrDict[ID - 1][lineType].positionCount - 1, new Vector3(x, 0, z));
}
else
{
GameObject typeObj = new GameObject();
typeObj.transform.parent = this.transform;
typeObj.name = "seaLineID:" + lineType;
lineRdrDict[ID - 1][lineType] = typeObj.AddComponent<LineRenderer>();
lineRdrDict[ID - 1][lineType].startColor = Color.red;
lineRdrDict[ID - 1][lineType].endColor = Color.red;
lineRdrDict[ID - 1][lineType].startWidth = 20;
lineRdrDict[ID - 1][lineType].endWidth = 20;
lineRdrDict[ID - 1][lineType].material = SetColor(depth);
lineRdrDict[ID - 1][lineType].positionCount = 1;
lineRdrDict[ID - 1][lineType].SetPosition(lineRdrDict[ID - 1][lineType].positionCount - 1, new Vector3(x, 0, z));
}
}
else
{
lineRdrDict[ID - 1] = new Dictionary<LineType, LineRenderer>();
if (lineRdrDict[ID - 1].ContainsKey(lineType))
{
lineRdrDict[ID - 1][lineType].positionCount++;
lineRdrDict[ID - 1][lineType].SetPosition(lineRdrDict[ID - 1][lineType].positionCount - 1, new Vector3(x, 0, z));
}
else
{
GameObject typeObj = new GameObject();
lineRdrDict[ID - 1][lineType] = typeObj.AddComponent<LineRenderer>();
typeObj.transform.parent = this.transform;
typeObj.name = "seaLineID:" + lineType;
lineRdrDict[ID - 1][lineType].startColor = Color.blue;
lineRdrDict[ID - 1][lineType].endColor = Color.blue;
lineRdrDict[ID - 1][lineType].startWidth = 20;
lineRdrDict[ID - 1][lineType].endWidth = 20;
lineRdrDict[ID - 1][lineType].material= SetColor(depth);
lineRdrDict[ID - 1][lineType].positionCount = 1;
lineRdrDict[ID - 1][lineType].SetPosition(lineRdrDict[ID - 1][lineType].positionCount - 1, new Vector3(x, 0, z));
}
}
}
}
private LineType GetLintType(string type)
{
LineType lineType;
switch (type)
{
case "p":
lineType = LineType.p;
break;
case "h0":
lineType = LineType.h0;
break;
case "h1":
lineType = LineType.h1;
break;
case "h2":
lineType = LineType.h2;
break;
case "h3":
lineType = LineType.h3;
break;
case "h4":
lineType = LineType.h4;
break;
case "h5":
lineType = LineType.h5;
break;
case "h6":
lineType = LineType.h6;
break;
case "h7":
lineType = LineType.h7;
break;
case "h8":
lineType = LineType.h8;
break;
case "h9":
lineType = LineType.h9;
break;
case "h10":
lineType = LineType.h10;
break;
case "h11":
lineType = LineType.h11;
break;
case "h12":
lineType = LineType.h12;
break;
default:
lineType = LineType.p;
break;
}
return lineType;
}
private Material SetColor(float depth)
{
Material material;
if (depth <= 2) material = lineRdrMaterials[0];
else if (depth <= 5) material = lineRdrMaterials[1];
else if (depth <= 10)material = lineRdrMaterials[2];
else if (depth <= 20)material = lineRdrMaterials[3];
else if (depth <= 30)material = lineRdrMaterials[4];
else material = lineRdrMaterials[5];
return material;
}
}