Quantcast
Channel: Answers by "dubbreak"
Viewing all articles
Browse latest Browse all 68

Answer by dubbreak

$
0
0
This might be of help? For my buttons (I use ngui) I have this script added on to them: using UnityEngine; using System.Collections; using System; public class EventButton : MonoBehaviour { public event Action Clicked; public bool buttonEnabled { get; set; } void Start() { buttonEnabled = true; } void OnClick() { if (Clicked != null && buttonEnabled) Clicked(); } } OnClick gets called by ngui (since it's attached to an ngui button) by the normal unity messaging. The gist is OnClick gets called when it is clicked which then calls my own event. Since you're not using ngui you use your touch logic to determine it has been clicked. I'm using the built in Action delegate since I'm not sending info with it, could be a different custom delegate or public delegate void ClickDelegate; public event ClickDelegate Clicked; Action is just easier and cleaner if you don't have any arguments. I then reference my buttons from my main script: public class GameManager : MonoBehaviour { public EventButton playButton; public EventButton leaveButton; void Start() { playButton.Clicked += HandlePlay; } private void HandlePlay() { DoPlayStuff(true); } } That's simplified a bit since I don't subscribe to the event until network stuff happens and some buttons get subscribed and unsubcribed depending on state.. but it gives you and idea of how I'm using events to deal with buttons. In case anyone that's using ngui reads this, I think the latest ngui version has events you can subscribe to now (rather than just the unity messaging). So you shouldn't have to roll your own like this.

Viewing all articles
Browse latest Browse all 68

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>