如何从该GameOject的组件中获取原始GameObject

问题描述 投票:0回答:2

我知道这看起来很复杂,但我有一个协程,它将 GameObject 的 RectTransform 作为参数,并使用此 RectTransform 执行操作。我希望它也将原始游戏对象添加到游戏对象列表中。我在网上查看是否有办法获取组件的所有者,但没有成功。我真的很想将参数保留为 RectTransform ,以免重写整个脚本。

这是我的代码片段,以便您可以了解我的意思:

private void ShowGraph(RectTransform activeGraphContainer) {
    graphs.Clear();
    graphs.Add(activeGraphContainer.GetComponent<GameObject>()); //<-- My attempt to get the orignal GameObject (doesn't work)

    //LABEL Y-AXIS -->
    float graphHeight = activeGraphContainer.sizeDelta.y;
    float graphWidth = activeGraphContainer.sizeDelta.x; //<-- example of things the coroutine is doing to the RectTransform
c# unityscript
2个回答
0
投票

GameObject
不是组件;它包含对象的组件。每个组件都有一个
gameObject
属性来获取它的游戏对象。这就是您正在寻找的房产


0
投票

这是你想做的吗?

GameObject sample;

void Start()
{
    RectTransform rectTransform = this.gameObject.transform.GetComponent<RectTransform>();
    sample = rectTransform.gameObject;
}

void Update()
{
    float graphHeight = sample.transform.GetComponent<RectTransform>().sizeDelta.y;
    float graphWidth = sample.transform.GetComponent<RectTransform>().sizeDelta.x;
}
© www.soinside.com 2019 - 2024. All rights reserved.