zoukankan      html  css  js  c++  java
  • 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;
        }

    作为备注方便以后使用

  • 相关阅读:
    Linux服务器安全审计工具与流程完全指南
    谈谈站桩
    Django Push 的一些资料
    Angularjs $http服务的两个request安全问题
    Ubuntu本地uwsgi配Django问题的解决
    Angularjs Post传值后台收不到的原因
    Flex实现双轴条状图
    时间序列学习笔记
    Nuget公布Dll
    【小游戏】有意思的小游戏集合
  • 原文地址:https://www.cnblogs.com/unityzc/p/6801515.html
Copyright © 2011-2022 走看看