首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过名称找到所有特定的子游戏对象并将它们添加到列表中?

如何通过名称找到所有特定的子游戏对象并将它们添加到列表中?
EN

Stack Overflow用户
提问于 2017-07-25 07:46:29
回答 2查看 64关注 0票数 0
代码语言:javascript
复制
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class SpinableObject
{
    public Transform t;
    public float rotationSpeed;
    public float minSpeed;
    public float maxSpeed;
    public float speedRate;
    public bool slowDown;

    public void RotateObject()
    {
        if (rotationSpeed > maxSpeed)
            slowDown = true;
        else if (rotationSpeed < minSpeed)
            slowDown = false;

        rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
        t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
    }
}
public class SpinObject : MonoBehaviour
{
    [SerializeField]
    [Header("Global Rotation")]
    [Space(5)]
    public float rotationSpeed;
    public float minSpeed;
    public float maxSpeed;
    public float speedRate;
    public bool slowDown;
    public List<GameObject> allObjects;


    [Space(5)]
    [Header("Rotation Mode")]
    [LabeledBool("Global Rotation", "Individual Rotation")]
    [SerializeField]
    bool _rotationMode = true;

    [Header("Individual Rotation")]
    [Space(3)]
    public SpinableObject[] individualObjects;


    private void Start()
    {
        allObjects = new List<GameObject>();
        foreach(Transform t in transform)
        {

        }
    }

    // Update is called once per frame
    void Update()
    {
        if (_rotationMode == false)
        {

            foreach (var spinner in individualObjects)
                spinner.RotateObject();
        }
        else
        {
            for (int i = 0; i < allObjects.Count; i++)
            {
                RotateAllObjects(allObjects[i].transform);
            }
        }
    }

    private void RotateAllObjects(Transform t)
    {
        if (rotationSpeed > maxSpeed)
            slowDown = true;
        else if (rotationSpeed < minSpeed)
            slowDown = false;

        rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
        t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
    }
}

在开始部分中,我想添加子对象的名称,而不是标记为“螺旋桨”,我有4个子对象:"Propeller1“、"Propeller2”、"Propeller3“、"Propeller4”

我想将这4个对象添加到allObjects中,这样它将只旋转脚本附加到的该对象的4个属性。因为我要将对象克隆给更多的人,所以我不想使用FindByTag。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-25 07:54:56

如果要添加包含脚本的GameObject的所有子元素:

代码语言:javascript
复制
private void Start()
    {
        allObjects = new List<GameObject>();
        foreach(Transform child in transform)
        {
            allObjects.Add(child.gameObject)
        }
    }

其他选项,以防您想要添加来自其他gameObjects的子级:

您可以通过名称找到GameObjects。甚至是孩子们也在走这条路。例如:

代码语言:javascript
复制
   aFinger = transform.Find("LeftShoulder/Arm/Hand/Finger");

如果您的孩子有您给出的名字作为例子,您可以这样做:

代码语言:javascript
复制
String Prefix = Propeller;
String nameGO;

for(int i = 1; i < 4; i++)
{
   nameGO = Propeller + i;
   GameObject mGameObject = transform.Find("PathToChildren/nameGO");
  //Now rotate or do something with it
}
票数 2
EN

Stack Overflow用户

发布于 2017-07-25 08:05:10

如果这只是一次调用,我建议使用以下代码片段:

代码语言:javascript
复制
// names you want to search for
string[] searchForNames = new string[] { "Propeller1" , "Propeller2" , "Propeller3" , "Propeller4" };

// list of objects that matches the search
List<GameObject> wantedObjects = new List<GameObject>();
// placeholder for all objects in the current scent
List<GameObject> allObjects = new List<GameObjects>();
// retrieve all objects from active scene ( wont retrieve objects marked with DontDestroyOnLoad from other scenes )
SceneManager.GetActiveScene()GetRootGameObjects( allObjects );
// iterate through all objects found in the current scene
foreach(GameObject obj in allObjects)
{
    // check if name is contained by searchForNames array
    if(searchForNames.Contains(obj.name))
    {
        // add to the matching list
        wantedObjects.Add(obj);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45297008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档