how to find the real type of parent of a unity object uibutton I tried using uibuttonparentgameobject but it is always show its base type gameobject
To find the real type of the parent of a Unity UIButton object, you can use the GetComponentInParent method provided by Unity. Here's an example:
using UnityEngine;
using UnityEngine.UI;
public class ExampleScript : MonoBehaviour
{
private void Start()
{
// Assuming the UIButton is attached to the current GameObject
UIButton uiButton = GetComponent<UIButton>();
// Get the real type of the parent GameObject
GameObject parent = uiButton.transform.parent.gameObject;
Button realParent = parent.GetComponentInParent<Button>();
// Check if the parent has the Button component
if (realParent != null)
{
Debug.Log("The real parent is a Button.");
}
else
{
Debug.Log("The real parent is not a Button.");
}
}
}
In this example, we first get the parent GameObject of the UIButton using the transform.parent.gameObject property. Then, we use GetComponentInParent<Button>() to check if the parent GameObject has the Button component attached to it. If it does, we know that the real parent is a Button. Otherwise, the real parent is not a Button
原文地址: http://www.cveoy.top/t/topic/hUaV 著作权归作者所有。请勿转载和采集!