学习笔记:搜索对象技巧————非For,Foreach方法,根据名称获取列表里的目标对象

这里使用图片列表展示:根据图片名称,在列表里找到对应的图片;

1、使用List存储目标图片集合;

public List<Sprite> sprites;

2、根据目标名称找列表里的目标对象;

(1)使用List的FindIndex API结合匿名方法,找到列表里匹配上(相同名称)的图片;

并返回图片在列表里的索引值;

(2)根据返回的索引值,设置目标对象的Sprite;

int index = sprites.FindIndex(sprite => sprite.name == targetName);
icon.sprite = sprites[index];

猜你喜欢

转载自blog.csdn.net/weixin_43908355/article/details/125320514