Unityスタディノート-txtデータを読んで描画する
ユニティのシーン制作では、シーンを生成するために大量のデータを使用する必要がある場合があります。データの動作が数千から数万行の場合、jsonおよびtxtデータを直接呼び出す方が便利です。 txtドキュメントデータを実行します要約を使用します。
1.ドキュメントのコンテンツ形式
ドキュメントはどこかにある私たちの位置データです。1@は最初のグループを意味し、次にX座標とY座標、そして高さの範囲が続きます。最後の文字は形状の識別を表します。たとえば、最初の行は最初のデータセットを表し、座標は(47056、-15464)高さ20〜30のPタイプの形状の場所です。
ユニティシーンで地図のようなグリッドを描く
## 2.コンテンツの読み取りと処理
新しいTextAssetを作成し、自分で名前を付けます。呼び出しの内容は、使いやすいようにリソースに配置するのが最適です
。TextAssettext = 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;
}
}