我有 canvas and a panel 。我想 Spawn 不同的图像UI对象在我的面板内的随机位置(在屏幕上)。我采用了面板的x和y min和最大限制,以生成一个随机位置(使用这些限制),但问题是对象不是在所需位置实例化。这是我的代码。
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
。您也不需要-1
为您的List.Count
。在下面的评论中检查链接。
我认为以下脚本将执行您正在寻找的事情:
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];
}
}
让我知道您是否需要更多解释。
要管理帆布对象的位置,您应该绝对使用RectTransform
而不是常规transform
。在您的情况下,您必须做类似:
m_RectTransform = GetComponent<RectTransform>();
对于设置位置 - 在画布内部的对象上看到的坐标不是.position
,而是RectTransform.anchoredPosition
,因此您应该在代码中使用它。这是anchoredPosition
的官方文档。
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);
}
}
}
这应该帮助您。