using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace Thousandto.Plugins.Common
{
///
/// 配置数据中各种字符串解析处理
///
public class DeclareDataUtils
{
///
/// 解析字符串 -- 不带有前缀的字符串
///
/// 从解析的数据中获取数据的索引
/// xxx;xxx
///
public static string GetValue(int idx, string originalStr)
{
if (string.IsNullOrEmpty(originalStr)) return string.Empty;
var arr = originalStr.Split(new char[] { ';', ';' });
if (idx >= 0 && idx < arr.Length)
{
return arr[idx];
}
return string.Empty;
}
///
/// 获取带有前缀索引的数值
///
///
///
///
///
public static float GetValue(int idx, string originalStr, float defaultValue)
{
float result = defaultValue;
if (float.TryParse(GetValue(idx, originalStr), out result))
{
return result;
}
return defaultValue;
}
///
/// 获取带有前缀索引的数值
///
///
///
///
///
public static int GetValue(int idx, string originalStr, int defaultValue)
{
int result = defaultValue;
if (int.TryParse(GetValue(idx, originalStr), out result))
{
return result;
}
return defaultValue;
}
///
/// 获取带有前缀索引的颜色
///
///
///
///
///
public static Color GetValue(int idx, string originalStr, Color defaultValue)
{
return DecodeColorBase(GetValue(idx, originalStr), defaultValue);
}
///
/// 解析字符串 -- 带有前缀的的字符
///
/// 从解析的数据中获取数据的索引
/// 0_xxx;1_xxx
///
public static string GetValueWithPrefix(int idx, string originalStr, string str = "{0}_")
{
if (string.IsNullOrEmpty(originalStr)) return string.Empty;
string idxStr = string.Format(str, idx);
var arr = originalStr.Split(new char[] { ';', ';' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < arr.Length; i++)
{
if (arr[i].StartsWith(idxStr))
{
return arr[i].Substring(2, arr[i].Length - 2);
}
}
return string.Empty;
}
///
/// 获取带有前缀索引的数值
///
///
///
///
///
public static float GetValueWithPrefix(int idx, string originalStr, float defaultValue, string str = "{0}_")
{
float result = defaultValue;
if (float.TryParse(GetValueWithPrefix(idx, originalStr, str), out result))
{
return result;
}
return defaultValue;
}
///
/// 获取带有前缀索引的数值
///
///
///
///
///
public static int GetValueWithPrefix(int idx, string originalStr,int defaultValue, string str = "{0}_")
{
int result = defaultValue;
if (int.TryParse(GetValueWithPrefix(idx, originalStr, str), out result))
{
return result;
}
return defaultValue;
}
///
/// 获取带有前缀索引的颜色
///
///
///
///
///
public static Color GetValueWithPrefix(int idx, string originalStr, Color defaultValue)
{
return DecodeColorBase(GetValueWithPrefix(idx, originalStr), defaultValue);
}
///
/// 解析格式化颜色
///
/// 255;255;255;255或者255,255,255,255
///
public static Color DecodeColorBase(string colorString, Color defaultValue)
{
if (string.IsNullOrEmpty(colorString)) return defaultValue;
var arr = colorString.Split(new char[] { ',', ';', ';' }, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length > 2)
{
int tmp = 0;
int.TryParse(arr[0], out tmp);
float r = (float)tmp / 255f;
int.TryParse(arr[1], out tmp);
float g = (float)tmp / 255f;
int.TryParse(arr[2], out tmp);
float b = (float)tmp / 255f;
float a = 1;
if (arr.Length > 3)
{
int.TryParse(arr[3], out tmp);
a = (float)tmp / 255f;
}
return new Color(r, g, b, a);
}
return defaultValue;
}
}
}