0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//[ExecuteInEditMode]
public class InstantiateObjects : MonoBehaviour
{
public GameObject[] objectsToInstantiate;
public Terrain terrain;
public float yOffset = 0.5f;
public int numberOfObjectsToCreate;
public bool parent = true;
public bool randomScale = false;
public float setRandScaleXMin, setRandScaleXMax;
public float setTandScaleYMin, setTandScaleYMax;
public float setTandScaleZMin, setRandScaleZMax;
private float terrainWidth;
private float terrainLength;
private float xTerrainPos;
private float zTerrainPos;
private GameObject objInstance;
private GameObject[] createdObjects;
private string objname;
public void Start()
{
//Get terrain size
terrainWidth = terrain.terrainData.size.x;
terrainLength = terrain.terrainData.size.z;
//Get terrain position
xTerrainPos = terrain.transform.position.x;
zTerrainPos = terrain.transform.position.z;
for(int i = 0; i < objectsToInstantiate.Length; i++)
{
objname = objectsToInstantiate[i].name;
MyCustomEditor.TagsAndLayers.AddTag(objname);
}
generateObjectOnTerrain();
}
public void Update()
{
}
public void DestroyObjects()
{
UpdateList(true);
if (createdObjects != null && createdObjects.Length > 0)
{
for (int i = 0; i < createdObjects.Length; i++)
{
DestroyImmediate(createdObjects[i]);
}
createdObjects = new GameObject[0];
}
}
public void generateObjectOnTerrain()
{
for (int i = 0; i < objectsToInstantiate.Length; i++)
{
//Generate random x,z,y position on the terrain
float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));
//Generate random x,y,z scale on the terrain
float randScaleX = Random.Range(setRandScaleXMin, setRandScaleXMax);
float randScaleY = Random.Range(setTandScaleYMin, setTandScaleYMax);
float randScaleZ = Random.Range(setTandScaleYMax, setRandScaleZMax);
//Apply Offset if needed
yVal = yVal + yOffset;
//Generate the Prefab on the generated position
objInstance = Instantiate(objectsToInstantiate[i], new Vector3(randX, yVal, randZ), Quaternion.identity);
if (randomScale == true)
objInstance.transform.localScale = new Vector3(randScaleX, randScaleY, randScaleZ);
if (parent)
objInstance.transform.parent = this.transform;
objInstance.tag = objname;
}
createdObjects = GameObject.FindGameObjectsWithTag(objname);
if (objname == "Teleportation Booth")
UpdateList(false);
}
private void UpdateList(bool destroy)
{
GameObject go = GameObject.Find("Main Camera");
var list = go.GetComponent<PatrolOverTerrain>().Targets;
if (destroy == false)
{
list.AddRange(createdObjects);
go.GetComponent<PatrolOverTerrain>().Targets = list;
}
if (destroy == true)
{
list.Clear();
go.GetComponent<PatrolOverTerrain>().Targets.Clear();
}
}
}
而是将脚本附加到每个gameObject我想克隆我想使用对象数组。如何为gameObject数组中的每个元素添加属性?
例如,假设我在数组2中有对象,现在我想让检查器在每个元素下都有自己的属性。 当我将每个元素的属性设置为子元素时,它将仅对特定元素生效。
取而代之的是从Y偏移量到最后一个设置兰德尺度Z最大值成为每个元素的子元素的所有元素的一般属性。 所以我可以设置每个元素的属性。
所以我可以扩展每个元素并查看他的属性。
btw:我怎样才能改变随机例如:设置兰德尺度X最小和设置兰德尺度X最小将在一行而不是两行?像:
集兰德缩放X最小值最大值 集兰德Y比例最小值最大值 集兰德规模ž最小值最大值
不知道我得到它,但你有没有尝试过一个InstantiateObject的数组? – Everts