unity项目字符串转为Vector3和Quaternion

运用环境:一般在读取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;
    }

作为备注方便以后使用

时间: 2024-08-16 17:36:29

unity项目字符串转为Vector3和Quaternion的相关文章

[C++] zlatlcv: ATL字符串转换辅助库。能很方便的将UTF-8字符串转为TCHAR等字符串

作者:zyl910 如今,UTF-8字符串的使用频率越来越多了.但是在VC中,不能直接处理UTF-8字符串,得专门去写UTF-8与窄字符串.宽字符串.TCHAR字符串相互转换的代码.不仅费时费力,而且稍不留心就容易造成内存泄露问题.于是我便想专门编写个库来解决UTF-8字符串编码问题. 特性——支持 TCHAR,能随时切换项目字符集配置.兼容 32位(x86)与64位(x64)Windows环境.兼容 VC2005 及更高版本的 VC. 一.设计思路 ATL中的字符串转换宏用起来很方便,于是我打

jquery过滤特殊字符及js字符串转为数字

//替换特殊字符 $(this).val($(this).val().replace(/[~'!<>@#$%^&*()-+_=:]/g, "")); 方法主要有三种 转换函数.强制类型转换.利用js变量弱类型转换. 1. 转换函数: js提供了parseInt()和parseFloat()两个转换函数.前者把值转换成整数,后者把值转换成浮点数.只有对String类型调用这些方法,这两个函数才能正确运行:对其他类型返回的都是NaN(Not a Number). 一些示

JavaScript基础 空字符串转为数值类型 Number()

镇场诗: 清心感悟智慧语,不着世间名与利.学水处下纳百川,舍尽贡高我慢意. 学有小成返哺根,愿铸一良心博客.诚心于此写经验,愿见文者得启发.------------------------------------------ code: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=ut

对于弱类型语言,譬如PHP,其字符串转为浮点型

弱类型语言,php,javascript,将字符串转为浮点型都可以这样做: $str = "45.3"; $str = float($str); 也可以直接在加上0, $str = $str + 0; 此时$str已经是一个浮点型了,当然结果可能小数位数很多,可以 $str = round($str,2);保留2位小数.

将UTC日期字符串转为本地时间字符串,如@&quot;yyyy-MM-dd&#39;T&#39;HH:mm:ssZ&quot;转换为本地时间

由于苹果商店上线应用24小时内会不稳定,更新提醒可能会陷入死循环,更新提醒需要24小时后弹出,需要把苹果返回的上线时间转换为本地时间故写了下边的方法: //将UTC日期字符串转为本地时间字符串//输入的UTC日期格式2013-08-03T04:53:51+0000-(NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate{    NSDateFormatter *dateFormatter = [[NSDateFormatter all

php json字符串转为数组或对象

从网上查到的方法是 用get_object_vars 把类类型转换成数组 然后在用foreach  遍历即可 $array = get_object_vars($test); $json= '[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d

[LeetCode] String to Integer (atoi) 字符串转为整数

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

sql2008 将行转为字符串, 将字符串转为行 互转

--将行转为字符串 select stuff((select top 20 ','+ QQ from dl_QQ where uiid=1 order by tim desc for xml path('')),1,1,'') -- ============================================= -- Description: 将字符串转为表格 /* 例:select * from [f_split]('spu0987*5//spu0988*5/spu0989*5',

[笔记]一道C语言面试题:IPv4字符串转为UInt整数

题目:输入一个IPv4字符串,如“1.2.3.4”,输出对应的无符号整数,如本例输出为 0x01020304. 来源:某500强企业面试题目 思路:从尾部扫描到头部,一旦发现无法转换,立即返回,减少无谓操作. #include "stdio.h" #include "stdlib.h" #include "string.h" bool ConvertIPv4ToUInt(const char *strIP, unsigned int *ip) {