找回密码
 立即注册
查看: 6183|回复: 75

[网络] Unity3D中RPC函数的代码解析

[复制链接]
发表于 2012-6-15 12:46 | 显示全部楼层 |阅读模式
  1. using UnityEngine;
  2. using System.Collections;

  3. public class Chat : MonoBehaviour {

  4.         bool usingChat = false;
  5.     bool showChat = false;

  6.     string inputField = "";

  7.     Vector2 scrollposition;
  8.     int width = 500;
  9.     int height = 200;
  10.     string playerName;
  11.     float lastUnfocusTime = 0;
  12.     Rect window;

  13.     ArrayList playerList = new ArrayList();
  14.     class PlayerNode
  15.     {
  16.         public string playerName;
  17.         public NetworkPlayer player;//NetworkPlayer是一个数据结构,保存着你可以从网络定位的另一位玩家的信息。比如,基于NetworkPlayer你可以向另外一个玩家发送消息。


  18.     }
  19.     ArrayList chatEntries=new ArrayList();
  20.     class ChatEntry
  21.     {
  22.         public string name="";
  23.         public string text="";
  24.     }
  25.         // Use this for initialization
  26.         void Start () {
  27.         window = new Rect(Screen.width / 2-width/2,Screen.height-height+5,width,height);
  28.         }
  29.     void OnConnectedToServer()
  30.     {
  31.                 playerName = PlayerPrefs.GetString("playerName","");
  32.         if(playerName=="")
  33.         {
  34.             playerName = "RandomName"+Random.Range(1,999);
  35.         }
  36.         ShowChatWindow();
  37.         networkView.RPC("TellServerOurName",RPCMode.Server,playerName);//在所有连接端调用一个RPC函数。
  38.         addGameChatMessage(playerName+" hase just joined the chat!");
  39.     }
  40.    
  41.     void OnServerInitialized()
  42.     {
  43.                 playerName = PlayerPrefs.GetString("playerName","");
  44.         if(playerName=="")
  45.         {
  46.             playerName = "RandomName"+Random.Range(1,999);
  47.         }
  48.         ShowChatWindow();
  49.         PlayerNode newEntry =new  PlayerNode();
  50.         newEntry.playerName = playerName;
  51.         newEntry.player = Network.player;
  52.         playerList.Add(newEntry);
  53.         addGameChatMessage(playerName+" hase just joined the chat!");
  54.     }
  55.     PlayerNode GetPlayerNode(NetworkPlayer netPlay)
  56.     {
  57.         foreach(PlayerNode entry in playerList)
  58.         {
  59.             if(entry.player==netPlay)
  60.             {
  61.                 return entry;
  62.             }
  63.            
  64.         }
  65.         Debug.LogError("GetPlayNode:Requested a playernode of non-existing player!");
  66.         return null;
  67.     }
  68.     void OnPlayerDisconnected(NetworkPlayer netPlayer)//当一个玩家从服务器上断开时在服务器端调用。
  69.     {
  70.         addGameChatMessage("A Player has discinnected");
  71.         playerList.Remove(GetPlayerNode(netPlayer));
  72.     }
  73.     void OnDisconnectedFromServer()
  74.     {
  75.         CloseChatWindow();
  76.     }
  77.    

  78.     [RPC]
  79.     void TellServerOurName(string name,NetworkMessageInfo info)//NetworkMessageInfo 网络数据信息,刚从网络接收的数据的相关信息会被保存到这个结构中。它揭示了从哪里来(数据源),什么时间发送和什么网络视图发送;其中包括:数据源、发送时间、网络视图。http://3d.ceeger.com/Script/NetworkMessageInfo/NetworkMessageInfo.html

  80.     {
  81.         PlayerNode newEntry = new PlayerNode();
  82.         newEntry.playerName = playerName;
  83.         newEntry.player = Network.player;
  84.         playerList.Add(newEntry);
  85.         addGameChatMessage(playerName+" has just joined the chat!");
  86.     }
  87.         void CloseChatWindow()
  88.     {
  89.         showChat = false;
  90.         inputField = "";
  91.         chatEntries = new ArrayList();
  92.     }
  93.         void ShowChatWindow()
  94.     {
  95.         showChat = true;
  96.         inputField = "";
  97.         chatEntries = new ArrayList();
  98.     }
  99.         
  100.         
  101.         void OnGUI () {
  102.         if (!showChat) return;
  103.         if(Event.current.type==EventType.keyDown && Event.current.character=='\n' & inputField.Length<=0)
  104.         {
  105.             if(lastUnfocusTime + .25f < Time.time)
  106.             {
  107.                 usingChat = true;
  108.                 GUI.FocusWindow(5);
  109.                 GUI.FocusControl("Chat input field");
  110.             }
  111.         }
  112.         window = GUI.Window(5,window,GlobalChatWindow,"");
  113.         }

  114.     void GlobalChatWindow(int id)
  115.     {
  116.         GUILayout.BeginVertical();
  117.         GUILayout.Space(10);
  118.         GUILayout.EndVertical();

  119.         scrollposition = GUILayout.BeginScrollView(scrollposition);

  120.         foreach(ChatEntry entry in chatEntries)
  121.         {
  122.             GUILayout.BeginHorizontal();
  123.             if (entry.name == " - ")
  124.             {
  125.                 GUILayout.Label(entry.name + entry.text);
  126.             }
  127.             else
  128.             {
  129.                 GUILayout.Label(entry.name+": "+entry.text);
  130.             }
  131.             GUILayout.EndHorizontal();
  132.             GUILayout.Space(2);
  133.         }

  134.         GUILayout.EndScrollView();
  135.         if(Event.current.type==EventType.keyDown && Event.current.character=='\n' & inputField.Length>0)
  136.         {
  137.             HitEnter(inputField);
  138.         }
  139.         GUI.SetNextControlName("Chat input field");
  140.         inputField = GUILayout.TextField(inputField);

  141.         if(Input.GetKeyDown("mouse 0"))
  142.         {
  143.             if(usingChat)
  144.             {
  145.                 usingChat = false;
  146.                 GUI.UnfocusWindow();
  147.                 lastUnfocusTime = Time.time;
  148.             }
  149.         }
  150.     }
  151.         void HitEnter(string msg)
  152.         {
  153.             msg = msg.Replace('\n',' ');
  154.             networkView.RPC("ApplyGlobalChatText",RPCMode.All,playerName,msg);
  155.         }
  156.    [RPC]
  157.     void ApplyGlobalChatText(string name,string msg)
  158.    {
  159.         ChatEntry entry=new ChatEntry();
  160.        entry.name=name;
  161.        entry.text=msg;

  162.        chatEntries.Add(entry);

  163.        if(chatEntries.Count>4)
  164.        {
  165.             chatEntries.RemoveAt(0);
  166.        }
  167.        scrollposition.y=1000000;
  168.        inputField="";
  169.    }
  170.     void addGameChatMessage(string str)
  171.     {
  172.         ApplyGlobalChatText(" - ",str);
  173.         if(Network.connections.Length>0)
  174.         {
  175.             networkView.RPC("ApplyGlobalChatText",RPCMode.Others," - ",str);
  176.         }
  177.     }
  178. }
复制代码
发表于 2017-2-23 15:47 | 显示全部楼层
楼主是超人
发表于 2017-2-23 15:04 | 显示全部楼层
顶顶多好
发表于 2017-2-23 15:12 | 显示全部楼层
真心顶
发表于 2017-2-23 15:54 | 显示全部楼层
难得一见的好帖
发表于 2017-2-23 15:58 | 显示全部楼层
LZ真是人才
发表于 2017-3-7 12:55 | 显示全部楼层
很不错
发表于 2017-3-7 13:22 | 显示全部楼层
楼主是超人
发表于 2017-3-7 12:37 | 显示全部楼层
好帖就是要顶
发表于 2017-3-7 12:41 | 显示全部楼层
说的非常好
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-1 08:44 , Processed in 0.094898 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表