繁体   English   中英

Unity C# 构造函数未正确调用

[英]Unity C# Constructor is not called properly

在编辑模式下从 Unity 检查器创建新的 List 元素时,我在从它们的构造函数初始化字符串时遇到了一些麻烦。

它所做的只是将“调用测试”写入控制台,但不会将其名称和描述初始化为占位符文本。

public class StatsManager : MonoBehaviour {   
    [System.Serializable]
    public class StatValue {
        public string name;
        public string description;
        public int currentValue;

        public StatValue(){
            this.name = "Times hit";
            this.description = "This is how much you have been hit since the start of the game.";
            Debug.Log("Call test");
        }
    }

    [SerializeField]
    public List<StatsManager.StatValue> stats;
}

好的,我找到了一个解决方案,以防有人遇到同样的问题。 不需要 cTor 或 ISerializableCallbackReceiver。 这使用了也会在实例化时调用的重置回调(来自 coq)。 干杯!

 [System.Serializeable]
 public class Test {
     int value = 500;
 }
 public class TestManager : MonoBehaviour {
     public Test testInt;
     public List<Test> testInts;
     void Reset(){
         testInts = new List<Test>(){
             new Test()
         };
     }
 }

来自https://answers.unity.com/questions/1467289/default-value-on-list-vanished.html?childToView=1467368#comment-1467368

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM