- Home /
Prefab a 2D UI?
Hi Unity people,
I've done some digging and am still confused....
I want to effectively prefab my Pause Screen UI across a number of scenes to ensure consistency and optimise project efficiency. Basically, when the player pauses, a semi-transparent white image should appear with three buttons (play, restart and exit) with the appropriate "on click" functions.
Is it possible to prefab the Pause Screen/menu UI? If so, how do you go about doing this?
Thanks in advance :)
I had a canvas (created automatically a la Unity) created but can delete and extract just the actual elements,
Any idea as to why its cloning in the hierarchy but not showing up in the scene/game view?
Thanks so much"!
I don't have enough visual information about your setup but be sure of:
Drag the elements to the project view to convert them in prefabs.
In a script load the instatiate the prefab as a gameobject and set the gameobject as child of your existant canvas to be able to render it correctly.
Okay.. still a bit confused on point 2. How do I set the gameobject (which I presume is the prefab) as a child of the existant canvas? I thought that once the Pause Screen canvas was set up and converetd to a prefab it could be deleted from the hierarchy, in which case, the prefab cannot be made a child of a non-existant object (if you get me).
In the inspector, I have attached a test prefab of a simple red image to test and it's not working. I think it's to do with the Vector3 positions. I'm not sure if I need them for this case (they are included in the code you kindly linked https://docs.unity3d.com/$$anonymous$$anual/InstantiatingPrefabs.html)
Thought I had posted the code - here it is (I'm using Csharp). I have commented out the dummy versions I tested with Vector3 positions - they weren't working either.
public GameObject PauseScreenPrefab;
private float prefabX = 0; //private variables used to try simulate the code linked at https://docs.unity3d.com/$$anonymous$$anual/InstantiatingPrefabs.html private float prefabY = 0;
void PausePlay()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P)) //p button as pause input
{
Debug.Log("just pressed p");
if (Time.timeScale == 1)
{
Debug.Log("game is paused");
Time.timeScale = 0;
showPaused();
}
else if (Time.timeScale == 0)
{
Debug.Log("game in play");
Time.timeScale = 1;
}
}
}
public void showPaused()
{
/*for (int y = 0; y < 1; y++)
{
for (int x = 0; x < 1; x++)
{*/
Debug.Log("about to instatiate prefab"); //DOES NOT APPEAR IN LOG
Instantiate(PauseScreenPrefab, new Vector3(prefabX, prefabY), Quaternion.identity);
Debug.Log("instatiated prefab"); //THIS APPEARS
/* }
}*/
}
This is the code you need in your method when you call the method to make appear your pause menu.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pause$$anonymous$$enu : $$anonymous$$onoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
public void ShowPause$$anonymous$$enu()
{
GameObject prefab = Instantiate(Resources.Load<GameObject>("Pause$$anonymous$$enu")); //Load the prefab
prefab.transform.SetParent(this.transform, false); //Current gameobject where the script is attached
}
}
The prefab you can do it this way: Video
Thanks - that link led to a dead link and I am almost more confused now than before.
Do I still declare a public GameObject prefab at the beginning (before start) I dont know whether you omitted that for breivity or because of the resources.load code
What's the value of resources as opposed to the standard prefab set up? I presume the item in inverted commas after needs to be the exact name of the prefab object in the resources folder
Can I do this without the REsources piece and just Instantiate(prefab.transform.SetParent(this.transform, false) ins$$anonymous$$d?
Don't get the SetParent reference in the code or its relevance... is this something to do with the canvas?
I do really appreciate your help - bear with me. I am a newcomer to Unity and ask an waful lot of questions. !
Sorry, I forgot that deleting the link deletes the video and excuse if I'm confusing you more than neccesary. I have little time using this forums to help people and also my first language is not english.
I hope with the next info the problem solves: First, you don't need to declare any public gameobject. You only create a prefab in your project.
Second, I'm using Resources folder because it's the easiest way to show you how to load a prefab into your script code you can create any of them that you want and modify them later. Of course you can do it with other folders, but as in Unity, it's kind of a standard I just recomend you to use it.
About the name of the prefab, you're correct, you reference it by the name (you can use which you want).
The main point of this is: - Create a prefab object with the contents you want. - In the scenes you want to add the prefab, just add a script in your canvas (since you have one already created). And in that script add a function that does the 2 lines in the method I wrote before.
The first line loads a gameobject so you can have a reference to it. BUT, this is the important thing, it's that if you want to add a prefab to a gameobject you have to use the method Instantiate. The second line is also important because Unity rendering order taking into account in the hierarchy so if you don make the prefab child of the canvas you won't be able to see it.
Answer by agarcialeon · Jul 18, 2017 at 12:17 PM
Of course you can, any of the gameobjects in the hierarchy can be converted into a prefab to instantiate it later when you want.
The simplest way to do this is select your menu gameobject (if it's inside a canvas select the canvas, if not just the ui elements you want to reuse) and drag them into your project view.
Then, when you want to instantiate it in another screen you can do use this in any of your scripts to add it to your hierarchy again and use it's functionality.
Hope it helps.
I'm not sure you would want to select the canvas as part of the prefab. I have always added UI prefabs, which do not include a canvas, to a canvas already existing in my scene. In my experience, this has allow for consistent usability of all my UI prefabs.
Of course canvases can be included in a prefab. I just want to note that it is a good idea to plan how you want to implement them up front.
Yeah, well I just was taking into account both cases if in your case you hadn't any canvas created previously but yes, It's a better approach to only create prefabs from UI components without the canvas.
Your answer
Follow this Question
Related Questions
Having trouble with cloning with prefabs 1 Answer
Enable UI Button from Prefab 1 Answer
Disable GameObject through Prefab? 2 Answers