[英]Transform a string to Vector3
我在MySQL数据库中有以下字符串:
“ -1035.43,-2728.861、13.75664”
我如何将其转换为Vector3以在此功能中使用它:
Vector3 newpos = new Vector3(getChar[15]);
API.setEntityPosition(player, newpos);
getChar是数据库列表,而15是值。
分割string
并提取坐标:
float[] newPosCoordinates = getChar[15].Split(new string[] { ", " }, StringSplitOptions.None).Select(x => float.Parse(x)).ToArray();
Vector3 newpos = new Vector3(newPosCoordinates[0], newPosCoordinates[1], newPosCoordinates[2]);
Vector3 ConvertFromString(string input)
{
if (input != null)
{
var vals = input.Split(',').Select(s => s.Trim()).ToArray()
if (vals.Length == 3)
{
Single v1, v2, v3;
if (Single.TryParse(vals[0], out v1) &&
Single.TryParse(vals[1], out v2) &&
Single.TryParse(vals[2], out v3))
return new Vector3(v1, v2, v3);
else
throw new ArgumentException();
}
else
throw new ArgumentException();
}
else
throw new ArgumentException();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.