Reputation: 117
I want to show popup when press the button.
But now it looks like this
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
Reputation: 1026
I describe my way how to make popup. There is a .gif how it looks like.
And there is link where it is described (Dacke is nickname there).
Upvotes: 0
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:
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