c# - 我如何在所有对象上搜索特定的脚本名称并对其进行处理?

标签 c# unity3d

我在层次结构中有一个游戏对象作为父对象。 我想遍历脚本名称的所有子游戏对象:

元素信息

然后我想用脚本检查哪些 GameObjects 字段 TextArea 填充了文本而不是空字符串。

然后将那些不为空的与我点击的对象的名称进行比较,然后根据信息获取与该游戏对象相关的信息。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemInformation : MonoBehaviour
{
    [TextArea]
    public string description;
}

这是我想要遍历子项并与我通过其名称单击的内容进行比较的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DetectInteractable : MonoBehaviour
{
    public Camera cam;
    public float distanceToSee;
    public string objectHit;
    public Image image;
    public bool interactableObject = false;
    public Canvas canvas;
    public Text interactableText;
    public ItemInformation iteminformation;

    private RaycastHit whatObjectHit;
    private RotateObject rotateobj;
    private bool hasDescription = false;
    private bool clickForDescription = false;
    private int layerMask = 1 << 8;

    private void Start()
    {
        rotateobj = GetComponent<RotateObject>();
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            clickForDescription = true;
        }

        Debug.DrawRay(cam.transform.position, cam.transform.forward * distanceToSee, Color.magenta);

        if (rotateobj.rotating == false)
        {
            if (Physics.Raycast(cam.transform.position, cam.transform.forward, out whatObjectHit, distanceToSee, layerMask))
            {
                image.color = Color.red;
                objectHit = whatObjectHit.collider.gameObject.name;
                interactableObject = true;
                interactableText.gameObject.SetActive(true);
                interactableText.text = whatObjectHit.collider.gameObject.name;
                print("Hit ! " + whatObjectHit.collider.gameObject.name);
                hasDescription = true;
            }
            else
            {
                image.color = Color.clear;
                image.color = Color.white;
                interactableText.gameObject.SetActive(false);
                hasDescription = false;
                clickForDescription = false;
                print("Not Hit !");
            }
        }
    }

    private void OnGUI()
    {
        if (hasDescription == true && clickForDescription == true)
        {
            var centeredStyle = GUI.skin.GetStyle("Label");
            centeredStyle.alignment = TextAnchor.UpperCenter;
            GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 25, 100, 50), iteminformation.description, centeredStyle);
        }
    }

    public class ViewableObject : MonoBehaviour
    {
        public string displayText;
        public bool isInteractable;
    }
}

例如,假设我移动相机并从一扇门往外看。 假设门的游戏对象名称是:SmallDoor

现在在这部分:

private void OnGUI()
        {
            if (hasDescription == true && clickForDescription == true)
            {
                var centeredStyle = GUI.skin.GetStyle("Label");
                centeredStyle.alignment = TextAnchor.UpperCenter;
                GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 25, 100, 50), iteminformation.description, centeredStyle);
            }
        }

我想遍历父 GameObject 下的子项检查哪些对象附加了脚本 ItemInformation 获取对象名称将其与我单击的对象名称进行比较例如 SmallDoor 并显示信息(iteminformation.description)小门

最佳答案

如果您尝试检索的对象是父 GameObject 的子对象,那么您可以通过简单地执行以下操作来遍历父对象的子对象:

foreach(GameObject child in parent)
{
    if(child.GetComponent<ItemInformation>() != null)
    {
       //do something
    }
}

如果它们没有子代到父游戏对象,您可以为它们分配一个公共(public)“标签”并以这种方式获取它们,然后遍历它们并执行相同的操作。

此外,您应该在问题的标题中说明这是 Unity 特定的。

关于c# - 我如何在所有对象上搜索特定的脚本名称并对其进行处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47839554/

相关文章:

c# - 无法转换异常类型的对象

c# - 在切换组中按下切换按钮调用多个按钮

c# - WWW/UnityWebRequest POST/GET 请求不会从 server/url 返回最新数据

android - Google Play 商店中不支持的 API 警告

c# - 地形树实例无法正确设置

c# - 标准化和平滑鼠标移动?

c# - Entity Framework 生成 short 而不是 int

c# - Visual Studio 生成错误 :requested operation cannot be performed on a file with a user-mapped

c# - Unity3d-播放和暂停之间的过渡时声音粗糙

ios - xcode 中的 iphonesimulator(未找到 SDK)错误