Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by MIkhail · Nov 29, 2009 at 06:58 PM · guigameobject

How do I handle object selection and GUI response ingame?

How can I call up a GUI interface when i click an object?

I have some clues: Use on mouseover or onmousedown, or use collision triggers that make the GUI appear, I just cant make it to work.. Please help would be apreciated. Im new to this.

I really want to be able to controll when a GUI appears on my screen.

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by AngryAnt · Dec 03, 2009 at 10:14 AM

One way of doing it would be to have a selection manager class which keeps track of the currently selected object.

Each selectable object would then have to have a collider and a monobehaviour attached which on mouse down changes selection. By using MonoBehaviour.OnMouseDown, you don't need to worry about raycasting or GUI input clickthrough - that is taken care of for you out of the box.

To handle objects of a dynamic nature, we also need to make sure that if the currently selected object leaves the active scene by being disabled or destroyed, we tell the manager to deselect it.

Heres one example of a simple implementation:

using UnityEngine; using System.Collections;

public class SelectableObject : MonoBehaviour { private Rect m_SelectionWindowRect = new Rect (10.0f, 10.0f, 300.0f, 100.0f);

 public void OnMouseDown ()
 {
     SelectionManager.Select (gameObject);
 }

 public void OnDisable ()
 {
     SelectionManager.Deselect (gameObject);
 }

 public void OnGUI ()
 {
     if (SelectionManager.IsSelected (gameObject))
     {
         m_SelectionWindowRect = GUI.Window (GetInstanceID (), m_SelectionWindowRect, SelectionWindow, gameObject.name);
     }
 }

 void SelectionWindow (int id)
 {
     GUILayout.Box ("I am the selection and my name is " + gameObject.name);
     GUI.DragWindow ();
 }

}

public class SelectionManager { private static GameObject s_ActiveSelection;

 public static GameObject ActiveSelection
 {
     get
     {
         return s_ActiveSelection;
     }
     set
     {
         s_ActiveSelection = value;
     }
 }

 public static void Select (GameObject gameObject)
 {
     ActiveSelection = gameObject;
 }

 public static void Deselect (GameObject gameObject)
 {
     if (ActiveSelection == gameObject)
     {
         ActiveSelection = null;
     }
 }

 public static bool IsSelected (GameObject gameObject)
 {
     return ActiveSelection == gameObject;
 }

}

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
avatar image
3

Answer by Cyclops · Mar 18, 2010 at 06:38 PM

This may be what you were looking for, if you're still here looking. :) Take this script file, drag/drop it onto an Object (say, a Cube). When you click the Cube in-game, a Button will appear with whatever text you assign it.

Note that you would have to attach this script to every Object you wanted to be clickable. This is a single-purpose solution, where some of the other Answers were showing you how to do a more general-purpose utility GUI function.

var wasClicked: boolean = false;

function OnMouseUp() { wasClicked = true; // turns on button. } function OnGUI() { if (wasClicked) { var someText = "Box Clicked"; if (GUI.Button(new Rect(20, 50, 100, 20), someText)) { // do other stuff when button clicked. wasClicked = false; // turns off button. } } }

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
avatar image
2

Answer by Jaap Kreijkamp · Nov 29, 2009 at 10:34 PM

  • give the clickable game object a trigger collider in special layer (clickable or whatever name you want to give it). As the object probably already uses layers for other purposes a way would be to add a new empty game object to the object, add the collider to this, and set this gameobject to the clickable layer
  • make a script in camera that in update checks for mouseclicks, if clicked use mouse coordinates with Camera.main.ScreenPointToRay(Vector3(mousex, mousey, 0)) to get a ray from screen into the level.
  • use this ray with Physics.Raycast limited to only the clickable layer to determine which object is clicked. When found, send message to parent (as we defined the collider in the childnode) that it's clicked.
  • add a script to the objects that show the dialog or whatever on this message
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
avatar image
0

Answer by Will · Nov 29, 2009 at 08:40 PM

Well do you want it to happen when you press a key or click a button

for a button the basic script would be

    var window1 = false;
function OnGUI () {
if (GUI.Button (Rect(//leftRight position, //UpDown Position, //width, //length), "Your Text")){
window1 = true;
if (window1) {
//your GUI data
}

For a key press it would be like this

var window1 = false;

function OnGUI () { if (Input.GetButtonDown ("Button you want")) window1 = true;

if (window1) { //your GUI data }

That would be the basic script, you just have to modify it to your needs, hope this helps.

Comment
Add comment · Show 1 · 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
avatar image MIkhail · Nov 29, 2009 at 09:49 PM 0
Share

Thanks for the answer. Very kind of you. I love those people who care about responding. But these functions wont really satisfy my need though. What i really want is: a window to appear when I click on a game object with my left mouse button.

Just One click on a creature and then a window comes up with text.

I do not want to click on a GUI button on screen or a key to make this happen. I would really apreciate further help here. Thanks :}

Only one click on the game object.

avatar image
0

Answer by Deadcrow · Jan 05, 2012 at 05:18 AM

Pour ma part jai tester ce donc vous parlez...

Jai mis un cube transparent avec collission Ontrigger sur le cube, et je lai mis sur le personnage ( moob)

Sur le cube transparent jai mic ce code JS

var Moob1 : GameObject; var Fenetre1 : GUITexture:

Function Start(){

   Fenetre.enabled = false;
   }

Function OnMouseUp(){

  Fenetre.enabled = true;
  }


Voila :P espérons que ca sera utile ^^

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

1 Person is following this question.

avatar image

Related Questions

GUI Appear Help 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Help with sanity script loading death screen 1 Answer

Problems using NGUI for in-game options sliders 0 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