Unity C#Constructor未正确调用

时间:2018-02-11 16:56:20

标签: c# list class unity3d initialization

在编辑模式下从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;
}

1 个答案:

答案 0 :(得分:0)

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

 [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