Created
October 31, 2017 14:58
-
-
Save iamPedroVictor/cc26124fe6974d39b0a533c061eb1058 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
//Credits to dwilches - SerialCommUnity | |
//Source >> https://github.com/DWilches/SerialCommUnity | |
public class MessageListener : MonoBehaviour { | |
public List<SerialCommand> serialCommand = new List<SerialCommand>(); | |
public static MessageListener instance; | |
private void Awake() | |
{ | |
if (instance != null) | |
{ | |
Destroy(this.gameObject); | |
} | |
else | |
{ | |
instance = this; | |
} | |
} | |
// Invoked when a line of data is received from the serial device. | |
void OnMessageArrived(string msg) | |
{ | |
Debug.Log(String.Format("Chegou a mensagem {0} nesse objeto {1}", msg, this.gameObject.name)); | |
SerialCommand sc = serialCommand.Find(x => x.message == msg); | |
if (sc != null) { | |
sc.c_event.Invoke(); | |
} | |
} | |
// Invoked when a connect/disconnect event occurs. The parameter 'success' | |
// will be 'true' upon connection, and 'false' upon disconnection or | |
// failure to connect. | |
void OnConnectionEvent(bool success) | |
{ | |
} | |
public void SetAction(string message, Action _action) | |
{ | |
Debug.Log("Entrou no SetAction com o parametro + " + message); | |
SerialCommand command = new SerialCommand(); | |
command.message = message; | |
command.c_event += _action; | |
this.serialCommand.Add(command); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment