单击一个按钮时实例化弹出窗口 [英] instantiate a Popup when click a button in unity
问题描述
我想在按下按钮时显示弹出窗口.
I want to show popup when press the button.
但是现在看起来像这样
我已经看过许多关于如何在按下按钮时弹出窗口的教程.
I have viewed many tutorials about how to make a popup when press the button.
但是它们全都是通过脚本弹出的,而不是实例化预制件.
But they all make the popup by script,not instantiate the prefab.
我不确定是否可以按照我的意愿实例化预制件,否则这不是一个好主意.
I'm not sure if I can instantiate the prefab as I hope or it's not a good idea.
这是我的代码
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Purchase : MonoBehaviour,IPointerClickHandler
{
public GameObject purchasePanel;
public GameObject panelPosition;
public void OnPointerClick (PointerEventData eventData)
{
GameObject instantiatedPurchase = Instantiate(purchasePanel, panelPosition.transform.position,panelPosition.transform.rotation) as GameObject;
instantiatedPurchase.transform.SetParent(panelPosition.transform);
}
}
推荐答案
您可能会使用Unity.UI,而仅使用面板.
You would probably use Unity.UI, and just use a panel.
这非常容易(1)单击添加画布"(2)单击添加面板".
It is very easy (1) click "add canvas" (2) click "add panel".
只需
.SetActive
来自您的代码.享受!如您所见,这很简单:
from your code. Enjoy! As you can see it's this easy:
尝试在编辑器中打开和关闭它,并在Inspector
上进行切换.在代码中只是..
Try turning it on and off in the Editor with that toggle on Inspector
. In code it's just..
public GameObject popupPanel;
...
popupPanel.SetActive(false); or...
popupPanel.SetActive(true);
这篇关于单击一个按钮时实例化弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!