- Home /
How to make UI component appear at selected UI's position?
Alright i'll explain deeper here.
You know how when you have 2d video game character and press a button next to the character or thing you want to talk with. Yes, then after that, you get a few options. Well that's what i'm trying to do. But I don't know how to instantiate the optional UI choices near the object's position.
I've asked this question before but it was back in the days when we had GUI Functions and not UI.
If anyone have anymore ideas please let me know. I'm running short.
Nvm, dudes I found out one step of doing it. But now I gotta do the others.
anyways here's my current code. When I get this thing running successful i'll post the full code as answered.
using UnityEngine.EventSystems;
public void OnGUI(){
EventSystem EventSP = EventSystem.current;
Event e = Event.current;
if(e.clickCount == 1){
Debug.Log(EventSP.currentSelectedGameObject.name);
//Debug.Log("Here is " + e.mousePosition + e.button + e.type);
}
}
Answer by Xeong-Hu · Oct 15, 2015 at 11:21 AM
Welp this is the best I came up with. Maybe its not the right way but it works for me real good. Forgot to post this 3 days after finishing.
using UnityEngine;
using System.Collections;
//You'll need these 2 systems for it to work
//Essential for List function
using System.Collections.Generic;
//Essential for the EventSystem in your scene
using UnityEngine.EventSystems;
public class Choices : MonoBehaviour {
//Your list of UI Selections that'll appear when you select the wanted object
public List<GameObject> Options;
public Transform Position;
public GameObject CanvasParent;
public float Order;
public GameObject Cloned;
public GameObject Self;
public EventSystem FindEvent;
// Use this for initialization
void Start () {
}
// Update is called once per frame
public void Update () {
//If the Object you selected was an instatiated prefab then you'll need it to
//find your EventSystem within your canvas for it to detect the OnClickFunctions and other functions including your mouse
FindEvent = GameObject.Find ("EventSystem").GetComponent<EventSystem> ();
CanvasParent = GameObject.Find ("Canvas").gameObject;
}
public void OnGUI(){
}
public void OnPointerClick(EventSystem Eventer){
//Your prefabed EventSystem will be replaced with your Scene EventSystem
Eventer = FindEvent;
Debug.Log (Eventer.currentSelectedGameObject.name);
Position = Eventer.currentSelectedGameObject.gameObject.transform;
//This Function will repeat until int i is less than your Maxed Options in your array list,(Options.Count).
for(int i = 1; i < Options.Count; i++){
//This determines the order of your options,
//so like Pizza = array number 0, Peanutbutter = array number 2, all of that stuff.
//Mines start at 1 to avoid complications for myself but it can began at 0.
Order = Options.IndexOf(Options[i]);
//Now you'll instantiate your first Option in your list as a Clone from your Prefab GameObject.
Cloned = (GameObject)Instantiate(Options[i],Position.position,Position.rotation);
//Then your Options will be a child of your Selected GameObject that generated the UI's(Your Options).
Cloned.transform.SetParent(Position);
//After that You can set the position of your Options.
//It's better this way so the pop up selections won't cloud your view.
//You'll also need Order to + your position.y so your Options won't stack on top of each other.
//Since Order is the number of your Options[i] which is your IndexNumber
//(Number given by Object in your Array)Each time your For loop function repeats your Order Number changes.
Cloned.transform.position = new Vector3 (Position.position.x+1f,Position.position.y + Order+0.5f,Position.position.z);
//Then Lastly Your Cloned Object will be a child of your overall Canvas.
//This was needed because i had a UI with a masked viewport. So my UI's became Invisible, and unclickable.
Cloned.transform.SetParent(CanvasParent.transform);
}
}
//Then ofcourse after you select your Selection you'll want them to vanish.
//This is the best way i could think of, but i messed up because, this does noes not destroy multiple GameObjects.
public void OnPointerUp(EventSystem Eventer){
Destroy (GameObject.FindGameObjectWithTag("Selective"),0);
}
public void OnPointerExit(EventSystem Eventer){
Destroy (GameObject.FindGameObjectWithTag("Selective"),0);
}
}
It is a bit of a long function but it's the best I could come up with. So I just wanted to post a working function in here for anyone else that might have this question. Don't worry it's worth it.