| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622 |
- using System;
- using System.Collections.Generic;
- using Base;
- using UnityEngine;
- namespace Model
- {
- [Serializable]
- public class BehaviorTreeArgsDict: Dictionary<string, ValueBase>
- {
- public void SetKeyValueComp(Type type, string fieldName, object value)
- {
- if (IsStringType(type))
- {
- SetStrValue(fieldName, (string) value);
- }
- else if (IsObjectType(type))
- {
- SetObjectValue(fieldName, type, (UnityEngine.Object) value);
- }
- else if (IsIntType(type))
- {
- int intValue = 0;
- int.TryParse(value.ToString(), out intValue);
- SetIntValue(fieldName, intValue);
- }
- else if (IsLongType(type))
- {
- long longValue = 0;
- long.TryParse(value.ToString(), out longValue);
- SetLongValue(fieldName, longValue);
- }
- else if (IsFloatType(type))
- {
- float floatValue = 0;
- float.TryParse(value.ToString(), out floatValue);
- SetFloatValue(fieldName, floatValue);
- }
- else if (IsDoubleType(type))
- {
- double doubleValue = 0;
- double.TryParse(value.ToString(), out doubleValue);
- SetDoubleValue(fieldName, doubleValue);
- }
- else if (IsBoolType(type))
- {
- bool boolValue = false;
- bool.TryParse(value.ToString(), out boolValue);
- SetBoolValue(fieldName, boolValue);
- }
- else if (IsStringArrType(type))
- {
- SetStrArrValue(fieldName, (string[]) value);
- }
- else if (IsIntArrType(type))
- {
- SetIntArrValue(fieldName, (int[]) value);
- }
- else if (IsLongArrType(type))
- {
- SetLongArrValue(fieldName, (long[]) value);
- }
- else if (IsFloatArrType(type))
- {
- SetFloatArrValue(fieldName, (float[]) value);
- }
- else if (IsDoubleArrType(type))
- {
- SetDoubleArrValue(fieldName, (double[]) value);
- }
- else if (IsEnumType(type))
- {
- SetEnumValue(fieldName, value.ToString());
- }
- else if (IsObjectArrayType(type))
- {
- SetObjectArrayValue(fieldName, type, (UnityEngine.Object[]) value);
- }
- else
- {
- Log.Error($"行为树节点暂时未支持此类型:{type}!");
- }
- }
- private void SetConvertebleValue(string fieldName, float value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].Convertivle = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.Convertivle = value;
- this.Add(fieldName, valueBase);
- }
- }
- public object GetTreeDictValue(Type fieldType, string fieldName)
- {
- if (!this.ContainsKey(fieldName))
- {
- Log.Error($"fieldName:{fieldName} 不存在!!!!");
- return null;
- }
- ValueBase obj = this[fieldName];
- return obj.GetValueByType(fieldType);
- }
- /// <summary>
- /// judge
- /// </summary>
- /// <param nodeName="type"></param>
- /// <returns></returns>
- public static bool IsStringType(Type type)
- {
- return typeof (string) == type;
- }
- public static bool IsBoolType(Type type)
- {
- return typeof (bool) == type;
- }
- public static bool IsIntType(Type type)
- {
- return typeof (int) == type;
- }
- public static bool IsLongType(Type type)
- {
- return typeof (long) == type;
- }
- public static bool IsFloatType(Type type)
- {
- return typeof (float) == type;
- }
- public static bool IsDoubleType(Type type)
- {
- return typeof (double) == type;
- }
- public static bool IsIntArrType(Type type)
- {
- return typeof (int[]) == type;
- }
- public static bool IsLongArrType(Type type)
- {
- return typeof (long[]) == type;
- }
- public static bool IsFloatArrType(Type type)
- {
- return typeof (float[]) == type;
- }
- public static bool IsDoubleArrType(Type type)
- {
- return typeof (double[]) == type;
- }
- public static bool IsStringArrType(Type type)
- {
- return typeof (string[]) == type;
- }
- public static bool IsObjectType(Type fieldType)
- {
- Type objecType = typeof (UnityEngine.Object);
- if (fieldType == objecType || fieldType.IsSubclassOf(objecType))
- {
- return true;
- }
- return false;
- }
- public static bool IsObjectArrayType(Type fieldType)
- {
- if (fieldType == typeof (UnityEngine.Object[]) || fieldType == typeof (GameObject[]) || fieldType == typeof (Material[]) ||
- fieldType == typeof (Texture[]) || fieldType == typeof (Texture2D[]) || fieldType == typeof (Texture3D[]) || fieldType == typeof (Shader[]) ||
- fieldType == typeof (AudioClip[]) || fieldType == typeof (Sprite[]))
- {
- return true;
- }
- return false;
- }
- public static bool IsConvertble(Type type)
- {
- return type == typeof (IConvertible);
- }
- public static bool IsAudioClipType(Type fieldType)
- {
- return fieldType == typeof (AudioClip);
- }
- public static bool IsMaterialType(Type fieldType)
- {
- return fieldType == typeof (Material);
- }
- public static bool IsGameObjectType(Type fieldType)
- {
- return fieldType == typeof (GameObject);
- }
- public static bool IsShaderType(Type fieldType)
- {
- return fieldType == typeof (Shader);
- }
- public static bool IsTextureType(Type fieldType)
- {
- return fieldType == typeof (Texture);
- }
- public static bool IsTexture2DType(Type fieldType)
- {
- return fieldType == typeof (Texture2D);
- }
- public static bool IsTexture3DType(Type fieldType)
- {
- return fieldType == typeof (Texture3D);
- }
- public static bool IsGameObjectArrayType(Type fieldType)
- {
- return fieldType == typeof (GameObject[]);
- }
- public static bool IsMaterialArrayType(Type fieldType)
- {
- return fieldType == typeof (Material[]);
- }
- public static bool IsTextureArrayType(Type fieldType)
- {
- return fieldType == typeof (Texture[]);
- }
- public static bool IsTexture2DArrayType(Type fieldType)
- {
- return fieldType == typeof (Texture2D[]);
- }
- public static bool IsTexture3DArrayType(Type fieldType)
- {
- return fieldType == typeof (Texture3D[]);
- }
- public static bool IsShaderArrayType(Type fieldType)
- {
- return fieldType == typeof (Shader[]);
- }
- public static bool IsAudioClipArrayType(Type fieldType)
- {
- return fieldType == typeof (AudioClip[]);
- }
- public static bool IsUnitConfigArrayType(Type fieldType)
- {
- return false;
- }
- public static bool IsSpriteArrayType(Type fieldType)
- {
- return fieldType == typeof (Sprite[]);
- }
- public static bool IsEnumType(Type fieldType)
- {
- Type enumType = typeof (Enum);
- if (fieldType == enumType || fieldType.IsSubclassOf(enumType))
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// Set Value
- /// </summary>
- /// <param nodeName="fieldName"></param>
- /// <param nodeName="value"></param>
- public void SetStrValue(string fieldName, string value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].StringValue = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.StringValue = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetIntValue(string fieldName, int value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].Int32Value = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.Int32Value = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetLongValue(string fieldName, long value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].Int64Value = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.Int64Value = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetFloatValue(string fieldName, float value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].SingleValue = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.SingleValue = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetDoubleValue(string fieldName, double value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].DoubleValue = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.DoubleValue = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetBoolValue(string fieldName, bool value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].BooleanValue = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.BooleanValue = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetStrArrValue(string fieldName, string[] value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].StringArray = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.StringArray = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetIntArrValue(string fieldName, int[] value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].Int32Array = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.Int32Array = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetLongArrValue(string fieldName, long[] value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].Int64Array = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.Int64Array = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetFloatArrValue(string fieldName, float[] value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].SingleArray = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.SingleArray = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetDoubleArrValue(string fieldName, double[] value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].DoubleArray = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.DoubleArray = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetObjectValue(string fieldName, Type type, UnityEngine.Object value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName]?.SetValueByType(type, value);
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- if (value != null)
- {
- valueBase.SetValueByType(type, value);
- }
- this.Add(fieldName, valueBase);
- }
- }
- public void SetEnumValue(string fieldName, string value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName].enumValue = value;
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- valueBase.enumValue = value;
- this.Add(fieldName, valueBase);
- }
- }
- public void SetObjectArrayValue(string fieldName, Type type, UnityEngine.Object[] value)
- {
- if (this.ContainsKey(fieldName))
- {
- this[fieldName]?.SetValueByType(type, value);
- }
- else
- {
- ValueBase valueBase = new ValueBase();
- if (value != null)
- {
- valueBase.SetValueByType(type, value);
- }
- this.Add(fieldName, valueBase);
- }
- }
- public static GameObject[] ConvertToGameObjectArray(UnityEngine.Object[] objectArray)
- {
- if (objectArray == null)
- {
- return null;
- }
- GameObject[] newObjectArray = new GameObject[objectArray.Length];
- for (int i = 0; i < objectArray.Length; i++)
- {
- newObjectArray[i] = (GameObject) objectArray[i];
- }
- return newObjectArray;
- }
- public static Material[] ConvertToMaterialArray(UnityEngine.Object[] objectArray)
- {
- if (objectArray == null)
- {
- return null;
- }
- Material[] newObjectArray = new Material[objectArray.Length];
- for (int i = 0; i < objectArray.Length; i++)
- {
- newObjectArray[i] = (Material) objectArray[i];
- }
- return newObjectArray;
- }
- public static Texture[] ConvertToTextureArray(UnityEngine.Object[] objectArray)
- {
- if (objectArray == null)
- {
- return null;
- }
- Texture[] newObjectArray = new Texture[objectArray.Length];
- for (int i = 0; i < objectArray.Length; i++)
- {
- newObjectArray[i] = (Texture) objectArray[i];
- }
- return newObjectArray;
- }
- public static Texture2D[] ConvertToTexture2DArray(UnityEngine.Object[] objectArray)
- {
- if (objectArray == null)
- {
- return null;
- }
- Texture2D[] newObjectArray = new Texture2D[objectArray.Length];
- for (int i = 0; i < objectArray.Length; i++)
- {
- newObjectArray[i] = (Texture2D) objectArray[i];
- }
- return newObjectArray;
- }
- public static Texture3D[] ConvertToTexture3DArray(UnityEngine.Object[] objectArray)
- {
- if (objectArray == null)
- {
- return null;
- }
- Texture3D[] newObjectArray = new Texture3D[objectArray.Length];
- for (int i = 0; i < objectArray.Length; i++)
- {
- newObjectArray[i] = (Texture3D) objectArray[i];
- }
- return newObjectArray;
- }
- public static Shader[] ConvertToShaderArray(UnityEngine.Object[] objectArray)
- {
- if (objectArray == null)
- {
- return null;
- }
- Shader[] newObjectArray = new Shader[objectArray.Length];
- for (int i = 0; i < objectArray.Length; i++)
- {
- newObjectArray[i] = (Shader) objectArray[i];
- }
- return newObjectArray;
- }
- public static AudioClip[] ConvertToAudioClipArray(UnityEngine.Object[] objectArray)
- {
- if (objectArray == null)
- {
- return null;
- }
- AudioClip[] newObjectArray = new AudioClip[objectArray.Length];
- for (int i = 0; i < objectArray.Length; i++)
- {
- newObjectArray[i] = (AudioClip) objectArray[i];
- }
- return newObjectArray;
- }
- public static Sprite[] ConvertToSpriteArray(UnityEngine.Object[] objectArray)
- {
- if (objectArray == null)
- {
- return null;
- }
- Sprite[] newObjectArray = new Sprite[objectArray.Length];
- for (int i = 0; i < objectArray.Length; i++)
- {
- newObjectArray[i] = (Sprite) objectArray[i];
- }
- return newObjectArray;
- }
- public static bool SatisfyCondition(GameObject go, Type[] constraintTypes)
- {
- if (go == null || constraintTypes == null || constraintTypes.Length <= 0)
- {
- return true;
- }
- foreach (var constraint in constraintTypes)
- {
- if (go.GetComponent(constraint) == null)
- {
- Log.Error($"此GameObject必须包含:{constraint}");
- return false;
- }
- }
- return true;
- }
- }
- }
|