在 canvas 中的随机位置生成图像

Spawn image at random position in canvas

我有一个canvas,里面有一个面板。我想 spawn 不同的 image UI object 在面板内的随机位置(在屏幕上)。我采用了面板的 x 和 y 最小和最大限制以生成随机位置(具有这些限制)但问题是对象未在所需位置实例化。这是我的代码。

public class ImageSpawnerScreen : MonoBehaviour {
    public float waitTime = 2;
    public float cubeSpawnTotal = 10;
    public List<GameObject> cubePrefabList;

    float xPosMinLimit = -347;
    float xPosMaxLimit = 340;
    float yPosMinLimit = -458f;
    float yPosMaxLimit = 450f;

    public GameObject panel;
    void Start()
    {
        StartCoroutine(SpawnCube());
    }


    IEnumerator SpawnCube()
    {
        for (int i = 0; i < cubeSpawnTotal; i++)
        {
            GameObject prefabToSpawn = cubePrefabList[Random.Range(0, cubePrefabList.Count - 1)];
            //Vector3 spawnPosition = Camera.main.ScreenToViewportPoint(new Vector3(Random.Range(0,Screen.width),0,Random.Range(0,Screen.height)));  //Random.Range(xPosMinLimit, xPosMaxLimit);

            float xPos = Random.Range(xPosMinLimit, xPosMaxLimit);
            float yPos = Random.Range(yPosMinLimit, yPosMaxLimit);
            Vector3 spawnPosition = new Vector3(xPos, yPos, 0f);
            GameObject spwanObj = Instantiate(prefabToSpawn, spawnPosition, Quaternion.identity) as GameObject;
            spwanObj.transform.parent = panel.transform;
            spwanObj.transform.position = spawnPosition;
            yield return new WaitForSeconds(waitTime);
        }
    }
}

我检查了实例化对象的位置远离给定的随机范围位置。我做错了什么?我认为它是 Rect Transform 所以我必须以不同的方式设置它的位置。

要管理 Canvas 对象的位置,您绝对应该使用 RectTransform 而不是常规的 transform。在你的情况下,你必须做类似的事情:

m_RectTransform = GetComponent<RectTransform>();

至于设置位置 - 您在 Canvas 内的对象上看到的坐标不是 .position 而是 RectTransform.anchoredPosition,因此您应该在代码中使用它。这是 anchoredPosition 上的官方 documentation

确实,您需要在 Canvas 中使用 RectTransform。 您也不需要 -1 List.Count。检查下面评论中的link。

我认为以下脚本可以满足您的需求:

public class ImageSpawnerScreen : MonoBehaviour
{
    public float waitTime = 2;
    public float cubeSpawnTotal = 10;
    public List<GameObject> imagesList;

    public RectTransform panel;

    void Start()
    {
        StartCoroutine(SpawnImage());
    }


    IEnumerator SpawnImage()
    {
        for (int i = 0; i < cubeSpawnTotal; i++)
        {
            GameObject imageToSpawn = imagesList[Random.Range(0, imagesList.Count)]; // Remove -1 after count since is exclusive for int (https://docs.unity3d.com/ScriptReference/Random.Range.html)

            Vector3 spawnPosition = GetBottomLeftCorner(panel) - new Vector3(Random.Range(0, panel.rect.x), Random.Range(0, panel.rect.y), 0);

            print("Spawn image at position: " + spawnPosition);

            GameObject spwanObj = Instantiate(imageToSpawn, spawnPosition, Quaternion.identity, panel);
            yield return new WaitForSeconds(waitTime);
        }
    }

    Vector3 GetBottomLeftCorner(RectTransform rt)
    {
        Vector3[] v = new Vector3[4];
        rt.GetWorldCorners(v);
        return v[0];
    }
}

如果您需要更多解释,请告诉我。

public class ImageSpawnerScreen : MonoBehaviour {
    public float waitTime = 2;
    public float cubeSpawnTotal = 10;
    public List<GameObject> cubePrefabList;

    public GameObject panel;
    void Start()
    {
        StartCoroutine(SpawnCube());
    }


    IEnumerator SpawnCube()
    {
        for (int i = 0; i < cubeSpawnTotal; i++)
        {
            GameObject prefabToSpawn = cubePrefabList[Random.Range(0, cubePrefabList.Count - 1)];
            //Vector3 spawnPosition = Camera.main.ScreenToViewportPoint(new Vector3(Random.Range(0,Screen.width),0,Random.Range(0,Screen.height)));  //Random.Range(xPosMinLimit, xPosMaxLimit);

            float xPos = Random.Range(0, Screen.width);
            float yPos = Random.Range(0, Screen.height);
            Vector3 spawnPosition = new Vector3(xPos, yPos, 0f);
            GameObject spwanObj = Instantiate(prefabToSpawn, spawnPosition, Quaternion.identity) as GameObject;
            spwanObj.transform.parent = panel.transform;
            spwanObj.transform.position = spawnPosition;
            yield return new WaitForSeconds(waitTime);
        }
    }
}

这应该能帮到你。