Unetã§HLAPIã䜿çšããŠãããè¡ãæ¹æ³ãç°¡åã«èª¬æããŸãããNetworkingManagerçµç±ã§ã¯ãªããå°ãäœãã¬ãã«ã§èª¬æããŸãã è¯ãäŒçµ±ã«ããããã®ãããªã¯ã©ã€ã¢ã³ããšãµãŒããŒã®çžäºäœçšã®å®è£ äŸãæ·»ä»ããŸãã ãã®ãœãªã¥ãŒã·ã§ã³ã®ã¢ãŒããã¯ãã£ã¯äŸ¡å€ããªããå°æ¥å€ãã®åé¡ãåŒãèµ·ããããšãå®å šã«ç解ããŠãããããäŸãå³å¯ã«å€æããªãã§ãã ããã ç§ã®ç®æšã¯ãå¯èœãªéãè¿ éã«ïŒé±æ«ã«ïŒã·ã¹ãã ãèšè¿°ãããããã¯ãŒã¯ãæäœããååã瀺ãããšã§ããã ãŸããç§ãçŽé¢ããªããã°ãªããªãã£ãåé¡ãèšããŸãã ãã®å®è£ äŸã§ã¯ããµãŒããŒãUnityã¢ããªã±ãŒã·ã§ã³ã§ãããšæ³å®ããŠããŸãã
ã€ã³ã¿ãŒãããã§ã¯ããã«ããã¬ã€ã€ãŒã®äœãæ¹ã®æãäžè¬çãªäŸã¯ãã£ããã§ãããç§ã¯ã²ãŒã ã奜ãã§ããã£ãããããã®ã¯éå±ã«æããŸããã ãããã£ãŠãäžç®äžŠã¹ã®äŸã䜿çšããŠãã«ããã¬ã€ã€ãŒãäœæããæ¹æ³ã決å®ããããšã«ããŸããã ã²ãŒã ã®ãã¹ãŠã®ããžãã¯ãåè ã®æ±ºå®ãåãã®å€æŽãªã©ãæžãããŠããŠããµãŒããŒãåºå®ããå¿ èŠããããšããŸãã æãç°¡åãªå Žåãäžç®äžŠã¹ã§ã¯ã2ã€ã®ã¡ãã»ãŒãžãåŠçããå¿ èŠããããŸãã ã³ãŒã¹ã®é åºã®æ±ºå®ïŒIDã®é åžïŒããã³ã»ã«ã®ãã£ããã£ã
äžè¬ã«ããã®äŸã®ã²ãŒã ã¯éåžžã«ç°¡åã«æ©èœããŸãã 2ã€ã®ã·ãŒã³ããããŸãã ããŒããšã²ãŒã ãã¬ã€ã ã²ãŒã ãã¬ã€ã·ãŒã³ãããŒããããšããã¬ã€ã€ãŒããã¬ã€ãããã£ãŒã«ããçæãããŸãã åå©æ¡ä»¶ã®ãã§ãã¯ããããUIã®åäœã移åã®é åºãã²ãŒã ã®å šäœçãªããžãã¯ãæ åœããã¯ã©ã¹ããããŸãããç¹ã«èå³ã¯ãããŸããã ã°ãªãããæ åœããäž»ãªã¯ã©ã¹ã¯ããµãŒããŒãã¯ã©ã€ã¢ã³ããNetManagerãNetMessagesã¡ãã»ãŒãžçšã®åå¥ã®ãã¡ã€ã«ãããã³ãã®äžã§å®çŸ©ãããŠããåæåMyMsgTypeã§ãã UnetããŒã«ã®ã©ãããŒã§ãã Unetã®èŠ³ç¹ããã䜿çšããäž»ãªã¯ã©ã¹ã¯NetworkClientãNetworkServerãMessageBaseãããã³MsgTypeã§ãã ãããã®ã¯ã©ã¹ã¯äœã§ããïŒ
æãåçŽãªã¯ã©ã¹ã¯ãMessageBaseãšMsgTypeã§ãã æåã®ã¯ã©ã¹ã¯ãã¯ã©ã€ã¢ã³ããšãµãŒããŒéã§ã¡ãã»ãŒãžã転éããããã«ãã¹ãŠã®ã¡ãã»ãŒãžãç¶æ¿ããå¿ èŠãããæœè±¡ã¯ã©ã¹ã§ãã MsgTypeã¯ãUnetã«çž«ãä»ããããç¹å®ã®ã¡ãã»ãŒãžã»ãããåŠçããå®æ°ãæ ŒçŽããã¯ã©ã¹ã§ãã
NetworkServerã¯ããªã¢ãŒãã¯ã©ã€ã¢ã³ããšã®éä¿¡ãåŠçããæ©èœãæäŸããã·ã³ã°ã«ãã³ã§ãã å éšã§ã¯ãNetworkServerSimpleã®ã€ã³ã¹ã¿ã³ã¹ã䜿çšããåºæ¬çã«ãã®äžã§äŸ¿å©ãªã©ãããŒã§ãã ãŸããç¹å®ã®ããŒãã§ãµãŒããŒãèµ·åããå¿ èŠããããŸãã ãããè¡ãã«ã¯ã ListenïŒint serverPortïŒã¡ãœãããåŒã³åºããŸãããã®ã¡ãœããã¯serverPortããŒãã§ãµãŒããŒãèµ·åããŸãã ïŒ0ãã1023ã®ç¯å²ã®ãã¹ãŠã®ããŒãã¯ã·ã¹ãã ããŒãã§ããããã®ã¡ãœããã®ãã©ã¡ãŒã¿ãŒãšããŠäœ¿çšããªãã§ãã ããïŒ
ãµãŒããŒã¯æ£åžžã«åäœããŠãããããŒãããªãã¹ã³ããŠããŸãã ä»ã圌ã¯ã¡ãã»ãŒãžã«å¿çããå¿ èŠããããŸãã ãããè¡ãã«ã¯ã RegisterHandlerã¡ãœããïŒçãmsgTypeãNetworking.NetworkMessageDelegateãã³ãã©ãŒïŒã䜿çšããŠãã³ãã©ãŒãç»é²ããŸãã ãã®ã¡ãœããã¯ããã©ã¡ãŒã¿ãšããŠã¡ãã»ãŒãžã¿ã€ããšããªã²ãŒããåãå ¥ããŸãã 次ã«ãããªã²ãŒãã¯NetworkMessageå ¥åãã©ã¡ãŒã¿ãŒãåãå ¥ããå¿ èŠããããŸãã ãã¬ãŒã€ãŒããµãŒããŒã«åå ããã²ãŒã ãã¬ã€ã·ãŒã³ã®èªã¿èŸŒã¿ãéå§ããããã¬ãŒã€ãŒã®èå¥åãèãããç¬éãå¿ èŠã ãšããŸãã 次ã«ã察å¿ããã¡ãã»ãŒãžã®ãã³ãã©ãŒãç»é²ãããšãšãã«ãç»é²ã®ããªã²ãŒããšããŠæž¡ãã¡ãœãããå®è£ ããå¿ èŠããããŸãã
次ã®ããã«ãªããŸãã
ãã³ãã©ãŒãšããªã²ãŒãã®ç»é²äŸ
NetworkServer.RegisterHandler(MsgType.Connect, OnConnect); private void OnConnect(NetworkMessage msg) { Debug.Log(string.Concat("Connected: ", msg.conn.address)); var connId = msg.conn.connectionId; if (NetworkServer.connections.Count > Constants.PLAYERS_COUNT) { SendPlayerID(connId, -1); } else { int index = Random.Range(0, Constants.PLAYERS_IDS.Length); SendPlayerID(connId, Constants.PLAYERS_IDS[index]); _CurrentUser.PlayerID = Constants.PLAYERS_IDS[(index + 1) % Constants.PLAYERS_COUNT]; SceneManager.LoadScene(1); } }
ããã§ããŠãŒã¶ãŒãæ¥ç¶ãããã³ã«OnConnectã¡ãœãããåŒã³åºãããŸãã ãã®å®è£ ã§ã¯ãIDãé²è¡ã®é åºã決å®ãããããæåã®æ¥ç¶ã§ã¯ã©ã€ã¢ã³ããšãµãŒããŒã®IDãéžæãããããšãæ確ã«ãã䟡å€ããããŸãã æ®ãã®ã¯ã©ã€ã¢ã³ãã¯èªåçã«-1ã®IDãååŸããŸããããã¯ããã®ã¯ã©ã€ã¢ã³ãã芳客ã§ããããšãæå³ããŸãã
ã·ã³ãã«ãªãµãŒããŒããããŸãã ä»ã§ã¯é¡§å®¢ã¯éªéã«ãªããŸããã ãããè¡ãã«ã¯ãNetworkClientã¯ã©ã¹ã䜿çšããŸãã ãµãŒããŒã«åå ããã«ã¯ã Connectã¡ãœããïŒstring serverIpãint serverPortïŒãåŒã³åºãã ãã§ãã ããŒããšããŠããµãŒããŒããªãã¹ã³ããããŒããèšå®ãããµãŒããŒãšããŠäœ¿çšããåããã·ã³ãŸãã¯ããŒã«ã«ãããã¯ãŒã¯äžã®IPã³ã³ãã¥ãŒã¿ãŒã§ã¢ããªã±ãŒã·ã§ã³ããã¹ãããå ŽåãIPãšããŠlocalhostãèšå®ããŸãïŒãããã¯ãŒã¯èšå®ã§èŠã€ããããšãã§ããŸãããŸãã¯ãµãŒããŒãšããŠæ©èœããã³ã³ãã¥ãŒã¿ãŒã§ipconfigã³ãã³ãã䜿çšããŠãã³ã³ãœãŒã«ã§ïŒã
çŽ æŽããããç§ãã¡ã¯æ¥ç¶ã§ããŸãã ãµãŒããŒãèå¥åãé åžãããšä»¥åã«èšãããŸããã ãã®ããããŸããèå¥åã«é¢ããã¡ãã»ãŒãžãéä¿¡ãããã®ã¡ãã»ãŒãžã®ãã³ãã©ãŒãã¯ã©ã€ã¢ã³ãã«ç»é²ããå¿ èŠããããŸãã åè¿°ã®ããã«ããã¹ãŠã®ã¡ãã»ãŒãžã¯MessageBaseããç¶æ¿ããå¿ èŠããããŸãã æåã«ãããããå®çŸ©ããŸãããïŒå¥ã®ãã¡ã€ã«ã§ãããè¡ãããšã奜ã¿ãŸãïŒïŒ
ã¡ãã»ãŒãžãšãã®ã¿ã€ã
public class PlayerIDMessage : MessageBase { public int PlayerID; } public enum MyMsgType : short { PlayerID = MsgType.Highest + 1, }
ãã®ã¡ãã»ãŒãžãéä¿¡ããã«ã¯ãã¯ã©ã€ã¢ã³ãã§Sendã¡ãœããïŒçãmsgTypeãNetworking.MessageBase msgïŒãåŒã³åºããŸãããã®ã¡ãœããã¯ãmsgã¡ãã»ãŒãž "type" msgTypeããµãŒããŒã«éä¿¡ããŸãã SendToClientïŒint connectionIdãçãmsgTypeãNetworking.MessageBase msgïŒ ãconnectionIdã¯ç¹å®ã®ã¯ã©ã€ã¢ã³ãã®IDã§ãã
å¥ã®ã¢ããªã±ãŒã·ã§ã³ããéãããŠããã«ã¹ã¿ã ã¡ãã»ãŒãžãèªãã«ã¯ã reader.ReadMessageïŒïŒã䜿çšããŸãã äŸïŒ
å°çããIDã®ã¯ã©ã€ã¢ã³ãã§ã®åŠç
private void OnPlayerID(NetworkMessage msg) { PlayerIDMessage message = msg.reader.ReadMessage<PlayerIDMessage>(); _CurrentUser.PlayerID = message.PlayerID; SceneManager.LoadScene(1); }
ååãšããŠããã¹ãŠã ãããªã䜿çšã¯ç¹å®ã§ãã ã²ãŒã ãã¬ã€ã«å¿ããŠå¿ èŠãªã¡ãã»ãŒãžãäœæããéä¿¡ããŸãã tic-tac-toeã§ã¯ããã€ã³ãã®ååŸãæ åœããå¥ã®ã¡ãã»ãŒãžãç¹å®ããŸããã ãããžã§ã¯ãã®å®å šãªå®è£ ã«ã€ããŠã¯ã ãã¡ããã芧ãã ãã ã
ç§ãä»ã«èšãããããšããããŠç§ãæéãè²»ãããªããã°ãªããªãã£ãããšã¯ãããªãããããã¯ãŒã¯ã§ä»äºãããŠããªããªãããªãã«ã¯æããã§ãªããããããªãããã€ãã®ããšã§ãã
1. TCPããã³UDPãä»ããŠéä¿¡ããããã«ãUnity Editorããã¡ã€ã¢ãŠã©ãŒã«ã«ãã£ãŠãããã¯ãããŠããªãããšã確èªããŸãã ãã¡ã€ã¢ãŠã©ãŒã«ã«ã¢ã¯ã»ã¹ããŠç®çã®ããŒããäŸå€ã«èšå®ããŸãããããšãã£ã¿ãŒããããã¯ãããŠããªãããšã¯ç¢ºèªããŠããŸããã§ããããããã«ãã°ããæéãè²»ãããŸããã
2.ã¡ãã»ãŒãžã§å€åãŸãã¯ã·ãªã¢ã«åå¯èœãªåç §åãæž¡ããMonoBehaviorã®çžç¶äººãæž¡ããŸããã ãŸãããã®å Žåã®åç §åã¯ãªããžã§ã¯ãèªäœã§ã¯ãªããªããžã§ã¯ãã®ã³ããŒãéä¿¡ãããããããã«å¿ããŠåŠçããå¿ èŠãããããšãç解ããããšãéèŠã§ãã
ããŒã«ã«ãããã¯ãŒã¯ãšä»¥åããç¥ãããŠããããŒããŠã§ã¢ã«ã€ããŠè©±ããŠããå Žåããå®éã®ããã«ããã¬ã€ã€ãŒã®å Žåã«ã¯å€ãã®åé¡ã¯çºçããŸããã é 延ãå€åããã±ããæ倱ãªã©ã«ã€ããŠèããå¿ èŠã¯ã»ãšãã©ãããŸããã ãããã£ãŠããã®ãããªãœãªã¥ãŒã·ã§ã³ã¯æçšã§ããããããã®åé¡ã¯ç¹å®ã®å¶éã«å¯Ÿãããã®ãããªã¢ãããŒãã§è§£æ±ºã§ããŸãã NetworkManagerãNetworkBehaviorãªã©ãä»ããUnetã®é«ã¬ãã«ã®æœè±¡åãšæ¯èŒããŠãã¯ã©ã€ã¢ã³ããç°ãªãã·ãŒã³ãªã©ãããŒãããå¿ èŠãããå Žåãããç解å¯èœã§æçœãªæè»æ§ãæäŸããŸããããµãŒããŒã䜿çšãããŠãããšããŸããããã¹ããªãŒãã³ã°ã®ããã«ããã¬ãŒã€ãŒã®ããã€ã¹ã«è¡šç€ºããããã®ã衚瀺ãã圌ã®äœçœ®ãååŸãã圌ã®åŽã§èµ·ãã£ãŠããããšãåããŠ+è€è£œããŸãã ãŸãããœãªã¥ãŒã·ã§ã³ãããè¿ éã«å¿ èŠã«ãªãããããã¯ãŒã¯ãæäœã§ããå ŽåãUnetã¯ãã©ã³ã¹ããŒãã¬ãã«ã§æžã蟌ãæ©èœãæäŸããŸãã
ç§ã¯ãŸããç§ã®æèŠã§ã¯githubã®æ±ºå®ãæ確ã«ããããšæã£ãŠããŸãïŒããŒã«ã®èŠ³ç¹ããã§ã¯ãªããã¢ãŒããã¯ãã£ãžã®ã¢ãããŒãã®èŠ³ç¹ããïŒãããã¯äŸã§ãã ã²ãŒã èªäœã®ã¢ãŒããã¯ãã£ã¯èšããŸã§ããªããåé¡ã¯ã¯ã©ã€ã¢ã³ããšãµãŒããŒã®ããžãã¯ãç¬ç«ããŠãããšèŠãªãããããšã§ãã ã¯ã©ã€ã¢ã³ã/ãµãŒããŒã¢ãŒããã¯ãã£ã§é©åãªãã«ããã¬ã€ã€ãŒãå®è£ ããå Žåãã²ãŒã ã®ç¶æ ããµãŒããŒã«ä¿åããŠã¯ã©ã€ã¢ã³ãã«è€è£œããã¯ã©ã€ã¢ã³ãããµãŒããŒäžã®ã²ãŒã ã®ç¶æ ãå€æŽããã³ãã³ããéä¿¡ããæ¹ãé©åã§ãã ãã¡ããããããå€ãã®èŠå ã«äŸåããŸãããå¹³åçã«ã¯ãã®ãããªã¢ãããŒãã§ãã
ããã«ãœãªã¥ãŒã·ã§ã³ãžã®ãªã³ã¯ãè€è£œããŸãã
ãæž èŽããããšãããããŸããïŒ