Unity 연구 노트-txt 데이터 읽기 및 그리기
Unity의 Scene 제작에서는 Scene을 생성하기 위해 많은 데이터를 사용해야하는 경우가 있습니다. 데이터 동작이 수천에서 수만 라인 인 경우 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 <key, value> Dict = new Dictionary <key, value> ();
Dict [key]를 읽으면 매핑 된 값을 직접 호출 할 수 있습니다.
## 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;
}
}