C# 将字符串转换为矢量3

C# 将字符串转换为矢量3,c#,mysql,C#,Mysql,我在MySQL数据库中有以下字符串: “-1035.43,-2728.861,13.75664” 如何将其转换为矢量3,以便在此函数中使用: Vector3 newpos = new Vector3(getChar[15]); API.setEntityPosition(player, newpos); getChar是数据库列表,15是值 Vector3 ConvertFromString(string input) { if (input != null) {

我在MySQL数据库中有以下字符串:

“-1035.43,-2728.861,13.75664”

如何将其转换为矢量3,以便在此函数中使用:

Vector3 newpos = new Vector3(getChar[15]);
API.setEntityPosition(player, newpos);
getChar是数据库列表,15是值

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();
}
以下是您提供的字符串的结果:

以下是您提供的字符串的结果:

拆分
字符串
并提取坐标:

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]);

拆分
字符串
并提取坐标:

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]);

有些人不知道向量是什么,有些人不知道向量是什么is@Roberth,是否
getchar[15]
等于
“-1035.43,-2728.861,13.75664”
的字符串值?否,这些坐标会发生基本变化,但您是否会从
getchar[15]获得带有坐标的
string
格式类似于问题开头的
字符串
?如果在
API.setEntityPosition(player,newpos)设置断点
,newpos的X,Y,Z值是多少?@Roberth,
getchar[15]
是否等于
“-1035.43,-2728.861,13.75664”
?不,这些坐标基本上发生了变化,但您是否从
getchar[15]获得了一个
字符串
格式类似于问题开头的
字符串
?如果在
API.setEntityPosition(player,newpos)设置断点,newpos的X、Y、Z值是多少?我用您在原始帖子中提供的字符串作为示例的结果更新了我的答案。你能发布给你不正确结果的字符串吗?据我所见,是同一个字符串,但不管怎样,这些字符串随时都会发生变化,不知道该告诉你什么。创建一个空的控制台应用程序,将ConvertFromString()方法复制到那里并对其进行测试。如果它能工作,问题在于你的代码的其余部分。我用你在原始帖子中作为示例提供的字符串的结果更新了我的答案。你能发布给你不正确结果的字符串吗?据我所见,是同一个字符串,但不管怎样,这些字符串随时都会发生变化,不知道该告诉你什么。创建一个空的控制台应用程序,将ConvertFromString()方法复制到那里并对其进行测试。如果它起作用,那么问题在于代码的其余部分。