Simple TCP/IP Unity Code

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</csharp>" to "</syntaxhighlight>")
 
(3 intermediate revisions by 2 users not shown)
Line 2: Line 2:
  
 
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.
 
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.
<c>
+
 
 +
LinkSyncSCR.cs    --- Unity C# script
 +
<syntaxhighlight lang="csharp">
 
using UnityEngine;
 
using UnityEngine;
 
using System.Collections;
 
using System.Collections;
Line 52: Line 54:
 
}
 
}
 
}
 
}
</c>
+
</syntaxhighlight>

Latest revision as of 20:47, 10 January 2012

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

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{}
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox