Simple TCP/IP Unity Code

From Unify Community Wiki
Revision as of 16:07, 15 June 2007 by Zumwalt (Talk | contribs)

Jump to: navigation, search

After you have the DLL in your plugins folder, create a c# script file and place this code in that file then attach it to either an empty game object or the main camera.

LinkSyncSCR.cs --- Unity C# script <csharp> using UnityEngine; using System.Collections; using System.Runtime.InteropServices; using SharpConnect; using System.Security.Permissions;

public class LinkSyncSCR : MonoBehaviour { public Connector test=new Connector(); string lastMessage; public Transform PlayerCoord;

void Start () { Debug.Log(test.fnConnectResult("Your server IP or URL without HTTP://",10000,System.Environment.MachineName)); if (test.res !="") { Debug.Log(test.res); }

} void Update () { if (Input.GetKeyDown ("space")) { Debug.Log ("space key was pressed"); test.fnPacketTest("space key was pressed"); }

if (Input.GetKeyDown ("escape")) { Debug.Log ("escape key was pressed"); test.fnPacketTest("escape key was pressed"); } if (test.strMessage !="JOIN") { if (test.res != lastMessage) { Debug.Log(test.res); lastMessage=test.res; } } test.fnPacketTest(PlayerCoord.position[0]+","+PlayerCoord.position[1]+","+PlayerCoord.position[2]); }

void OnApplicationQuit (){ try{test.fnDisconnect();} catch{} } } </csharp>

Personal tools