Skip to content

Instantly share code, notes, and snippets.

@iamPedroVictor
Created October 31, 2017 14:58
Show Gist options
  • Save iamPedroVictor/cc26124fe6974d39b0a533c061eb1058 to your computer and use it in GitHub Desktop.
Save iamPedroVictor/cc26124fe6974d39b0a533c061eb1058 to your computer and use it in GitHub Desktop.
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