如何在不使用FindGameObjectsWithTag的情况下获取特定的GameObjects数组?

时间:2017-04-07 03:12:49

标签: c# unity3d unity5

在第一个脚本中我克隆了一些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;
    }
}

在我第一次给孩子们提供对象之前,我克隆了一个Tag名称。 所以我能做到:

var ObjectsToDestroy = GameObject.FindGameObjectsWithTag("ClonedObject");

但现在我没有使用这条线。所以克隆的所有对象都没有标记。 所以现在我没有使用这条线。

但现在我想从另一个脚本中获取所有克隆的对象:

void Start()
    {
        anims = GetComponent<Animations>();
        waypoints = GameObject.FindGameObjectsWithTag("ClonedObject");
        originalPosition = transform.position;
    }

但是由于克隆的onjects没有任何标记,因此waypoint将为空。 waypoints是GameObject:GameObject [] waypoints。

的数组

我应该为每个克隆对象提供标记名吗?或者有没有其他方法可以将所有克隆的对象都放入数组/列表中?

2 个答案:

答案 0 :(得分:1)

解决方案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 
    }
}

答案 1 :(得分:1)

  

但是由于克隆的onjects没有任何标记,因此   空。 waypoints是GameObject:GameObject [] waypoints。

的数组

您可以在实例化后更改GameObject的标记。

var o = Instantiate(ObjectToCreate);
o.tag = "ClonedObject";

确保您创建一个名为&#34; ClonedObject &#34;的标记。在里面 编辑。现在,如果有实例化的具有该标记名称的对象,GameObject.FindGameObjectsWithTag("ClonedObject");应返回一些内容。

  

我应该为每个克隆对象提供标记名称吗?或者还有其他什么   将所有克隆对象转换为数组/列表的方法?

使用FindGameObjectsWithTag标记应该可以。如果您关心性能,那么请使用List,因为FindGameObjectsWithTag将搜索带有该标记的GameObjects。它比较慢。

我注意到你已经在List中存储了实例化的GameObjects。

更改

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++)
{

}