Unity 3D导入txt文本文件坐标并打印

先看打印结果:

首先我们创建一个txt文件,将坐标输入或复制进去,从左到右依次为x  y  z,中间用逗号(英文逗号)隔开

在Unity工程文件下的Assets文件夹下创建Resources文件夹,并将该txt文件复制在此文件夹下

 创建C#脚本,名称任意

导入该文件:

TextAsset matlabData = Resources.Load("MTBtest") as TextAsset;
//MTBtest为我的txt文件名称,matlabData是你随意起的一个名称

按行切分数据:

string[] test = matlabData.text.Split('\n');

确定行数:

rowNum = test.Length;

将MTBtest.txt中的坐标分别存入pointX[i]、pointY[i]、 pointZ[i]:

for (int i = 0, j = 0; i < test.Length; i++)
        {
            splitData = test[i].Split(","); //在splitData中存入test数据,存入的是坐标            

            pointX[i] = splitData[j];
            pointY[i] = splitData[j + 1];
            pointZ[i] = splitData[j + 2];           
        }

放出完整代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MTBtoCS : MonoBehaviour
{
    string[] pointX = new string[100];
    string[] pointY = new string[100];
    string[] pointZ = new string[100];
    string[] splitData = new string[1000];
    int rowNum, currentRowLabel = 1;
    public int pointLabel = 0;

    void Start()
    {
        TextAsset matlabData = Resources.Load("MTBtest") as TextAsset;
        string[] test = matlabData.text.Split('\n'); //test.Length = 28
        rowNum = test.Length;   // = 28  组数据
        
        //将MTBtest.txt中的坐标分别存入pointX[i]、pointY[i]、 pointZ[i]
        for (int i = 0, j = 0; i < test.Length; i++)
        {
            splitData = test[i].Split(","); //在splitData中存入test数据,存入的是坐标            

            pointX[i] = splitData[j];
            pointY[i] = splitData[j + 1];
            pointZ[i] = splitData[j + 2];           
        }
        DrawLine();

    }
  
    void Update()
    {
        
    }
    
    /// <summary>
    /// 画线
    /// </summary>
    void DrawLine()
    {
        for (int i = 0; i < rowNum; i += 4)
        {
            Debug.DrawLine(new Vector3(float.Parse(pointX[i]), float.Parse(pointY[i]), float.Parse(pointZ[i])),
                new Vector3(float.Parse(pointX[i+1]), float.Parse(pointY[i+1]), float.Parse(pointZ[i+1])),
                Color.black,100f);
            Debug.DrawLine(new Vector3(float.Parse(pointX[i+1]), float.Parse(pointY[i+1]), float.Parse(pointZ[i+1])),
                new Vector3(float.Parse(pointX[i + 2]), float.Parse(pointY[i + 2]), float.Parse(pointZ[i + 2])),
                Color.black, 100f);
            Debug.DrawLine(new Vector3(float.Parse(pointX[i + 2]), float.Parse(pointY[i + 2]), float.Parse(pointZ[i + 2])),
                new Vector3(float.Parse(pointX[i + 3]), float.Parse(pointY[i + 3]), float.Parse(pointZ[i + 3])),
                Color.black, 100f);
            Debug.DrawLine(new Vector3(float.Parse(pointX[i + 3]), float.Parse(pointY[i + 3]), float.Parse(pointZ[i + 3])),
                new Vector3(float.Parse(pointX[i]), float.Parse(pointY[i]), float.Parse(pointZ[i])),
                Color.black, 100f);           
        }
    }

}

如果想要查看坐标,可以在start函数中的for中加入:

Debug.Log(pointX[i] + "," + pointY[i] + "," + pointZ[i]);

最后,将脚本挂在到物体上(空物体即可),点击运行,即可实现

注意,分割数据用的是

.text.Split('\n');

重点就是导入和分割数据

猜你喜欢

转载自blog.csdn.net/weixin_50736953/article/details/126821946