Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Xeong-Hu · Oct 12, 2015 at 12:07 AM · uiclickmousepositionmouseclickpop-up

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xeong-Hu · Oct 12, 2015 at 04:25 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Only want to detect button click event 1 Answer

How do I get mouse position in game units? 3 Answers

How do I make a UI button to click a key? 1 Answer

UI Button: one action after other with only one click 1 Answer

Raycast against UI in world space 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges