Parcourir la source

修改rdReset注解

zhengchen il y a 1 an
Parent
commit
43c8151bfe

+ 19 - 19
Assets/Scripts/Game/GameCore.cs

@@ -10,43 +10,43 @@ namespace Game
 {
     public class GameCore : RdSingletonStateMachine<GameState, GameCore>
     {
-        public GameState pendingState;
+        [RdReset] public GameState pendingState;
 
-        public GameState battleState;
+        [RdReset] public GameState battleState;
 
-        public GameState liftState;
+        [RdReset] public GameState liftState;
 
-        public GameState endState;
+        [RdReset] public GameState endState;
 
-        [RdReset(null)] public GamePlayer localPlayer;
+        [RdReset] public GamePlayer localPlayer;
 
-        [RdReset(null)] public GamePlayer antiPlayer;
+        [RdReset] public GamePlayer antiPlayer;
 
-        [RdReset("0")] public int winPlayerId;
+        [RdReset] public int winPlayerId;
 
-        public int diamond;
+        [RdReset] public int diamond;
 
-        public Dictionary<int, CockActionComp> cockDict;
+        [RdReset] public Dictionary<int, CockActionComp> cockDict;
 
-        public Dictionary<int, MasterActionComp> masterDict;
+        [RdReset] public Dictionary<int, MasterActionComp> masterDict;
 
-        [CanBeNull] public GameObject parent;
+        [RdReset] public GameObject parent;
 
-        [CanBeNull] public BattleMainComp battleMainComp;
+        [RdReset] public BattleMainComp battleMainComp;
 
-        [CanBeNull] public GameObject camera;
+        [RdReset] public GameObject camera;
 
-        [CanBeNull] public GameObject resultPanel;
+        [RdReset] public GameObject resultPanel;
 
-        [CanBeNull] public LinkedList<BattleDetailObj> battleDetailObjs;
+        [RdReset] public LinkedList<BattleDetailObj> battleDetailObjs;
 
-        public (bool, bool) masterBack; // 主人复位
+        [RdReset] public (bool, bool) masterBack; // 主人复位
 
-        public string curBattleSession;
+        [RdReset] public string curBattleSession;
 
-        public int liftTimes;
+        [RdReset(2)] public int liftTimes;
 
-        public bool inBattleState = false;
+        [RdReset] public bool inBattleState = false;
 
         public GameCore()
         {

+ 62 - 8
Assets/Scripts/Ragdoll/RdResetAttribute.cs

@@ -1,15 +1,17 @@
 using System;
-using System.Reflection;
+using System.Collections.Generic;
 using UnityEngine;
 
 namespace Ragdoll
 {
+    public delegate void ResetDelegate(object value);
+
     [AttributeUsage(AttributeTargets.Field)]
     public class RdResetAttribute : Attribute
     {
-        public string DefaultValue { get; set; }
+        public object DefaultValue { get; set; }
 
-        public RdResetAttribute(string defaultValue)
+        public RdResetAttribute(object defaultValue = null)
         {
             DefaultValue = defaultValue;
         }
@@ -20,13 +22,65 @@ namespace Ragdoll
         public static void ResetAttribute(object obj)
         {
             var type = obj.GetType();
-            foreach (var member in type.GetMembers())
+
+            foreach (var field in type.GetFields())
             {
-                if (Attribute.GetCustomAttribute(member, typeof(RdResetAttribute)) is not
-                        RdResetAttribute
-                        attribute || member is not FieldInfo field) continue;
+                var resetAttribute = field.GetCustomAttributes(typeof(RdResetAttribute), true)
+                    as RdResetAttribute[];
+
+                if (resetAttribute.Length == 0) continue;
+
+                var defaultValue = resetAttribute[0].DefaultValue;
                 Debug.Log("重置 " + field.Name);
-                field.SetValue(obj, Convert.ChangeType(attribute.DefaultValue, field.FieldType));
+                if (defaultValue == null)
+                {
+                    Debug.Log("重置为默认值");
+                    if (field.FieldType == typeof(string)) // 字符串类型重置为null
+                    {
+                        field.SetValue(obj, null);
+                    }
+                    else if (field.FieldType == typeof(int) ||
+                             field.FieldType == typeof(float) ||
+                             field.FieldType == typeof(double) ||
+                             field.FieldType == typeof(long) ||
+                             field.FieldType == typeof(short) ||
+                             field.FieldType == typeof(byte) ||
+                             field.FieldType == typeof(uint) ||
+                             field.FieldType == typeof(ulong) ||
+                             field.FieldType == typeof(ushort) ||
+                             field.FieldType == typeof(sbyte))
+                    {
+                        field.SetValue(obj, 0); // 数值型重置为0
+                    }
+                    else if (field.FieldType == typeof(bool)) // 布尔类型重置为false
+                    {
+                        field.SetValue(obj, false);
+                    }
+                    else if (field.FieldType.IsArray) // 数组重置为空数组
+                    {
+                        field.SetValue(obj, Array.CreateInstance(field.FieldType.GetElementType(), 0));
+                    }
+                    else if (field.FieldType.IsGenericType &&
+                             field.FieldType.GetGenericTypeDefinition() == typeof(List<>)) // 列表重置为空列表
+                    {
+                        field.SetValue(obj, Activator.CreateInstance(field.FieldType));
+                    }
+                    else if (field.FieldType.IsGenericType &&
+                             field.FieldType.GetGenericTypeDefinition() == typeof(Dictionary<,>)) // 字典重置为空字典
+                    {
+                        field.SetValue(obj, Activator.CreateInstance(field.FieldType));
+                    }
+                    else // 其他类型重置为null
+                    {
+                        Debug.Log("未兼容的类型,重置为null");
+                        field.SetValue(obj, null);
+                    }
+                }
+                else
+                {
+                    Debug.Log("重置为自定义默认值");
+                    field.SetValue(obj, defaultValue);
+                }
             }
         }
     }

+ 2 - 1
Assets/Scripts/Ragdoll/RdStateMachine.cs

@@ -36,9 +36,10 @@ namespace Ragdoll
             return currentState;
         }
 
+        // ReSharper disable Unity.PerformanceAnalysis
         public virtual void TransitionToState(T nextState)
         {   
-            Debug.Log("change state ->" + nextState.ToString());
+            Debug.Log("change state -> " + nextState);
             // 退出当前状态
             if (currentState != null)
             {