我试图读取csv文件并将读取的数据分配为Vector 3输入数据。根据我的理解,统一将从csv文件读取的数据分类为“对象”,而Vector 3函数的输入数据则为float。如何将这些数据转换为浮点值?代码示例在这里
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class readnmove : MonoBehaviour {
public float lat;
public float lon;
public float alt;
public float speed;
void Awake() {
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
List<Dictionary<string,object>> data = CSVReader.Read ("cuba");
for(var i=0; i < data.Count; i++) {
print ("time " + data[i]["time"] + " " +
"x " + data[i]["x"] + " " +
"y " + data[i]["y"] + " " +
"z " + data[i]["z"]);
lat = data[i]["x"];
lon = data[i]["y"];
alt = data[i]["z"];
transform.Translate(new Vector3 (lat, lon, alt)*speed*Time.deltaTime);
}
}
}
答案 0 :(得分:0)
尝试使用float.Parse(string)
或float.TryParse(string, out float)
所以,在你的情况下,你会得到:
lat = float.Parse(data[i]["x"].ToString());
lon = float.Parse(data[i]["y"].ToString());
alt = float.Parse(data[i]["z"].ToString());