http://blog.csdn.net/yuanlin2008/article/details/7834864文章:Unity的网络功能,NetworkView实用介绍
http://www.ceeger.com/Components/class-NetworkView.htmlUnity宝典对NetworkView的介绍
http://www.ceeger.com/Components/net-MasterServer.html MasterServer 官方翻译文档。
unity5.x开发指南:222页 网络实例
游戏管理:
using UnityEngine;
using System.Collections;
public enum BallGameState
{
lobby,//大厅
idle,//等待
gaming,//游戏中
lose //失败
}
public class Manager : MonoBehaviour {
public GameObject prefab_ball;
private BallGameState state = BallGameState.lobby;
private string IP = "127.0.0.1";
private int Port = 1000;
private Rigidbody myBall;
// Use this for initialization
void Start () {
}
void OnGUI()
{
switch(state)
{
case BallGameState.lobby:
OnLobby();
break;
case BallGameState.idle:
OnIdle();
break;
case BallGameState.gaming:
OnGaming();
break;
case BallGameState.lose:
OnLose();
break;
}
}
void OnLobby()
{
if(GUILayout.Button("建立服务器"))
{
NetworkConnectionError error = Network.InitializeServer(100, Port, false);
if(error==NetworkConnectionError.NoError)
{
state = BallGameState.idle;
}
}
GUILayout.Label("================================");
GUILayout.BeginHorizontal();
GUILayout.Label("服务器IP地址:");
IP = GUILayout.TextField(IP);
GUILayout.EndHorizontal();
if(GUILayout.Button("连接服务器"))
{
NetworkConnectionError error = Network.Connect(IP, Port);
if(error==NetworkConnectionError.NoError)
{
state = BallGameState.idle;
}
}
}
void OnIdle()
{
if(GUILayout.Button("Start"))
{
StartGaming();
}
}
//游戏逻辑状态
void OnGaming()
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
myBall.AddForce(new Vector3(x, y*3, 0));
//出界则失败
if(myBall.transform.position.y<-5)
{
Network.Destroy(myBall.GetComponent<NetworkView>().viewID);
state = BallGameState.lose;
}
}
void OnLose()
{
GUILayout.Label("You Lose");
if(GUILayout.Button("Start Again!"))
{
StartGaming();
}
}
//开始游戏
void StartGaming()
{
state = BallGameState.gaming;
GameObject obj = Network.Instantiate(prefab_ball, new Vector3(0, 1, 0), Quaternion.identity, 0) as GameObject;
myBall = obj.GetComponent<Rigidbody>();
//obj.GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value);
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
public class SphereSerializer2 : MonoBehaviour {
private Rigidbody rigidbody;
private Renderer renderer;
private NetworkView networkView;
bool firstSerialize = true;
void Awake()
{
rigidbody = GetComponent<Rigidbody>();
renderer = GetComponent<Renderer>();
networkView = GetComponent<NetworkView>();
networkView.observed = this;//不再是同步刚体,而是脚本组件
}
void OnSerializeNetworkView(BitStream stream,NetworkMessageInfo info)
{
if(firstSerialize)
{
firstSerialize = false;
Vector3 color = Vector3.zero;
if(stream.isWriting)//发送数据
{
if (networkView.isMine)//如果isMine是true,表示此游戏对象是本机创建的
color = new Vector3(1, 0, 0);
else
color = new Vector3(0, 1, 0);
stream.Serialize(ref color);
}
else//读取数据
{
stream.Serialize(ref color);
}
renderer.material.color = new Color(color.x, color.y, color.z);
}
Vector3 pos = Vector3.zero;
Quaternion rotation = Quaternion.identity;
Vector3 velocity = Vector3.zero;
if(stream.isWriting)
{
pos = rigidbody.position;
stream.Serialize(ref pos);
rotation = rigidbody.rotation;
stream.Serialize(ref rotation);
velocity = rigidbody.velocity;
stream.Serialize(ref velocity);
}
else
{
stream.Serialize(ref pos);
transform.position = pos;
stream.Serialize(ref rotation);
transform.rotation = rotation;
stream.Serialize(ref velocity);
rigidbody.velocity = velocity;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
MasterServer实例:224页
单独运行:Server
using UnityEngine;
using System.Collections;
public class MyServer : MonoBehaviour {
public string gameName = "CrazyBalls";
// Use this for initialization
void Start () {
MasterServer.ipAddress = "127.0.0.1";
//指定MasterServer的端口号
MasterServer.port = 23466;
//初始化服务器
Network.InitializeServer(35, 25003, false);//!Network.HavePublicAddress()最后一个参数这样应该更好
//在MasterServer上注册本服务器,命名为“小菜鸟的房间”,介绍为一起玩吧
MasterServer.RegisterHost(gameName,"小菜鸟的房间", "来一起玩吧");//创建这个类型的主机服务器(房间)
}
// Update is called once per frame
}
Client:单独运行
using UnityEngine;
using System.Collections;
public class Client : MonoBehaviour {
// Use this for initialization
void Start () {
//指定MasterServer的IP地址
MasterServer.ipAddress = "127.0.0.1";
//指定MasterServer的端口号
MasterServer.port = 23466;
//请求CrazyBalls游戏
MasterServer.RequestHostList("CrazyBalls");//请求这个游戏类型的主机
}
void OnGUI()
{
//得到所有服务器
HostData[] data = MasterServer.PollHostList();//获取请求的房间列表(服务器)
//显示服务器列表,并可加入
foreach(HostData hostData in data)
{
GUILayout.BeginHorizontal();
string text = hostData.gameName + " 房间人数:" + hostData.connectedPlayers + "/" + hostData.playerLimit;//最大人数
GUILayout.Label(text);
string hostInfo = "";
foreach(string hostIp in hostData.ip)//应该只有一个ip
{
hostInfo += hostIp + " : ";
}
hostInfo += hostData.port;//:+端口
if(GUILayout.Button("加入"+" "+hostInfo))
{
Network.Connect(hostData);
}
GUILayout.EndHorizontal();
}
}
}