在场景中新建一个C#的CircleShape脚本,并挂载到游戏场景中任何一个游戏物体上,然后打开脚本,并复制以下的代码!!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CircleShape : MonoBehaviour {
public GameObject prefab;//预制体
public int numberOfObjects = 20; //物体总数
public float radius = 5f; //圆圈半径
void Start()
{
for (int i = 0; i < numberOfObjects; i++)
{
//算出物体间隔角度
float angle = i * Mathf.PI * 2 / numberOfObjects;
//利用三角函数求位置
Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
//实例化生成物体
Instantiate(prefab, pos, Quaternion.identity);
}
}
}
保存场景,回到场景中,然后把需要摆放的游戏物体在Inspector属性面板赋值给prefab,然后运行游戏,就可以看到我们所赋值的游戏物体在场景中摆放成一圈圆的形状!!!