如果你想结合实例上运行几个GameObjects创建一个场景,你可以创建一个名为generateEnviroment.cs的脚本(只是一个示例名称),那么你应该p将一个或多个GameObjects作为属性添加到检查器的脚本中。
下一步是实例正确的游戏对象,然后请你修改它的属性(位置,尺寸,材质...)。每个实例都是独立的,具有您可以随意修改的属性,并且同一个GameObject的其他实例不会被更改。
public GameObject customGameObject1;
public GameObject customGameObject2;
void Start()
{
generateEnviroment()
}
void generateEnviroment()
{
//In case you want to add other type of GameObject, like a car or sth you have created:
GameObject myInstantiatedGameObject = GameObject.Instantiate (customGameObject1);
//You change its position
myInstantiatedGameObject.transform.position = new Vector3(0, 0.5F, 0);
// Widen the object by 0.1
myInstantiatedGameObject.transform.localScale += new Vector3(0.1F, 0, 0);
//Change material properties, assuming it has a material component
Renderer rend = myInstantiatedGameObject.GetComponent();
rend.material.shader = Shader.Find("Specular");
rend.material.SetColor("_SpecColor", Color.red);
...
}
你甚至可以添加到游戏对象的脚本组件,你也将能够在该脚本独立的游戏对象的每个实例访问和修改每个属性(可变)(只要你不” t声明该属性为静态)