using System; using System.Collections.Generic; using System.Reflection; using UnityEngine; using Object = UnityEngine.Object; namespace TriInspector.Utilities { internal static class TriUnitySerializationUtilities { public static bool IsSerializableByUnity(FieldInfo fieldInfo) { if (fieldInfo.GetCustomAttribute() != null || fieldInfo.GetCustomAttribute() != null) { return false; } if (fieldInfo.GetCustomAttribute() != null) { return true; } if (fieldInfo.IsPublic || fieldInfo.GetCustomAttribute() != null) { return IsTypeSerializable(fieldInfo.FieldType); } return false; } private static bool IsTypeSerializable(Type type) { if (type == typeof(object)) { return false; } if (type == typeof(string) || type == typeof(bool) || type == typeof(char) || type == typeof(int) || type == typeof(float) || type == typeof(Vector2) || type == typeof(Vector2Int) || type == typeof(Vector3) || type == typeof(Vector3Int) || type == typeof(Vector4) || type == typeof(Color) || type == typeof(LayerMask) || type == typeof(Rect) || type == typeof(RectInt) || type == typeof(AnimationCurve) || type == typeof(Bounds) || type == typeof(BoundsInt) || type == typeof(Gradient) || type == typeof(Quaternion)) { return true; } if (typeof(Object).IsAssignableFrom(type)) { return true; } if (type.IsEnum) { return true; } if (type.IsPrimitive) { return true; } if (type.IsArray) { var elementType = type.GetElementType(); return IsTypeSerializable(elementType); } if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>)) { var elementType = type.GetGenericArguments()[0]; return IsTypeSerializable(elementType); } if (type.GetCustomAttribute() != null) { return true; } // any other cases? return false; } } }