删除并重新添加相同的精灵以列出 Unity
Removing and re-adding same sprite to list Unity
我在 c# 中有一个 Unity 脚本,其中声明了一个 public 精灵列表,然后在检查器中手动填充。
该列表包含 19 个汽车精灵。
只有当玩家完成每个级别后才能获得这些汽车。
例如:当您第一次打开游戏时,您只有一辆可用的汽车,但是如果您完成了第 1 级,您将解锁另一辆汽车,当您完成第 2 级时,您将获得另一辆,依此类推。
我的问题是,由于我手动填充了列表,我该如何在关卡完成后重新添加相同的精灵?
我打算使用 cars.RemoveRange(1, 18)
将它们从列表中删除,但不知道如何在不手动调用每个 sprite 的情况下将它们添加回来。我相信有一种我不知道的更简单、更好的方法。
这是脚本:
public class CarSelection : MonoBehaviour
{
public List<Sprite> cars; //store all your images in here at design time
public Image displayImage; //The current image thats visible
public Button nextCar; //Button to view next image
public Button previousCar; //Button to view previous image
private int i = 0; //Will control where in the array you are
void OnEnable()
{
//Register Button Events
nextCar.onClick.AddListener(() => NextCarButton());
previousCar.onClick.AddListener(() => PreviousCarButton());
}
private void Start()
{
cars.RemoveRange(1, 18);
if (FinishLineTouch.levelOneComplete == true) {
//Re-adding the same sprites here
}
}
public void NextCarButton()
{
if (i + 1 < cars.Count)
{
i++;
}
}
public void PreviousCarButton()
{
if (i - 1 >= 0)
{
i--;
}
}
void Update()
{
if (LevelSelect.isCarChosen == true) {
nextCar.interactable = false;
previousCar.interactable = false;
}
displayImage.sprite = cars[i];
}
}
提前致谢。
你的逻辑有点问题。
说你在 inspector 中添加了汽车 sprite,删除所有 sprite 并不理想。
您可以获得所有精灵的列表,以及一个包含可用选项的列表(从精灵[0] 开始并在您每次完成关卡时添加)。
但是,如果你想知道如何在删除它们之后将它们添加回来,你需要在全局变量中制作一个副本,所以尝试我上面的建议更有效。
你应该有两个不同的数组,一个用于精灵,另一个用于玩家是否解锁它。或者你应该创建一个自定义 class 有汽车的精灵,如果它已经解锁。
听起来你真正需要做的就是确保索引不超过解锁的汽车
if(i + 1 < cars.Count && i + 1 <= amountOfFinishedLevels)
{
i++;
// and then you really should do this in both methods and not every frame
displayImage.sprite = cars[i];
}
并且根本不 remove/readd 任何项目。
我在 c# 中有一个 Unity 脚本,其中声明了一个 public 精灵列表,然后在检查器中手动填充。
该列表包含 19 个汽车精灵。
只有当玩家完成每个级别后才能获得这些汽车。
例如:当您第一次打开游戏时,您只有一辆可用的汽车,但是如果您完成了第 1 级,您将解锁另一辆汽车,当您完成第 2 级时,您将获得另一辆,依此类推。
我的问题是,由于我手动填充了列表,我该如何在关卡完成后重新添加相同的精灵?
我打算使用 cars.RemoveRange(1, 18)
将它们从列表中删除,但不知道如何在不手动调用每个 sprite 的情况下将它们添加回来。我相信有一种我不知道的更简单、更好的方法。
这是脚本:
public class CarSelection : MonoBehaviour
{
public List<Sprite> cars; //store all your images in here at design time
public Image displayImage; //The current image thats visible
public Button nextCar; //Button to view next image
public Button previousCar; //Button to view previous image
private int i = 0; //Will control where in the array you are
void OnEnable()
{
//Register Button Events
nextCar.onClick.AddListener(() => NextCarButton());
previousCar.onClick.AddListener(() => PreviousCarButton());
}
private void Start()
{
cars.RemoveRange(1, 18);
if (FinishLineTouch.levelOneComplete == true) {
//Re-adding the same sprites here
}
}
public void NextCarButton()
{
if (i + 1 < cars.Count)
{
i++;
}
}
public void PreviousCarButton()
{
if (i - 1 >= 0)
{
i--;
}
}
void Update()
{
if (LevelSelect.isCarChosen == true) {
nextCar.interactable = false;
previousCar.interactable = false;
}
displayImage.sprite = cars[i];
}
}
提前致谢。
你的逻辑有点问题。 说你在 inspector 中添加了汽车 sprite,删除所有 sprite 并不理想。
您可以获得所有精灵的列表,以及一个包含可用选项的列表(从精灵[0] 开始并在您每次完成关卡时添加)。
但是,如果你想知道如何在删除它们之后将它们添加回来,你需要在全局变量中制作一个副本,所以尝试我上面的建议更有效。
你应该有两个不同的数组,一个用于精灵,另一个用于玩家是否解锁它。或者你应该创建一个自定义 class 有汽车的精灵,如果它已经解锁。
听起来你真正需要做的就是确保索引不超过解锁的汽车
if(i + 1 < cars.Count && i + 1 <= amountOfFinishedLevels)
{
i++;
// and then you really should do this in both methods and not every frame
displayImage.sprite = cars[i];
}
并且根本不 remove/readd 任何项目。