C# 如何为gameObject数组中的每个元素添加属性? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; //[执行编辑模式] 公共类实例化对象:MonoBehavior { 公共游戏对象[]对象实例化; 公共地形; 公共浮点数偏移=0.5f; public int numberOfObjectsToCreate; public bool parent=true; 公共布尔随机标度=假; 公共浮点数setRandScaleXMin、setRandScaleXMax; 公共浮点数setTandScaleYMin,setTandScaleYMax; 公共浮点数setTandScaleZMin、setRandScaleZMax; 私有浮动地形宽度; 私人浮动平台长度; 私人浮动xTerrainPos; 私人浮动zTerrainPos; 私人游戏对象; 私有游戏对象[]创建对象; 私有字符串objname; 公开作废开始() { //获取地形大小 terrainWidth=terrain.terrainData.size.x; terrainLength=terrain.terrainData.size.z; //获取地形位置 xTerrainPos=terrain.transform.position.x; zTerrainPos=地形.transform.position.z; for(int i=0;i0) { for(int i=0;i
相反,将脚本附加到每个我想克隆的游戏对象上,我想使用对象数组 例如,假设数组中有2个对象,现在我希望在inspector中的每个元素下都有自己的属性。 当我将属性设置为每个元素的子元素时,它将仅对特定元素生效。 取而代之的是所有元素的常规属性,以使该属性从Y偏移直到最后一次将Rand Scale Z Max设置为每个元素的子级。 所以我可以设置每个元素的属性 所以我可以展开每个元素并查看其属性 顺便问一句:我如何更改随机设置,例如:设置Rand Scale X Min和设置Rand Scale X Min将位于一行,而不是两行?比如: 设置Rand Scale X Min Max 设置Rand Scale Y Min MaxC# 如何为gameObject数组中的每个元素添加属性? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; //[执行编辑模式] 公共类实例化对象:MonoBehavior { 公共游戏对象[]对象实例化; 公共地形; 公共浮点数偏移=0.5f; public int numberOfObjectsToCreate; public bool parent=true; 公共布尔随机标度=假; 公共浮点数setRandScaleXMin、setRandScaleXMax; 公共浮点数setTandScaleYMin,setTandScaleYMax; 公共浮点数setTandScaleZMin、setRandScaleZMax; 私有浮动地形宽度; 私人浮动平台长度; 私人浮动xTerrainPos; 私人浮动zTerrainPos; 私人游戏对象; 私有游戏对象[]创建对象; 私有字符串objname; 公开作废开始() { //获取地形大小 terrainWidth=terrain.terrainData.size.x; terrainLength=terrain.terrainData.size.z; //获取地形位置 xTerrainPos=terrain.transform.position.x; zTerrainPos=地形.transform.position.z; for(int i=0;i0) { for(int i=0;i,c#,unity3d,unity5,C#,Unity3d,Unity5,相反,将脚本附加到每个我想克隆的游戏对象上,我想使用对象数组 例如,假设数组中有2个对象,现在我希望在inspector中的每个元素下都有自己的属性。 当我将属性设置为每个元素的子元素时,它将仅对特定元素生效。 取而代之的是所有元素的常规属性,以使该属性从Y偏移直到最后一次将Rand Scale Z Max设置为每个元素的子级。 所以我可以设置每个元素的属性 所以我可以展开每个元素并查看其属性 顺便问一句:我如何更改随机设置,例如:设置Rand Scale X Min和设置Rand Scale
设置Rand Scale Z Min Max这样做的好方法是创建自定义类,然后创建该类实例的数组,但它需要更多的行。如果您想了解更多有关此搜索的信息 最简单的方法可能是将变量包装在结构中,如下所示。然后,您可以将它们“展开”到您的类中,也可以不展开。这取决于你想要什么
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();
}
}
}
不确定我是否理解,但您是否尝试过实例化对象数组?
using UnityEngine;
using System;
public class InstantiateObjects : MonoBehaviour {
public ObjectsToInst[] objectsToInstantiate;
[Serializable]
public struct ObjectsToInst
{
public GameObject objectToInstantiate;
public Terrain terrain;
public float yOffset;
public int numberOfObjectsToCreate;
public bool parent;
public bool randomScale;
public float setRandScaleXMin, setRandScaleXMax;
public float setTandScaleYMin, setTandScaleYMax;
public float setTandScaleZMin, setRandScaleZMax;
}
}