//calls a function on a script by name public void NetFunction(string objectName, string componentName, string functionName, string argument) { //tell the server to tell the other clients to do the same NetFunctionData data = new NetFunctionData(); data.objectName = objectName; data.componentName = componentName; data.functionName = functionName; data.argument = argument; socket.Emit("netFunction", JsonUtility.ToJson(data)); }
//from the server: a function is being called public void OnNetFunction(SocketIOEvent e) { NetFunctionData data = JsonUtility.FromJson<NetFunctionData>(e.data.ToString()); Component comp = GetObjectComponent(data.objectName, data.componentName); if (comp != null) { //Uses a rather obscure system called Reflection to find a function MethodInfo method = comp.GetType().GetMethod(data.functionName); if (method != null) method.Invoke(comp, new object[] {data.argument}); else print("Warning: there is no PUBLIC function named " + data.functionName + " on object " + data.objectName + " and component " + data.componentName); } }