如何跟踪生成的随机选择的模型,以便不再重复?

时间:2016-01-07 16:09:48

标签: c# unity3d unity5

此代码生成一个随机游戏对象,但由于有时只有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);


    }

5 个答案:

答案 0 :(得分:1)

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

    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);
        }
}

答案 1 :(得分:0)

所以你的问题是,当你调用currentPoint并创建一个声音时,你会遇到一个问题,即gameObject可能被更频繁地调用来创建&#34;相同的&#34;对象?

如果是,您可以保存最后创建的游戏对象并检查它是否与新游戏相同?

这样你每次都会有不同的gameObject;)。我不知道你的呼叫之间的差异以及它们持续多长时间,但是检查gameObject应该可以解决问题。

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

此外,您可以检查验证值是否与当前活动的游戏对象不同。

答案 2 :(得分:0)

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

     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:唯一令我烦恼的是它可能会因为Random.Range而重复检查一些值......

答案 3 :(得分:0)

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

等。如果您想在重复之前循环一次可能的值,则需要采用不同的方法。

答案 4 :(得分:0)

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

     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--;
        }



    }