- Home /
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!!
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
}
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!
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
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?