DataTableExtension.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using GameFramework.DataTable;
  8. using System;
  9. using UnityEngine;
  10. using UnityGameFramework.Runtime;
  11. namespace GFMain
  12. {
  13. public static class DataTableExtension
  14. {
  15. private const string DataRowClassPrefixName = "ProjectWuJiang.DR";
  16. public static readonly char[] DataSplitSeparators = new char[] { '\t' };
  17. public static readonly char[] DataTrimSeparators = new char[] { '\"' };
  18. public static void LoadDataTable(this DataTableComponent dataTableComponent, string dataTableName, string dataTableAssetName, object userData)
  19. {
  20. if (string.IsNullOrEmpty(dataTableName))
  21. {
  22. Log.Warning("Data table name is invalid.");
  23. return;
  24. }
  25. string[] splitedNames = dataTableName.Split('_');
  26. if (splitedNames.Length > 2)
  27. {
  28. Log.Warning("Data table name is invalid.");
  29. return;
  30. }
  31. string dataRowClassName = DataRowClassPrefixName + splitedNames[0];
  32. Type dataRowType = Type.GetType(dataRowClassName);
  33. if (dataRowType == null)
  34. {
  35. Log.Warning("Can not get data row type with class name '{0}'.", dataRowClassName);
  36. return;
  37. }
  38. string name = splitedNames.Length > 1 ? splitedNames[1] : null;
  39. DataTableBase dataTable = dataTableComponent.CreateDataTable(dataRowType, name);
  40. dataTable.ReadData(dataTableAssetName, Constant.AssetPriority.DataTableAsset, userData);
  41. }
  42. public static Color32 ParseColor32(string value)
  43. {
  44. string[] splitedValue = value.Split(',');
  45. return new Color32(byte.Parse(splitedValue[0]), byte.Parse(splitedValue[1]), byte.Parse(splitedValue[2]), byte.Parse(splitedValue[3]));
  46. }
  47. public static Color ParseColor(string value)
  48. {
  49. string[] splitedValue = value.Split(',');
  50. return new Color(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]), float.Parse(splitedValue[3]));
  51. }
  52. public static Quaternion ParseQuaternion(string value)
  53. {
  54. string[] splitedValue = value.Split(',');
  55. return new Quaternion(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]), float.Parse(splitedValue[3]));
  56. }
  57. public static Rect ParseRect(string value)
  58. {
  59. string[] splitedValue = value.Split(',');
  60. return new Rect(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]), float.Parse(splitedValue[3]));
  61. }
  62. public static Vector2 ParseVector2(string value)
  63. {
  64. string[] splitedValue = value.Split(',');
  65. return new Vector2(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]));
  66. }
  67. public static Vector3 ParseVector3(string value)
  68. {
  69. string[] splitedValue = value.Split(',');
  70. return new Vector3(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]));
  71. }
  72. public static Vector4 ParseVector4(string value)
  73. {
  74. string[] splitedValue = value.Split(',');
  75. return new Vector4(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]), float.Parse(splitedValue[3]));
  76. }
  77. public static string[] ParseStringArray(string value)
  78. {
  79. return value.Split('|');
  80. }
  81. }
  82. }