- Home /
Question by
Littellittel · Jul 09, 2017 at 09:36 PM ·
uiprefab
Associate UI with gameobject
Hi,
I currently face the issue of wanting to load specific UI parts (Overlays) when I click on certain game elements, but I couldn't figure out how to correctly associate the UI prefabs with the object prefabs.
Thanks in advance.
Comment
Best Answer
Answer by Jwizard93 · Jul 09, 2017 at 10:15 PM
The object prefab can have public variables of the Type of the UI elements. Create that object prefab and then drop the UI prefabs into fields using the inspector.
Answer by Cornelis-de-Jager · Jul 09, 2017 at 10:29 PM
/* Public Variables */
public GameObject UIOverlay;
/* Private Variables */
Transform canvas;
void Start () {
canvas = GameObject.Find("Canvas").transform;
}
void CreateOverlay () {
GameObject overlay = Instantiate (Overlay, position, rotation, canvas) as GameObject; // This sets the overlays parent to the canvas
// Set the overlay layer to the UI layer
overlay.transform.layer = (1 << LayerMask.NameToLayer("UI")); // Possibly remove the .transform, not sure, test it.
}