1
我有一些按鈕,每個代表一些級別,並希望以編程方式添加偵聽器,但不太熟悉C#的lambda函數(可能是一些封閉的東西?),這就是我現在做的:統一添加按鈕使用腳本編程方式與參數
for(int i=0; i<levels.Count; i++){
//omit the making a button snippet
button.GetComponent<Button>().onClick.AddListener(() =>
{
Debug.Log("load Scene");
ApplicationModel.currentLevel = levels[i];
SceneManager.LoadScene("Game");
//Application.LoadLevel("Game");
});
}
但行:
ApplicationModel.currentLevel = levels[i];
levels
是List<Level>
和ApplicationModel
是一類根據this post 持有信息,但它一直給予ArgumentOutOfRang eException:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[Level].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
GameLevelManger+<initScrollPanel>c__AnonStorey0.<>m__0() (at Assets/GameLevelManger.cs:72)
謝謝,它有幫助。如果你可以添加更多的 參考文獻,那麼它會更好:D – armnotstrong
@armnotstrong如果它幫助我很感激它,如果你將答案標記爲已接受,並確定我會追加一些參考鏈接。 –
感謝@Ousmane,我會深入研究它:D – armnotstrong