How to send strings from an Peer-To-Peer server to a client using Unity C#? -
i´m programming game unity engine using c#. want users function servers or clients , want server able send messages(strings) clients. i´ve tried using functions, of course of study didn´t realy work since if have 2 seperate identities. right i´m trying utilize rcp doesn´t seem work in tutorial....i compiler error in update function. i´ve attached network view component.
using unityengine; using system.collections; public class networkmanager : monobehaviour { private int maxconnections = 12; private int serverport = 9955; public string message = "nomessageselected"; public bool sendmessage = false; public void ongui() { //creates 2 buttons on gui, if disconnected if (network.peertype == networkpeertype.disconnected) { if (gui.button (new rect (100, 100, 100, 30), "server")) { network.initializeserver (maxconnections, serverport); } if (gui.button (new rect (200, 100, 100, 30), "client")) { network.connect ("localhost", serverport); } } ////shows client or server gui //client if (network.peertype == networkpeertype.client) { gui.label (new rect (100, 100, 400, 30), "connected server"); //disconnect if (gui.button (new rect (200, 200, 100, 30), "disconnect")) { network.disconnect(); masterserver.unregisterhost(); } } //server if (network.peertype == networkpeertype.server) { gui.label (new rect (100, 100, 400, 30), "clients connected: " + network.connections.length); message = gui.textfield(new rect(150, 150, 200, 20), message, 25);//how receive "message"? //sends message via rpc if (gui.button (new rect (200, 200, 100, 30), "send message")) { networkview.rpc ("printtext", rpcmode.all, "hello world"); } } } public void update() { debug.log (message); } @rpc function printtext (text : string) { debug.log(text); } }
you've copied js functon declaration c# script. can see examples in 3 languages here - http://docs.unity3d.com/scriptreference/networkview.rpc.html
c# equivalent:
[rpc] public void printtext (string text) { debug.log(text); } c#
No comments:
Post a Comment