gpt4 book ai didi

c# - 如何访问 UI 文本?

转载 作者:太空宇宙 更新时间:2023-11-03 13:05:06 25 4
gpt4 key购买 nike

我想通过我的脚本设置我的 UI 文本。我可以使用公共(public)文本访问,但我想使用私有(private)游戏对象访问。

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

public class WebsiteAd : MonoBehaviour
{
private GameObject name;

void Awake()
{
name = GameObject.Find("/Item0/Name");
}

void Start()
{
name.GetComponent<Text>().text = "My Text";
}

我的层次结构 Hierarchy

最佳答案

GameObject.Find文档指出:“此函数仅返回事件的游戏对象。”

为了访问不活动的游戏对象,您需要使用支持它们的函数,例如 GameObject.GetComponentsInChildren .

public Component[] GetComponentsInChildren(Type type, bool includeInactive = false); 

关于c# - 如何访问 UI 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31019422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com