From 261c29578d03e3f30b844fa14d9e308ae415b09d Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 21 Mar 2025 20:24:34 +0800 Subject: [PATCH] Update ComponentTemplateBase.cs --- .../Templates/ComponentTemplateBase.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Templates/EntityTemplate/Templates/ComponentTemplateBase.cs b/src/Templates/EntityTemplate/Templates/ComponentTemplateBase.cs index 0e8c249..252828e 100644 --- a/src/Templates/EntityTemplate/Templates/ComponentTemplateBase.cs +++ b/src/Templates/EntityTemplate/Templates/ComponentTemplateBase.cs @@ -63,6 +63,34 @@ namespace DCFApixels.DragonECS protected static readonly TypeMeta Meta = EcsDebugUtility.GetTypeMeta(); protected static readonly bool _isHasIEcsComponentLifecycle; protected static readonly IEcsComponentLifecycle _iEcsComponentLifecycle; + + private static bool _defaultValueTypeInit = false; + private static T _defaultValueType; + protected static T DefaultValueType + { + get + { + if (_defaultValueTypeInit == false) + { + Type type = typeof(T); + if (type.IsValueType) + { + FieldInfo field; + field = type.GetField("Default", BindingFlags.Static | BindingFlags.Public); + if (field != null && field.FieldType == type) + { + _defaultValueType = (T)field.GetValue(null); + } + field = type.GetField("Empty", BindingFlags.Static | BindingFlags.Public); + if (field != null && field.FieldType == type) + { + _defaultValueType = (T)field.GetValue(null); + } + } + } + return _defaultValueType; + } + } static ComponentTemplateBase() { _isHasIEcsComponentLifecycle = EcsComponentLifecycleHandler.isHasHandler; @@ -75,6 +103,7 @@ namespace DCFApixels.DragonECS { _iEcsComponentLifecycle.Enable(ref result); } + result = DefaultValueType; return result; }