using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LogPrint;
public class CommonHelper
{
private static readonly string[] null_Arrar = new string[0];
//以分号分割把字符串转化为vector2
public static Vector2 StrToVector2(string str)
{
if (string.IsNullOrEmpty(str)) return Vector2.zero;
Vector2 value = Vector2.zero;
string[] strArray = str.Split(';');
if (strArray.Length < 2)
{
LogSystem.LogError(str + " string to Vector2 failed");
return value;
}
float x = Convert.ToSingle(strArray[0]);
float y = Convert.ToSingle(strArray[1]);
value = new Vector2(x, y);
return value;
}
//以分号分割把字符串转化为vector3
public static Vector3 StrToVector3(string str)
{
if (string.IsNullOrEmpty(str))
return Vector3.zero;
Vector3 value = Vector3.zero;
string[] strArray = str.Split(';');
if (strArray.Length < 3)
{
LogSystem.LogError(str + " string to Vector3 failed");
return value;
}
if (strArray[0] != "" && strArray[1] != "" && strArray[2] != "")
{
float x = Convert.ToSingle(strArray[0]);
float y = Convert.ToSingle(strArray[1]);
float z = Convert.ToSingle(strArray[2]);
value = new Vector3(x, y, z);
}
return value;
}
/// <summary>
/// 字符串转换数组;
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string[] Str2StringArray(string str, char splitMark = ';')
{
if (string.IsNullOrEmpty(str))
{
return null_Arrar;
}
string[] strArray = str.Split(splitMark);
return strArray;
}
/// <summary>
/// 字符串转成float;
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static float Str2Float(string str)
{
float refloat = 0;
try
{
refloat = string.IsNullOrEmpty(str) == true ? 0 : Convert.ToSingle(str);
}
catch (Exception e)
{
LogSystem.Log(str + "::" + e.Message);
}
return refloat;
}
public static int Str2Int(string str)
{
return string.IsNullOrEmpty(str) == true ? 0 : Convert.ToInt32(str);
}
//将数字转换成2位数 3 -> 03
public static string DoubleDigit(int score)
{
if (score > 9)
{
return score.ToString();
}
else if (score < 0)
{
//只能显示正整数,负数不要显示
return "9527";
}
else
{
return "0" + score;
}
}
}