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 AnnaB417 · Jan 29, 2016 at 03:46 PM · c#uibuttoneventudp

How to make Buttons communicate with UDP

Hi all! I'll start with: I am new to Unity3D, am using 5.1.3, and I've been going through UI tutorials all week. I want to create buttons that will do three things:
1. change color when pressed, (I've got this part down by using color tint or could use swap Sprite for the color changes). 2. Want buttons to stay in pressed state (function 0 off or 1 o) until pressed a second time.
3. Want the buttons to send their states to another computer communicating with UDP. I've got the UDP Code in C#, can confirm it is sending and receiving, but do not know how to link the buttons to the UDP, and which function to use to make the buttons talk to the other computer.

My question: Can I do this through the On Click () section in the Inspector for each button? If so, which function should I use?

Thank you in advance for any help you can provide!!

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

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

Answer by DroidifyDevs · Jan 30, 2016 at 03:31 PM

To make a button do something, my favorite way is to use a snippet of code that works when you press the button. For example, here is a script I made (and works in my project):

 using UnityEngine;
 using System.Collections;
 
 public class PlanetoMapSelect : MonoBehaviour
 { 
     void Awake()
     {
         //keeps GameObject alive thru all the scenes
         DontDestroyOnLoad(transform.gameObject);
     }
 
     void Start()
         //This script selects the plane in the garage, but does NOT load it in the map.
         //To load the plane in map, use PlaneLoadOnMap.cs
     {
         //resets the plane values so you can select another plane after playing
         PlayerPrefs.SetInt("PlaneOne", 0);
         PlayerPrefs.SetInt("PlaneTwo", 0);
     }
 
     public void SelectPlaneOne()
     {
         //button actions
         PlayerPrefs.SetInt("PlaneOne", 1);
         PlayerPrefs.SetInt("PlaneTwo", 0);
         Application.LoadLevel(2);
         Debug.Log("Plane1 button pressed, loading scene 2...");
     }
 
     public void SelectPlaneTwo()
     {
         //button actions
         PlayerPrefs.SetInt("PlaneTwo", 1);
         PlayerPrefs.SetInt("PlaneOne", 0);
         Debug.Log("Plane2 button pressed, loading scene 2...");
         Application.LoadLevel(2);
     }
 }

So I created an empty game object and dragged that script onto the empty object. Then I created a new button. Then I selected the button, and dragged the empty object I created into the On click () slot. Then I can select what to do. SO one button does SelectPlaneOne() and another does SelectPlaneTwo().

This is perhaps the best tutorial on how to make buttons do something. So perhaps instead of changing scenes in the video or selecting airplanes in my script, you can put in your code instead.

So YES, you can do this in the OnClick (), and simply create

 public void yournamehere()
 {
 // do your stuff here
 }


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 AnnaB417 · Mar 15, 2016 at 07:15 PM 0
Share

Thanks so much! This was helpful! I ended up just using toggles and tweaking them to look like buttons, but I'm still quite new to scripting and Unity3D. I am finally to the level where I understand your code, and it is indeed a good solution!

avatar image
1

Answer by hexagonius · Jan 30, 2016 at 07:55 AM

The first works because of the buttons build in transitions, right?
for the second to stay active you could adjust the tint colors of both states, highlighted and normal and toggle it.
the third goes for all buttons, right? so what you can do is adding a script to the parent of those buttons, add public methods to it, as many as you need different calls for your buttons. Assign the gameobject to each buttons onClick and choose the corresponding method.
within those methods you can call your Rpc stuff

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 AnnaB417 · Feb 04, 2016 at 02:54 PM 0
Share

Yes, that's correct the first works because of the Transitions (I'm using colors ins$$anonymous$$d of sprite swap, but may change that). for item 2, I feel that I need to add a boolean return or somehow make the buttons act like toggles, which I can't seem to find documentation on for the new Unity3D GUI. I'm building the UI$$anonymous$$anager script now, to assign a public methods to buttons, and then if I'm understanding correctly, I'll just call back that method through OnClick() for each particular button. Is that right?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding single Event listener to multiple ui buttons via scripting 1 Answer

scrollview buttons are overlapping 0 Answers

Event system or UI Button ? 0 Answers

Script Two Button Functions At Once 1 Answer

Transferring Windows Forms buttons and labels to Unity UI (Beginner Level) 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