unity项目字符串转为Vector3和Quaternion
原:http://www.cnblogs.com/unityzc/p/6801515.html
运用环境:一般在读取csv表格的数据时是string类型转为Vector3或者Quaternion类型
字符串格式:x,x,x /x,x,x,x (英文逗号)
方法:
/// <summary>
/// 字符串转Vector3
/// </summary>
/// <param name="p_sVec3">需要转换的字符串</param>
/// <returns></returns>
public static Vector3 GetVec3ByString(string p_sVec3)
{
if (p_sVec3.Length <= 0)
return Vector3.zero;
string[] tmp_sValues = p_sVec3.Trim(' ').Split(',');
if (tmp_sValues != null && tmp_sValues.Length == 3)
{
float tmp_fX = float.Parse(tmp_sValues[0]);
float tmp_fY = float.Parse(tmp_sValues[1]);
float tmp_fZ = float.Parse(tmp_sValues[2]);
return new Vector3(tmp_fX, tmp_fY, tmp_fZ);
}
return Vector3.zero;
}
/// <summary>
/// 字符串转换Quaternion
/// </summary>
/// <param name="p_sVec3">需要转换的字符串</param>
/// <returns></returns>
public static Quaternion GetQuaByString(string p_sVec3)
{
if (p_sVec3.Length <= 0)
return Quaternion.identity;
string[] tmp_sValues = p_sVec3.Trim(' ').Split(',');
if (tmp_sValues != null && tmp_sValues.Length == 4)
{
float tmp_fX = float.Parse(tmp_sValues[0]);
float tmp_fY = float.Parse(tmp_sValues[1]);
float tmp_fZ = float.Parse(tmp_sValues[2]);
float tmp_fH = float.Parse(tmp_sValues[3]);
return new Quaternion(tmp_fX, tmp_fY, tmp_fZ, tmp_fH);
}
return Quaternion.identity;
}
作为备注方便以后使用


浙公网安备 33010602011771号