2016-02-26 276 views
0

我想在按下按钮时显示弹出框。单击单个按钮时弹出一个弹出框

enter image description here

enter image description here

但现在它看起来像这样

enter image description here

我观看了关于如何使一个弹出时,按下按钮很多教程。

但他们都通过脚本弹出,而不是实例化预制件。

我不确定是否可以实例化预制件,因为我希望或它不是一个好主意。

这里是我的代码

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); 
} 

}

回答

2

你可能会使用Unity.UI,只是使用的面板。

这很容易(1)点击“添加画布”(2)点击“添加面板”。

只需打开它,并用

.SetActive 

关闭从您的代码。请享用!正如你可以看到它的这个简单:

enter image description here

尝试在与Inspector是切换编辑器打开和关闭它。在代码中它只是...

public GameObject popupPanel; 
    ... 
    popupPanel.SetActive(false); or... 
    popupPanel.SetActive(true); 
+0

你的意思是不是每次都实例化它,只保留一个在场景中,并使用SetActive来控制它? –

+0

是的,这就是他的意思。 – Savlon

+0

确保您的数据透视也在预制中设置 – LumbusterTick