UNITY3D学习笔记11



using UnityEngine;
using System.Collections;

public class Test1 : MonoBehaviour {
	
	int Port = 10000;
	
	// Use this for initialization
	void Start () { }
	
	// Update is called once per frame
	void Update () { }
	
	void OnGUI(){
		
		switch(Network.peerType){
		case NetworkPeerType.Disconnected:
			StartServer();
			break;
		case NetworkPeerType.Server:
			OnServer();
			break;
		case NetworkPeerType.Client:
			
			break;
		case NetworkPeerType.Connecting:
			
			break;
		}
	}
	
	void StartServer(){
		if(GUILayout.Button("create local server")){
			NetworkConnectionError error = Network.InitializeServer(10,Port,false);
			
			Debug.Log("state:"+error);
		}
	}
	
	void OnServer(){
		GUILayout.Label("create finish wait ro connection:");
		
		int length = Network.connections.Length;
		
		for(int i = 0;i<length;i++){
			GUILayout.Label("ID:"+i);
			GUILayout.Label("IP:"+Network.connections[i].ipAddress);
			GUILayout.Label("port number"+Network.connections[i].port);
		}
		
		if(GUILayout.Button("disconnection"))
		{
			Network.Disconnect();
		}
	}
}



using UnityEngine;
using System.Collections;

public class Test2 : MonoBehaviour {

	string IP = "127.0.0.1";

	int Port = 10000;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI(){
		switch(Network.peerType){
		case NetworkPeerType.Disconnected:
			StartServer();
			break;
		case NetworkPeerType.Server:
			//OnServer();
			break;
		case NetworkPeerType.Client:
			
			break;
		case NetworkPeerType.Connecting:
			
			break;
		}
	}

	void StartServer(){
		if(GUILayout.Button("connection")){
			NetworkConnectionError error = Network.Connect(IP,Port);
			Debug.Log("state:"+error);
		}
	}
}



using UnityEngine;
using System.Collections;

public class Test3 : MonoBehaviour {

	int Port = 10000;
	string Message = "";
	Vector2 scrollPosition;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI(){
		switch(Network.peerType){
		case NetworkPeerType.Disconnected:
			StartServer();
			break;
		case NetworkPeerType.Server:
			OnServer();
			break;
		case NetworkPeerType.Client:
			
			break;
		case NetworkPeerType.Connecting:
			
			break;
		}
	}

	void StartServer(){
		if(GUILayout.Button("connection")){
			NetworkConnectionError error = Network.InitializeServer(10,Port,false);
			Debug.Log("state:"+error);
		}
	}

	void OnServer(){
		GUILayout.Label("create finish wait ro connection:");
		
		int length = Network.connections.Length;
		
		for(int i = 0;i<length;i++){
			GUILayout.Label("ID:"+i);
			GUILayout.Label("IP:"+Network.connections[i].ipAddress);
			GUILayout.Label("port number"+Network.connections[i].port);
		}
		
		if(GUILayout.Button("disconnection"))
		{
			Network.Disconnect();

			Message = "";
		}

		scrollPosition = GUILayout.BeginScrollView(scrollPosition,GUILayout.Width(200),GUILayout.Height(500));

		GUILayout.Box(Message);
		GUILayout.EndScrollView();
	}

	[RPC]
	void RequestMessage(string message,NetworkMessageInfo info){
		Message += "\n" + info.sender+":"+message;
	}
}



using UnityEngine;
using System.Collections;

public class Test4 : MonoBehaviour {

	string IP = "127.0.0.1";

	int Port = 10000;

	string inputMessage = "input Message";

	string Message = "";

	Vector2 scrollPosition;

	void OnGUI(){
		switch(Network.peerType){
		case NetworkPeerType.Disconnected:
			StartServer();
			break;
		case NetworkPeerType.Server:
			//OnServer();
			break;
		case NetworkPeerType.Client:
			OnClient();
			break;
		case NetworkPeerType.Connecting:
			
			break;
		}
	}

	void StartServer(){
		if(GUILayout.Button("connection")){
			NetworkConnectionError error = Network.Connect(IP,Port);
			Debug.Log("state:"+error);
		}
	}

	void OnClient(){
		scrollPosition  = GUILayout.BeginScrollView(scrollPosition,
		                                            GUILayout.Width(200),
		                                            GUILayout.Height(500));


		GUILayout.Box(Message);
		GUILayout.BeginHorizontal();
		inputMessage = GUILayout.TextArea(inputMessage);

		if(GUILayout.Button("send Message")){
			networkView.RPC("RequestMessage",RPCMode.All,inputMessage);
		}

		GUILayout.EndHorizontal();

		if(GUILayout.Button("disconnection")){
			Network.Disconnect();
			Message= "";
		}

		GUILayout.EndScrollView();
	}

	[RPC]
	void RequestMessage(string message,NetworkMessageInfo info){
		Message +="\n"+"Sendor"+info.sender+":"+message;
	}

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}



using UnityEngine;
using System.Collections;

public class Test5 : MonoBehaviour {
	int Port = 10000;

