在 Unity 中实例化预制件
Instantiate prefab in Unity
我正在统一实例化一个预制面板,并将它设置为另一个面板的父面板,如下所示:
GameObject notificationPanel = (GameObject) Resources.Load("NotificationWindow");
Text notificationText = notificationPanel.GetComponentInChildren<Text>();
if (notificationType == NotificationType.Warning)
{
notificationText.color = Color.red;
}
notificationText.text = text;
GameObject newNotificationWindow =
(GameObject) Instantiate(notificationPanel, new Vector3(0, 0, 0), Quaternion.identity);
newNotificationWindow.transform.SetParent(Settings.NotificationHolder.transform);
然而,当实例化时它的大小异常,父面板有一个布局组,其中单元格的大小固定,为什么这不影响它?新面板比我的屏幕大 10 倍左右。在层次结构视图中,新面板在其父面板下正确显示为子面板。另外 'z' 位置在 - 3900 为什么?
因此,在阅读完 Unity Documentation: Instantiating the UI element 之后,您所要做的就是致电:
newNotificationWindow.transform.SetParent(Settings.NotificationHolder.transform, false);
"false"表示 worldPositionStays 参数,它缩放 UI。
让我知道这是否适合你。
我正在统一实例化一个预制面板,并将它设置为另一个面板的父面板,如下所示:
GameObject notificationPanel = (GameObject) Resources.Load("NotificationWindow");
Text notificationText = notificationPanel.GetComponentInChildren<Text>();
if (notificationType == NotificationType.Warning)
{
notificationText.color = Color.red;
}
notificationText.text = text;
GameObject newNotificationWindow =
(GameObject) Instantiate(notificationPanel, new Vector3(0, 0, 0), Quaternion.identity);
newNotificationWindow.transform.SetParent(Settings.NotificationHolder.transform);
然而,当实例化时它的大小异常,父面板有一个布局组,其中单元格的大小固定,为什么这不影响它?新面板比我的屏幕大 10 倍左右。在层次结构视图中,新面板在其父面板下正确显示为子面板。另外 'z' 位置在 - 3900 为什么?
因此,在阅读完 Unity Documentation: Instantiating the UI element 之后,您所要做的就是致电:
newNotificationWindow.transform.SetParent(Settings.NotificationHolder.transform, false);
"false"表示 worldPositionStays 参数,它缩放 UI。
让我知道这是否适合你。