Unity study notes-read txt data and draw

Unity study notes-read txt data and draw

In the scene production of unity, we sometimes need to use a lot of data to generate our scene. When the data behavior is thousands to tens of thousands of rows, it is more convenient to directly call json and txt data. Today, we will do the txt document data Use summary.

1. Document content format

The document is our location data somewhere, 1@ means the first group, followed by the X and Y coordinates, and then the height range. The last letter represents the shape identification. For example, the first line represents the first set of data, and the coordinates are (47056, -15464) a place with a shape of P type between 20-30 height.
Insert picture description here
Draw a grid like a map in the unity scene
Insert picture description here

## 2. Content reading and processing
Create a new TextAsset and name it yourself. The content of the call is best placed in Resources for easy use:
TextAsset text = Resources.Load("ENC_DEPARE_POLYGON");

Use Split to split the document and store it according to your needs,
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. Use LineRenderer to draw

lineRenderer.startColor = Color.blue;//Set the start color
lineRenderer.endColor = Color.blue;//Set the end color
lineRenderer.startWidth = 20;//Set the start width
lineRenderer.endWidth = 20;//End width
lineRenderer .material = SetColor(depth);//Select the color according to the height (depth) value, this sentence is a function defined by yourself
lineRenderer.positionCount = n;//Set the total number of points
lineRenderer.SetPosition();//Set the position

## 4. Supplement the use of Dictionary
When we have a lot of data to call frequently, the use of Dictionary can greatly facilitate our work: it
can be understood as a key to value mapping.
Dictionary<key, value> Dict = new Dictionary<key, value>();
When Dict[key] is read, the mapped value value can be called directly;
## 5. Complete code

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;
    }
}

Guess you like

Origin blog.csdn.net/qq_42434073/article/details/108440685