Chenxing Zheng
Chenxing Zheng

Reputation: 117

instantiate a Popup when click a button in unity

I want to show popup when press the button.

enter image description here

enter image description here

But now it looks like this

enter image description here

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.

Here is my code

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

}

Upvotes: 1

Views: 10465

Answers (2)

Dado
Dado

Reputation: 1026

I describe my way how to make popup. There is a .gif how it looks like. enter image description here

And there is link where it is described (Dacke is nickname there).

Upvotes: 0

Fattie
Fattie

Reputation: 12592

You would probably use Unity.UI, and just use a panel.

It is very easy (1) click "add canvas" (2) click "add panel".

Simply turn it on and off using

 .SetActive

from your code. Enjoy! As you can see it's this easy:

enter image description here

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

Upvotes: 3

Related Questions