检查动作列表中的参数

时间:2016-05-24 19:07:49

标签: c# list action

我想知道是否可以引用List中的参数,例如

if ( listOfActions[index].SecondParameter == x ){
    do stuff
}

老实说我对Action Documentation的了解不多,但这是我到目前为止所得到的:

List<Action> musicSequence = new List<Action>();
...
void addBulletToList (string direction, float time){
    musicSequence.Add ( ()=>musicControl.spawnBullet(direction, time) );
}

void spawnBullet(string direction, float time){
    go = (GameObject)Instantiate(Resources.Load(direction));
}
//And in my main function it would be something like this
if ( CurrentMusicTime equals the time parameter in musicSequence[index] ){
    execute musicSequence[index]
    next index
}

1 个答案:

答案 0 :(得分:0)

如果float time每个操作都是唯一的,您可以考虑使用Dictionary<float, Action>来存储相应的操作,因为您当前正在尝试的操作是不可能的。

Dictionary<float, Action> musicSequence = new Dictionary<float, Action>();

void addBulletToList (string direction, float time){
    musicSequence.Add(time, () => musicControl.spawnBullet(direction, time));
}

void spawnBullet(string direction, float time){
    go = (GameObject)Instantiate(Resources.Load(direction));
}

if (musicSequence.Keys.Contains(CurrentMusicTime)){
    musicSequence[CurrentMusicTime]();
    next index
}