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 /
  • Help Room /
This question was closed Oct 15, 2015 at 10:29 AM by BramNH for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by BramNH · Oct 14, 2015 at 05:50 PM · android2d gamebuttonstouchscreenonmousedown

Touchscreen Start Button

Hello,

I have a menu for my 2d android game now, and I have added a UI button to it. Now on this button, i have this simple script attached on it:

 void OnMouseEnter()
 {
     Application.LoadLevel("Krolsch");
 }

I can touch the button, but nothing happens, it doesn't load the new scene.

I have heard that you can use a OnMouseEnter or OnMouseDown, and that should work as a touch?

Bram

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

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by freemann098 · Oct 14, 2015 at 08:37 PM

Like said above just use the OnClick event in the inspector. Here's what I do for UI.

  1. Make a new script for managing button clicks.

  2. Attach this script to some Gameobject in your scene.

  3. In the script, make a new public function for what you want your button to do...for example

  4. public void playButton() { Application.LoadLevel("Krolsch"); }.

  5. Now click on your button in the hierarchy and find the little area in the inspector that says "On Click () "

  6. Click the little plus symbol

  7. List item

  8. This will add somthing with 2 drop down menus and an input thing that says "None (Object)". Drag the game object that has your button script on it into that field that says "None (Object)".

  9. This will let you reference this object. Click on the drop-down menu that says "No Function" and it will show some options. Click on the one that is the same name as the script you made earlier and then click on the function name. This will make the button run this function.

  10. For example, my script is called ButtonManager and my function was called playButtonList item


fkf.png (66.7 kB)
jdh.png (20.5 kB)
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 BramNH · Oct 15, 2015 at 10:28 AM 0
Share

Thanks @freemann098 and @DiegoSLTS,

The public void is what I needed to know.

avatar image
0

Answer by DiegoSLTS · Oct 14, 2015 at 07:01 PM

By "UI" you mean uGUI or the legacy GUI?

In uGUI, for a UI element to react to touch events you can do various things:

  1. Use the Button "OnClick" event in the inspector, setup the function that should be called. Or...

  2. Implement the IPointerDownHandler interface in a script added to the button. This forces you to implement the OnPointerDown function.

  3. Use an EventTrigger component instead of the OnClick event of the button, and add and setup the PointerDown event in the inspector.

I'm not sure if OnMouseEnter should work for the UI, but if it should, I guess OnMouseDown would make a lot more sense.

Also, UI elements need an EventSystem game object in the scene to react to events. Make sure it's not missing in your scene.

Comment
Add comment · Show 3 · 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 BramNH · Oct 14, 2015 at 07:37 PM 0
Share

I don't really understand the IPointerDownHandler, so I am trying the others. But what do I need to add on the "OnClick" event on the button and on the EventTrigger.

I have added my button as a object on it, then I need to add a function to that, what function does it need to be? Because I can't add my On$$anonymous$$ouseDown function to it.

avatar image BramNH BramNH · Oct 14, 2015 at 07:40 PM 0
Share

And by the way, what I did:

at the Hierarchy, Create > UI > Button

I just started this so I don't really know the means of UI, uGUI or GUI

avatar image DiegoSLTS BramNH · Oct 14, 2015 at 08:15 PM 0
Share

That's uGUI.

UI means User Interface. GUI means Graphic User Interface.

UI and GUI are generic names for this, they're not related to the technology used.

uGUI means "Uinty GUI", it's the UI system implemented in Unity since version 4.6, it works by adding UI objects in a Canvas on the scene. "legacy" GUI (or OnGUI) is the old system used by Unity. Is pretty ugly and hard to use, and you had to do anything in code (inside the OnGUI method). It's still used for editor scripts, but for GUI that's part of the game is a really bad idea to keep using it.

The function you wrote doesn't show in the list because it's private. This video explains everything you need to know to correctly setup a button in uGUI: http://unity3d.com/es/learn/tutorials/modules/beginner/ui/ui-button

This should be enough, but if you need something else, this other video explains the basics about EventTriggers: http://unity3d.com/es/learn/tutorials/modules/beginner/ui/ui-events-and-event-triggers?playlist=17111

The IPointerDownHandler may seem to complicated if you just started, ignore it unless you need it. This methods gives you more information of the event, like the exact screen coordinate of the pointer.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity 2d android game How to "freeze" position on Y axis 1 Answer

Android UI push Multiple buttons Help 0 Answers

How do I transfer the control to the PC on your android? how to change the script the character animation that would work? what should be done? 1 Answer

Making buttons for touch devices 0 Answers

High memory usage 2d game 1 Answer


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