	string Message = "";
	string MoveInfo = ""; 

	Vector2 scrollPosition;

	void OnGUI(){
		switch(Network.peerType){
		case NetworkPeerType.Disconnected:
			StartServer();
			break;
		case NetworkPeerType.Server:
			OnServer();
			break;
		case NetworkPeerType.Client:
			
			break;
		case NetworkPeerType.Connecting:
			
			break;
		}
	}

	void StartServer(){
		if(GUILayout.Button("create local server")){
			NetworkConnectionError error = Network.InitializeServer(10,Port,false);
			Debug.Log("state:"+error);
		}
	}

	void OnServer(){
		GUILayout.Label("create finish wait to connection:");
		
		int length = Network.connections.Length;
		
		for(int i = 0;i<length;i++){
			GUILayout.Label("ID:"+i);
			GUILayout.Label("IP:"+Network.connections[i].ipAddress);
			GUILayout.Label("port number:"+Network.connections[i].port);
		}
		
		if(GUILayout.Button("disconnection"))
		{
			Network.Disconnect();
			MoveInfo = "";	
			Message = "";
		}

		scrollPosition = GUILayout.BeginScrollView(scrollPosition,
		                                          GUILayout.Width(200),
		                                          GUILayout.Height(Screen.height));

		GUILayout.Box(Message);
		GUILayout.Box(MoveInfo);
		GUILayout.EndScrollView();
	}

	[RPC]
	void RequestMessage(string message,NetworkMessageInfo info){
		Message +="\n" +"sendor"+info.sender+":"+message;
	}

	[RPC]
	void RequestMove(string message,NetworkMessageInfo info){
		MoveInfo += "\n"+"Mover"+info.sender+":action"+message+"move event";
	}

	// Use this for initialization
	void Start () { }
	
	// Update is called once per frame
	void Update () { }
}



using UnityEngine;
using System.Collections;

public class Test6 : MonoBehaviour {

	string IP = "127.0.0.1";

	int Port = 10000;

	string inputMessage = "input message";

	string Message = "";

	Vector2 scrollPosition;

	float speed = 10.0f;
	float rotationSpeed = 100.0f;

	GameObject cube0 = null;
	GameObject cube1 = null;

	void OnGUI(){
		switch(Network.peerType){
		case NetworkPeerType.Disconnected:
			StartServer();
			break;
		case NetworkPeerType.Server:
			//OnServer();
			break;
		case NetworkPeerType.Client:
			OnClient();
			break;
		case NetworkPeerType.Connecting:
			
			break;
		}
	}

	void FixedUpdate(){
		if(Network.isClient){
			float translation = Input.GetAxis("Vertical");
			float rotation = Input.GetAxis("Horizontal");

			if(translation==1){
				networkView.RPC ("RequestMove",RPCMode.All,"up");
			}

			if(translation == -1){
				networkView.RPC ("RequestMove",RPCMode.All,"down");
			}

			if(rotation == 1){
				networkView.RPC ("RequestMove",RPCMode.All,"left");
			}

			if(rotation == -1){
				networkView.RPC ("RequestMove",RPCMode.All,"right");
			}
		}
	}

	void StartServer(){
		if(GUILayout.Button("connection")){
			NetworkConnectionError error = Network.Connect(IP,Port);
			Debug.Log("state:"+error);
		}
	}

	void OnClient(){
		scrollPosition = GUILayout.BeginScrollView(scrollPosition,
		                                          GUILayout.Width(200),
		                                          GUILayout.Height(500));

		GUILayout.Box(Message);
		GUILayout.BeginHorizontal();

		inputMessage = GUILayout.TextArea(inputMessage);

		if(GUILayout.Button("send")){
			networkView.RPC("RequestMessage",RPCMode.All,inputMessage);
		}

		GUILayout.EndHorizontal();

		if(GUILayout.Button("disconnection")){
			Network.Disconnect();
			Message = "";
		}

		GUILayout.EndScrollView();
	}

	[RPC]
	void RequestMessage(string message,NetworkMessageInfo info){
		Message += "\n"+"sender"+info.sender+":"+message;
	}

	[RPC]
	void RequestMove(string message,NetworkMessageInfo info){
		string sender = info.sender.ToString();

		GameObject moveObject = null;

		if(sender =="-1")
		{
			moveObject = cube0;
		}else{
			moveObject = cube1;
		}

		int vertical = 0;
		int horizontal = 0;

		if(message == "up"){
			vertical = 1;
		}
		if(message == "down"){
			vertical = -1;
		}
		if(message == "left"){
			horizontal = 1;
		}
		if(message == "right"){
			horizontal = -1;
		}

		float translation = vertical * speed * Time.deltaTime;
		float rotation = horizontal * rotationSpeed * Time.deltaTime;
		moveObject.gameObject.transform.Translate(0,0,translation);
		moveObject.gameObject.transform.Rotate(0,rotation,0);
	}



	// Use this for initialization
	void Start () {
		cube0 = GameObject.Find("Cube0");
		cube1 = GameObject.Find("Cube1");
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值