我在理解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
}
謝謝〜斯科特
「不工作」 - 是不是一個正確的描述。 –