Unity教程:如何制作简易聊天室

发表于2017-03-26
评论0 4.7k浏览
很多项目都要做聊天系统,方便玩家用户之间的交流。那么在unity中如何建立制作一个简易聊天室呢,这就需要利用 Unity 中内建的 Network 来简易说明。

首先新增一个 Server.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using UnityEngine; 
using System.Collections; 
 
public class Server : MonoBehaviour
    //The number of allowed incoming connections
    public int connections = 4;
    //The port number we want to listen to
    public int port = 25000;
     
    void OnGUI()
    {
        //Network state
        switch (Network.peerType)
        {
        //Disconnected
        case NetworkPeerType.Disconnected:
            CreateServer();
            break;
        //In server
        case NetworkPeerType.Server:
            OnServer();
            break;
        }
    }
 
    //Create server button
    void CreateServer()
    {
        if (GUILayout.Button ("Create Server"))
        {
            //Initialize the server
            //Network.InitializeServer(int connections, int listenPort, bool useNat);
            Network.InitializeServer( connections, port, false );
        }
    }
 
    //Wait for client and display the client information
    void OnServer()
    {
        GUILayout.Label ( "The Server is startting now, waiting for client connecting." );
        int length = Network.connections.Length;
 
        for (int cnt = 0; cnt < length; cnt++)
        {
            GUILayout.Label( "-------------------------" );
            GUILayout.Label( "Client " + cnt );
            GUILayout.Label( "Client IP : " + Network.connections[cnt].ipAddress );
            GUILayout.Label( "Client Port : " + Network.connections[cnt].port );
        }
    }
}
Server.cs 脚本主要是用来初始化伺服器
这裡所使用的方法都是 Unity API
这个脚本是用来初始化伺服器提供客户端连线

接下来新增 Client.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using UnityEngine; 
using System.Collections; 
  
public class Client : MonoBehaviour
{
    public string IP = "127.0.0.1";
  
    //The port number we want to listen to
    public int port = 25000;
  
    //The total message
    private string message;
    //The send message
    private string sendMessage = "";
  
    //The client name
    private string name = "";
  
    void OnGUI()
    {  
        switch (Network.peerType)
        {  
        case NetworkPeerType.Disconnected:
            StartConnect();
            break;
        case NetworkPeerType.Client:
            OnClient();
            break
        }
    }
  
    void StartConnect()
    {
        if (GUILayout.Button ("Connect to server"))
        {      
            Network.Connect( IP, port );
        }
    }
  
    void OnClient()
    {
        name = GUILayout.TextField( name, 10 );
        sendMessage = GUILayout.TextField(sendMessage, 25);
          
        if (GUILayout.Button ("Send Message"))
        {
            //Network send function
            //networkView.RPC(string name, RPCMode mode, params object[] args);
            GetComponent().RPC( "ReceiveMessage", RPCMode.All, name, sendMessage );
            sendMessage = "";
        }
          
        GUILayout.Label ( "Message : " );
        GUILayout.Label ( message );
    }
  
    //Network receive function
    [RPC]
    void ReceiveMessage(string n, string msg, NetworkMessageInfo info)
    {  
        message += n + " : " + msg + "n";
    }
  
}

用来传递讯息至其他客户端

并且接受从其他客户端传来的讯息


接下来创建两个新场景并分别命名为 Server 及 Client

分别将两个脚本赋予场景中的 Main Camera

并新增 Component → Miscellaneous → Network View 至 Camera 上

 

接著分别输出两个场景档

 

产生出 Server 及 Client 执行档后

分别执行一次 Server、两次 Client

 

按下 Create Server 按钮后

至 Client 端按下 Connect to server 按钮

 

若伺服端接收成功後
即可開始進行聊天功能測試

測試結果

此範例所用到的 Unity API 彙整

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引