我如何跟踪哪个随机选择的模型是生成的,所以它赢得了';不要再重复了

本文关键字:跟踪 何跟踪 模型 选择 随机 | 更新日期: 2023-09-27 18:29:26

此代码生成了一个随机游戏对象,但由于只有9个游戏对象,有时随机生成的对象会多次重新生成。我怎么能限制它,并且只生成一个游戏对象一次?

     public GameObject[] models;
     public static GameObject currentPoint;
     int index;
     public static string randomName;
     public AudioSource FindTheNumber;
public void PlayNumbers()
    {   

        models = GameObject.FindGameObjectsWithTag ("numbers");
        index = Random.Range (0,models.Length);
        currentPoint = models [index];
        randomName = currentPoint.name;
        print ("Trackable " + randomName);
        FindTheNumber.Play ();
        currentPoint.GetComponent<AudioSource> ().PlayDelayed(2);

    }

我如何跟踪哪个随机选择的模型是生成的,所以它赢得了';不要再重复了

如果我理解正确,在一个单独的列表中跟踪选定的游戏对象如何。

    public static List<GameObject> models;
    public static List<GameObject> selectedModels = new List<GameObject>();
    public static GameObject currentPoint;
    int index;
    public static string randomName;
    public AudioSource FindTheNumber;
    public static Random random = new Random();
    public void PlayNumbers()
    {   
        models = GameObject.FindGameObjectsWithTag("numbers").Except(selectedModels).ToList();
        if ((models == null) || (!models.Any()))
        {
            Console.WriteLine("No new game objects");
        }
        else
        {
            index = random.Next(models.Count);
            currentPoint = models[index];
            randomName = currentPoint.name;
            print ("Trackable " + randomName);
            FindTheNumber.Play ();
            currentPoint.GetComponent<AudioSource> ().PlayDelayed(2);
            selectedModels.Add(currentPoint);
        }
}

所以你的问题是,当你调用currentPoint并创建一个声音时,例如,你会遇到这样的问题,即在创建"同一"对象时,gameObject可能会更频繁地被调用?

如果是,您可以保存上次创建的gameObject,并检查它是否与新的相同?

这样,每次都会有一个不同的gameObject;)。我不知道你的调用之间的delta是多少,以及它们持续了多久,但检查gameObject应该可以做到这一点。

如果对象正在执行其工作,则可以创建一个存储信息的变量。

此外,您可以检查并验证这些值是否与当前活动的gameObjects不同。

如果我理解你的问题,纠正你的代码应该是这样的。

     public GameObject[] models;
 public static GameObject currentPoint;
 int index;
 public static string randomName;
 public AudioSource FindTheNumber;
 public void PlayNumbers()
 {   
    do {
      models = GameObject.FindGameObjectsWithTag ("numbers");
      index = Random.Range (0,models.Length);
      currentPoint = models [index];
   } while(currentPoint.getComponent<AudioSource>().isPlaying);
   randomName = currentPoint.name;  
   print ("Trackable " + randomName);
   FindTheNumber.Play ();
   currentPoint.GetComponent<AudioSource> ().PlayDelayed(2);
 }

现在,代码将至少循环一次。如果您尝试使用的对象已经在播放,则代码将再次在数组中循环。一旦它发现一个没有播放的对象,代码就会继续并做它应该做的事情

如果你有任何错误,或者如果我的解决方案不正确,请再次发布并告诉我们你想要什么(详细)

祝你今天过得愉快;)

PS:唯一让我恼火的是,由于随机范围…

,它可能会重复检查一些值

Vancold.at推荐的一种方法

public void PlayNumbers()
{
    var rnd = new Random();
    models = GameObject.FindGameObjectsWithTag("numbers");
    while (true)
    {
        index = rnd.Next(0, models.Length - 1);
        var newPoint = models[index];
        //assumes currentPoint is initially null the first time this method is called
        if(currentPoint == null || newPoint.name != currentPoint.name)
        { 
            currentPoint = newPoint;
            break;
        }
    }
    randomName = currentPoint.name;
    print("Trackable " + randomName);
    FindTheNumber.Play();
    currentPoint.GetComponent<AudioSource>().PlayDelayed(2);
}

请记住,这只能确保同一个GameObject实例不会连续使用两次。它不会阻止同一对象被多次使用。例如,如果您有9个名称为"1"、"2"、"3"、"4"、"5"、"6"、"7"、"8"、"9"的GameObject实例,则上面的代码允许以下输出:

Trackable 5
Trackable 7
Trackable 5
Trackable 4
Trackable 7

等等。如果你想在重复之前循环一遍可能的值,那么就需要一种不同的方法。

我找到了解决方案,我会发布我写的代码,以便将来为某人服务:

     public GameObject[] models;
     public static GameObject currentPoint;
     int index;
     public static string randomName;
     public AudioSource FindTheNumber;
     int i =9;
public void PlayNumbers()
    {   

       models = GameObject.FindGameObjectsWithTag ("numbers");
        if (i >= 0) 
        {
            index = i;
            currentPoint = models [index];
            randomName = currentPoint.name;
            print ("Trackable " + randomName);
            FindTheNumber.Play ();
            currentPoint.GetComponent<AudioSource> ().PlayDelayed (2);
            i--;
        } 
        else 
        {
            i=9;
            index = i;
            currentPoint = models [index];
            randomName = currentPoint.name;
            print ("Trackable " + randomName);
            FindTheNumber.Play ();
            currentPoint.GetComponent<AudioSource> ().PlayDelayed (2);
            i--;
        }

    }