我在理解C#中的属性如何工作时遇到了一些麻烦。首先介绍一下背景知识:我在统一级别管理器上工作,其目的是包含每个级别的游戏级别和信息列表(级别描述,级别图像等),所有这些都保存在一个ScriptableObject中。C#遇到属性变量问题(获取并设置)
因此,让我说我有3个属性,sceneIndex,sceneName和LevelID。
sceneIndex是一个变量,我应该能够在unity的检查器中进行更改,并且应该在构建设置中匹配级别索引。
sceneName不应该在团结督察中编辑,并且应该始终是关卡的名称。 (这是由sceneIndex确定的)
LevelID应始终设置为级别列表中此类的索引。
这是我当前的代码 - 当前当我更改sceneIndex时,我的sceneName未更新为检查器中的匹配名称,并且当我向列表中添加新元素时,LevelID未更新。
public class LevelDataBase : ScriptableObject {
[System.Serializable]
public class LevelData {
[SerializeField]
public string p_sceneName{//the levelName
get{ return sceneName; }
set{ sceneName = SceneManager.GetSceneAt (sceneIndex).name; }// it should always be set depending on the Scene Index
}
public string sceneName;//Properties dont display in unity's inspector so i have to use secondary variable for it to be displayed.
public int sceneIndex;//the scene index i should be able to change and then the scene Name should Updatedepending on the value
[SerializeField]
public int p_LevelID {//the Level id Should always be the index of this class in the list.
get{ return LevelID; }
set{ LevelID = LevelList.IndexOf (this); }
}
public int LevelID;
public string DisplayName;
public Sprite Image;
public string Description;
}
public List<LevelData> LevelList;//this is a list containing all the levels in the game
}
谢谢〜斯科特
“不工作” - 是不是一个正确的描述。 –