简体   繁体   English

为什么Random.Range与Unity中列表中的按钮位置重叠?

[英]Why Random.Range is overlapping button positions from the list in Unity?

I have a list of UI Buttons and Vector2 for positions. 我有一个UI Buttons和Vector2位置列表。 I am using random.range to place buttons randomly every time i load them. 每次加载按钮时,我都会使用random.range随机放置按钮。 But it never shows all 5 buttons at different positions. 但是它永远不会显示所有5个按钮在不同位置。 instead overlapping some of them. 而是重叠其中的一些。 Can anyone please help me with this? 谁能帮我这个忙吗?

[SerializeField] List<Button> answersButtons = new List<Button>();
[SerializeField] List<Vector2> positions = new List<Vector2>();

void ShuffleAnswersList()
{             

    for (int i = 0; i < answersButtons.Count; i++)
    {              
       Vector2 tempPosition = 
       answersButtons[i].GetComponent<RectTransform>().position;

       int randomIndex = Random.Range(0, positions.Count);

       answersButtons[i].transform.position = positions[randomIndex];
       answersButtons[randomIndex].GetComponent<RectTransform>().position 
       = tempPosition;

       Debug.Log(randomIndex);          
    }      

}

// shuffle positions
public void Shuffle()
{
    for (int i = 0; i < positions.Count; i++)
    {
        int rnd = Random.Range(0, positions.Count);
        Vector2 tempGO = positions[rnd];
        positions[rnd] = positions[i];
        positions[i] = tempGO;
    }
}

Try this code, not sure where your Random.Range() function comes from, but there is a built-in Random in System that you can use to get random integers. 尝试使用此代码,不确定您的Random.Range()函数来自何处,但是System中有一个内置的Random,可用于获取随机整数。 In this example the random is declared ahead and the Next() function is used to get a random number: 在此示例中,将在前面声明随机数,然后使用Next()函数获取随机数:

void Shuffle()
{
    System.Random random = new System.Random();
    for (int i = 0; i < positions.Count; i++)
    {
        int rnd = random.Next(0, positions.Count);
        Vector2 tempGO = positions[rnd];
        positions[rnd] = positions[i];
        positions[i] = tempGO;
    }
}

When you are doing this : 当您这样做时:

int randomIndex = Random.Range(0, positions.Count);

answersButtons[i].transform.position = positions[randomIndex];

You may get the same value when you are randoming, that's why some button are overlapping. 当您进行随机化时,您可能会获得相同的值,这就是某些按钮重叠的原因。

You want to either : 您想要:

  • Shuffle the list of the position (what you did) and keep the same order for answers list 随机排列职位列表(您做了什么),并保持答案列表的顺序相同

  • Shuffle the list of answers and keep the same order for position 随机排列答案列表,并保持排名相同的顺序

Try this instead : 试试这个代替:

void ShuffleAnswersList()
{             
    //  Notice this v--------------------v to avoid out of range exception
    for (int i = 0; i < positions.Count && i < answersButtons.Count; i++)
    {
// always use i ------V---------------------------------V
       answersButtons[i].transform.position = positions[i];
    }
}

Of course, make sure you call your method Shuffle first. 当然,请确保先调用方法Shuffle This method need to be fixed, Random.Range(min, max) may return max (and provide a out of range exception) 此方法需要固定, Random.Range(min, max)可能返回max (并提供超出范围的异常)

// shuffle positions
public void Shuffle()
{
    for (int i = 0; i < positions.Count; i++)
    {
// notice this -------------------------------------V
        int rnd = Random.Range(0, positions.Count - 1);
        Vector2 tempGO = positions[rnd];
        positions[rnd] = positions[i];
        positions[i] = tempGO;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM