public class Mapping3D : MonoBehaviour
{
public GameObject _cubePrefab;
private const int MaxCubes = 48;
private const float SpawnTime = 0.25f;
private float _timer = SpawnTime;
private List<GameObject> _cubes = new List<GameObject>(32);
void Update()
{
_timer -= Time.deltaTime;
if (_timer <= 0f)
{
_timer = SpawnTime;
SpawnCube();
if (_cubes.Count > MaxCubes)
{
RemoveCube();
}
}
}
private void SpawnCube()
{
Quaternion rotation = Quaternion.Euler(Random.Range(-180f, 180f), Random.Range(-180f, 180f), Random.Range(-180f, 180f));
float scale = Random.Range(0.1f, 0.6f);
GameObject go = (GameObject)GameObject.Instantiate(_cubePrefab, this.transform.position, rotation);
Transform t = go.GetComponent<Transform>();
t.localScale = new Vector3(scale, scale, scale);
_cubes.Add(go);
}
private void RemoveCube()
{
GameObject go = _cubes[0];
_cubes.RemoveAt(0);
Destroy(go);
}
}
此篇博客介绍了如何使用Unity3D创建一个场景,通过Prefab生成并随机分布48个立方体,每个立方体贴上随机大小和旋转。更新逻辑中,每隔一定时间会新增一个立方体,当达到最大数量时会移除最旧的立方体,实现动态效果。

8788

被折叠的 条评论
为什么被折叠?



