在第一个脚本中,我克隆了一些GameObjects:
using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class CloneObjects : MonoBehaviour
{
public GameObject ObjectToCreate;
public int objectsHeight = 3;
[HideInInspector]
public GameObject[] objects;
// for tracking properties change
private Vector3 _extents;
private int _objectCount;
private float _objectSize;
private List<GameObject> cloneList = new List<GameObject>();
/// <summary>
/// How far to place spheres randomly.
/// </summary>
public Vector3 Extents;
/// <summary>
/// How many spheres wanted.
/// </summary>
public int ObjectCount;
public float ObjectSize;
public static float LargestSize = 0;
// Use this for initialization
void Start()
{
Clone();
//objects = GameObject.FindGameObjectsWithTag("ClonedObject");
objects = cloneList.ToArray();
foreach (var element in objects)
{
float Size = element.transform.localScale.x;
if (Size > LargestSize)
LargestSize = Size;
}
}
private void OnValidate()
{
// prevent wrong values to be entered
Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
ObjectCount = Mathf.Max(0, ObjectCount);
ObjectSize = Mathf.Max(0.0f, ObjectSize);
}
private void Reset()
{
Extents = new Vector3(250.0f, 20.0f, 250.0f);
ObjectCount = 100;
ObjectSize = 20.0f;
}
// Update is called once per frame
void Update()
{
}
private void Clone()
{
if (Extents == _extents && ObjectCount == _objectCount && Mathf.Approximately(ObjectSize, _objectSize))
return;
// cleanup
//var ObjectsToDestroy = GameObject.FindGameObjectsWithTag("ClonedObject");
var ObjectsToDestroy = objects;
foreach (var t in ObjectsToDestroy)
{
if (Application.isEditor)
{
DestroyImmediate(t);
}
else
{
Destroy(t);
}
}
var withTag = GameObject.FindWithTag("Terrain");
if (withTag == null)
throw new InvalidOperationException("Terrain not found");
for (var i = 0; i < ObjectCount; i++)
{
var o = Instantiate(ObjectToCreate);
cloneList.Add(o);
o.transform.SetParent(base.gameObject.transform);
o.transform.localScale = new Vector3(ObjectSize, ObjectSize, ObjectSize);
// get random position
var x = Random.Range(-Extents.x, Extents.x);
var y = Extents.y; // sphere altitude relative to terrain below
var z = Random.Range(-Extents.z, Extents.z);
// now send a ray down terrain to adjust Y according terrain below
var height = 10000.0f; // should be higher than highest terrain altitude
var origin = new Vector3(x, height, z);
var ray = new Ray(origin, Vector3.down);
RaycastHit hit;
var maxDistance = 20000.0f;
var nameToLayer = LayerMask.GetMask("Terrain");
var layerMask = 1 << nameToLayer;
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
{
var distance = hit.distance;
y = height - distance + y; // adjust
}
else
{
Debug.LogWarning("Terrain not hit, using default height !");
}
// place !
o.transform.position = new Vector3(x, y + objectsHeight, z);
}
_extents = Extents;
_objectCount = ObjectCount;
_objectSize = ObjectSize;
}
}在我第一次给子对象命名之前,我克隆了一个标签名。所以我可以做:
var ObjectsToDestroy = GameObject.FindGameObjectsWithTag("ClonedObject");但现在我不用这句话了。所以所有克隆的对象都没有标记。所以现在我不用这句话了。
但是现在我想从另一个脚本中获取所有克隆的对象:
void Start()
{
anims = GetComponent<Animations>();
waypoints = GameObject.FindGameObjectsWithTag("ClonedObject");
originalPosition = transform.position;
}但是,由于克隆的单个对象没有任何标记,所以路径点将为空。路径点是GameObject: GameObject[]路径点的数组。
我应该给每个克隆对象取标签名吗?或者是否有其他方法将所有克隆的对象都放入数组/列表中?
发布于 2017-04-07 04:22:51
解A
使您的cloneList公开或创建一个返回它的公共属性,然后从另一个脚本访问它。
public class CloneObjects : MonoBehaviour
{
public List<GameObject> cloneList = new List<GameObject>();
}
public class AnotherScript : MonoBehaviour
{
void Start()
{
var cloneObjects = obj_with_CloneObjects.GetComponent<CloneObjects>();
//cloneObjects.cloneList
}
}解B
在另一个脚本中创建一个List<GameObject>类型的字段。克隆后,将cloneList分配给该字段。
public class CloneObjects : MonoBehaviour
{
public List<GameObject> cloneList = new List<GameObject>();
void Clone()
{
}
void Start()
{
Clone();
obj_with_AnotherScript.GetComponent<AnotherScript>.cloneList = cloneList;
}
}
public class AnotherScript : MonoBehaviour
{
public List<GameObject> cloneList = new List<GameObject>();
void Start()
{
//this.cloneList
}
}发布于 2017-04-07 04:23:04
但是,由于克隆的单个对象没有任何标记,所以路径点将为空。路径点是GameObject: GameObject[]路径点的数组。
您可以在实例化GameObject之后更改它的标记。
var o = Instantiate(ObjectToCreate);
o.tag = "ClonedObject";只需确保在编辑器中创建了一个名为"ClonedObject“的标记。现在,如果有带有标记名的实例化对象,GameObject.FindGameObjectsWithTag("ClonedObject");应该返回一些内容。
我应该给每个克隆对象取标签名吗?或者是否有其他方法将所有克隆的对象都放入数组/列表中?
在FindGameObjectsWithTag中使用标记应该可以。如果您关心性能,那么使用List,因为FindGameObjectsWithTag将使用该标记搜索GameObjects。这是比较慢的。
我注意到您已经将实例化的GameObjects存储在List中。
变化
private List<GameObject> cloneList = new List<GameObject>();至
public List<GameObject> cloneList = new List<GameObject>();如果场景中只有一个CloneObjects脚本实例,则可以使用FindObjectOfType查找CloneObjects脚本,然后访问cloneList变量。
CloneObjects cloneObjectsInstance = FindObjectOfType<CloneObjects>();
List<GameObject> clonedObj = cloneObjectsInstance.cloneList;
for (int i = 0; i < clonedObj.Count; i++)
{
}如果场景中有多个CloneObjects脚本实例,请查找脚本附加的GameObject,然后在其上执行GetComponent。
CloneObjects cloneObjectsInstance = GameObject.Find("ObjectCloneObjectsIsAttachedTO").GetComponent<CloneObjects>();
List<GameObject> clonedObj = cloneObjectsInstance.cloneList;
for (int i = 0; i < clonedObj.Count; i++)
{
}https://stackoverflow.com/questions/43268995
复制相似问题