ValueBase.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Reflection;
  3. using UnityEngine;
  4. namespace Model
  5. {
  6. [Serializable]
  7. public class ValueBase
  8. {
  9. public IConvertible Convertivle;
  10. public string enumValue;
  11. public string StringValue;
  12. public bool BooleanValue;
  13. public int Int32Value;
  14. public long Int64Value;
  15. public float SingleValue;
  16. public double DoubleValue;
  17. public int[] Int32Array;
  18. public long[] Int64Array;
  19. public string[] StringArray;
  20. public float[] SingleArray;
  21. public double[] DoubleArray;
  22. public GameObject GameObjectValue;
  23. public AudioClip AudioClipValue;
  24. public Material MaterialValue;
  25. public Shader ShaderValue;
  26. public Texture TextureValue;
  27. public Texture2D Texture2DValue;
  28. public Texture3D Texture3DValue;
  29. public Sprite SpriteValue;
  30. public GameObject[] GameObjectArray;
  31. public AudioClip[] AudioClipArray;
  32. public Material[] MaterialArray;
  33. public Shader[] ShaderArray;
  34. public Texture[] TextureArray;
  35. public Texture2D[] Texture2DArray;
  36. public Texture3D[] Texture3DArray;
  37. public Sprite[] SpriteArray;
  38. public static ValueBase Clone(ValueBase source)
  39. {
  40. ValueBase v = new ValueBase();
  41. FieldInfo[] infos = source.GetType().GetFields();
  42. foreach (FieldInfo info in infos)
  43. {
  44. object value;
  45. if (info.FieldType.IsSubclassOf(typeof (Array)))
  46. {
  47. Array sourceArray = (Array) info.GetValue(source);
  48. if (sourceArray == null)
  49. {
  50. continue;
  51. }
  52. Array dest = Array.CreateInstance(info.FieldType.GetElementType(), sourceArray.Length);
  53. Array.Copy(sourceArray, dest, dest.Length);
  54. value = dest;
  55. }
  56. else
  57. {
  58. value = info.GetValue(source);
  59. }
  60. info.SetValue(v, value);
  61. }
  62. return v;
  63. }
  64. public object GetValueByType(Type type)
  65. {
  66. try
  67. {
  68. FieldInfo fieldInfo = GetFieldInfo(type);
  69. if (BehaviorTreeArgsDict.IsEnumType(type))
  70. {
  71. Enum value;
  72. if (string.IsNullOrEmpty(enumValue))
  73. {
  74. value = GetDefaultEnumValue(type);
  75. }
  76. else
  77. {
  78. value = (Enum) Enum.Parse(type, enumValue);
  79. }
  80. return value;
  81. }
  82. if (BehaviorTreeArgsDict.IsStringType(type))
  83. {
  84. if (string.IsNullOrEmpty(this.StringValue))
  85. {
  86. this.StringValue = this.enumValue;
  87. return this.StringValue;
  88. }
  89. }
  90. return fieldInfo.GetValue(this);
  91. }
  92. catch (Exception err)
  93. {
  94. throw new GameException($"行为树报错:{err}");
  95. }
  96. }
  97. private Enum GetDefaultEnumValue(Type type)
  98. {
  99. Array array = Enum.GetValues(type);
  100. Enum value = (Enum) array.GetValue(0);
  101. return value;
  102. }
  103. public void SetValueByType(Type type, object value)
  104. {
  105. if (type == null)
  106. {
  107. return;
  108. }
  109. FieldInfo field = GetFieldInfo(type);
  110. if (BehaviorTreeArgsDict.IsMaterialArrayType(field.FieldType))
  111. {
  112. field.SetValue(this, BehaviorTreeArgsDict.ConvertToMaterialArray((UnityEngine.Object[]) value));
  113. }
  114. else if (BehaviorTreeArgsDict.IsGameObjectArrayType(field.FieldType))
  115. {
  116. field.SetValue(this, BehaviorTreeArgsDict.ConvertToGameObjectArray((UnityEngine.Object[]) value));
  117. }
  118. else if (BehaviorTreeArgsDict.IsTextureArrayType(field.FieldType))
  119. {
  120. field.SetValue(this, BehaviorTreeArgsDict.ConvertToTextureArray((UnityEngine.Object[]) value));
  121. }
  122. else if (BehaviorTreeArgsDict.IsTexture2DArrayType(field.FieldType))
  123. {
  124. field.SetValue(this, BehaviorTreeArgsDict.ConvertToTexture2DArray((UnityEngine.Object[]) value));
  125. }
  126. else if (BehaviorTreeArgsDict.IsTexture3DArrayType(field.FieldType))
  127. {
  128. field.SetValue(this, BehaviorTreeArgsDict.ConvertToTexture3DArray((UnityEngine.Object[]) value));
  129. }
  130. else if (BehaviorTreeArgsDict.IsShaderArrayType(field.FieldType))
  131. {
  132. field.SetValue(this, BehaviorTreeArgsDict.ConvertToShaderArray((UnityEngine.Object[]) value));
  133. }
  134. else if (BehaviorTreeArgsDict.IsAudioClipArrayType(field.FieldType))
  135. {
  136. field.SetValue(this, BehaviorTreeArgsDict.ConvertToAudioClipArray((UnityEngine.Object[]) value));
  137. }
  138. else if (BehaviorTreeArgsDict.IsSpriteArrayType(field.FieldType))
  139. {
  140. field.SetValue(this, BehaviorTreeArgsDict.ConvertToSpriteArray((UnityEngine.Object[]) value));
  141. }
  142. else
  143. {
  144. field.SetValue(this, value);
  145. }
  146. }
  147. private FieldInfo GetFieldInfo(Type type)
  148. {
  149. string fieldName;
  150. if (BehaviorTreeArgsDict.IsEnumType(type))
  151. {
  152. fieldName = "enumValue";
  153. }
  154. else if (type.IsArray)
  155. {
  156. fieldName = type.GetElementType() + "Array";
  157. }
  158. else
  159. {
  160. fieldName = type.Name + "Value";
  161. }
  162. fieldName = RemovePrefix(fieldName);
  163. FieldInfo fieldInfo = GetType().GetField(fieldName);
  164. return fieldInfo;
  165. }
  166. private string RemovePrefix(string fieldName)
  167. {
  168. string enginePrefix = "UnityEngine.";
  169. int engineIndex = fieldName.IndexOf(enginePrefix);
  170. if (engineIndex != -1)
  171. {
  172. fieldName = fieldName.Remove(engineIndex, enginePrefix.Length);
  173. }
  174. string systemPrefix = "System.";
  175. int systemIndex = fieldName.IndexOf(systemPrefix);
  176. if (systemIndex != -1)
  177. {
  178. fieldName = fieldName.Remove(systemIndex, systemPrefix.Length);
  179. }
  180. return fieldName;
  181. }
  182. }
  183. }