ã¯ããã«
ã¾ã å®é¨ä¸ãªã®ã§ãããUnity WebGL x WebSocket ã§å¤§éã®ã¯ã©ã¤ã¢ã³ãéã§å¤§éã®ãªãã¸ã§ã¯ããåæã§ããä»çµã¿ãä½ã£ã¦ãã¾ããUNET ã Photon ã«ãã£ã¦è¿ããã¡ã«å¯¾å¿ãããã¨æãã®ã§ãããç¾ç¶ã§ã¯ä½¿ããªãã®ã¨ãå¶éãªãæ±ããããã¥ã¼ãã³ã°æ¬¡ç¬¬ã§éãåºæ¥ãå¯è½æ§ãããã®ã§ä½ã£ã¦ã¿ããã¨æãã¾ããã
æ¨æ¥ãæè¡ãã¢ãå ¬éãã¾ãã¦ãæ大ã§åæã« 30 ã 40 人ã»ã©ã®æ¹ã«åæã«éãã§ãããã¾ãããçæ§ãããã¨ããããã¾ããï¼200 ãªãã¸ã§ã¯ãããããªããã¬ã¼ã è½ã¡ãããã¨ç¡ããµã¯ãµã¯åããã¨æãã®ã§ãç¾ç¶ã®ä»çµã¿ãç¥è¦ãªã©ãåå¿é²ãå ¼ãã¦å ±æã§ããã°ã¨æãã¾ãã
éçºç°å¢
- Mac OS X 10.10.1
- Unity 5.0.0b18
ãã¢
ãã¤æ¢ãããã¯æªå®ã§ããã以ä¸ããéã¶ãã¨ãåºæ¥ã¾ãã
Mac x Chrome ã ã¨ãã¼ããã°ãã®ã§ããã以å¤ã®çµã¿åããã§éãã§ã¿ã¦ãã ããï¼Firefox æ¨å¥¨ï¼ã
- WASD: 移å
- QE: å転
- Space: ã¸ã£ã³ã
- ãã©ãã°: è¦ç¹ç§»å
- F: å¼¾çºå°
åç»
@itachin ãããæ®å½±ãããã¨ããããã¾ãã
ããã¸ã§ã¯ãä¸å¼
åèç¨åº¦ã«ã覧ãã ããã
ä»çµã¿
ã¾ãã以ä¸ã®ï¼ã¤ã®è¦ä»¶ãèãã¾ããã
- éãããµã¯ãµã¯
- ãªãã¸ã§ã¯ãã®åæããããç°¡åãã¹ã±ã¼ã«ãã
ãããå®ç¾ããããã«ãä»çµã¿ãèãããè²ã å®é¨ãã¦ã¿ã¾ãããæçµçã«ä»çµã¿ã®å ¨ä½åã¯ãç ©éã§ãã以ä¸ã®æ§ãªå½¢ã«ãã¾ããã
ã¾ããã²ã¼ã ã¨ãã¦ã®ä½æåä¸ã®ããã«ããã¹ã¿ã¼ããã¦ãããã§åãããå 容ãåæããã®ã§ã¯ãªããèªæ©ã»èªåãçæãããªãã¸ã§ã¯ãã¯èªåã®ã¨ããã§åããã¦ããããä»ã®ã¯ã©ã¤ã¢ã³ãã¨åæããå½¢ã«ãã¾ããã
åæã¯ãä¸å³ã§ãã SynchronizedComponent
ãéãã¦è¡ãã¾ãããã㯠Send()
㨠OnReceive()
ãéãã¦ã¡ãã»ã¼ã¸ã®ããåãããã¾ããä¾ãã°ä½ç½®ã®åæã¯å
·ä½çã«ã¯ä»¥ä¸ã®æ§ãªã¹ã¯ãªãããæ¸ãã¦ãä½ç½®ãåæããããªãã¸ã§ã¯ãã«ã¢ã¿ãããã¦ããã ãåæãããããã«ãããã¨ã§ã¹ã±ã¼ã«ãããããªä»çµã¿ã«ãã¾ããã
using UnityEngine; public class PositionSync : SynchronizedComponent { private Vector3 to_; private bool isFirst_ = true; // ãã¼ã«ã«ã§æå®ããééã§å¼ã°ãã protected override void OnSend() { // ãªã¢ã¼ãã«æ å ±ãä¼ãã Send(transform.position); } // ãªã¢ã¼ãã®ãªãã¸ã§ã¯ãã Send ãåä¿¡ãããå¼ã°ãã protected override void OnReceive(Vector3 value) { if (isFirst_) { transform.position = to_ = value; isFirst_ = false; } else { to_ = value; } } // ãªã¢ã¼ãã§æ¯ãã¬ã¼ã å¼ã°ããï¼isLocal == falseï¼ protected override void OnRemoteUpdate() { transform.position += (to_ - transform.position) * easing; } // ãã¼ã«ã«ã§æ¯ãã¬ã¼ã å¼ã°ããï¼isLocal == trueï¼ protected override void OnLocalUpdate() { } }
ä»ã«ã姿å¢ããã¢ä¸ã®ãããã£ã¼ã«é¨åã®åæãªã©ã¯ä»¥ä¸ã®ã³ã¼ãã«æãã¾ãã
- UWO/RotationSync.cs at 4c3d50a98473f9259e2ed09b52092e1fce57b2d3 · hecomi/UWO · GitHub
- UWO/ProfileSetter.cs at 4c3d50a98473f9259e2ed09b52092e1fce57b2d3 · hecomi/UWO · GitHub
Send()
ã§ç»é²ããå¤æ°ã¯ãã£ãããã¼ã«ããã¾ãããã®æä¸ã¤ã®ã³ã³ãã¼ãã³ãããã®ã¡ãã»ã¼ã¸ã¯ä»¥ä¸ã®æ§ãªç¬èªã®ãã©ã¼ãããã«èªåã§å¤æããã¾ãã
// Command ComponentID GameObjectID PrefabPath ComponentName Params Type u a6936e8f-9669-4c0e-b04c-c6df99ff5998 13f76ab3-b871-4201-a8e8-4d7152ad9e79 Prefabs/Network Player PositionSync -0.9182832,0.5,-1.529093 vector3
ComponentName
㯠GetType().Name
ã§åå¾ãID 㯠GUID ãçæãã¦ããã¾ããSynchronizedComponent
ãã¢ã¿ãããã㨠SynchronizedObject
ãèªåã§ã¢ã¿ãããããGameObject èªä½ã®ç®¡çãããã¾ãããã㧠GameObject ã® IDãããã³ã©ã® Prefab ããçæãããã¨ãã PrefabPath
ï¼Resources
ä¸ã®ãã¹ï¼ã EditorScript ãéãã¦èªåçã«åå¾ãã¦ããã¾ãã
ãããèªåçã«çæããã Synchronizer
ã¹ã¯ãªãããéãã¦éä¿¡ãã¾ããã³ã³ãã¼ãã³ããã Send()
ã§éããã¦ããã¡ãã»ã¼ã¸ã¯ãååéã£ããã¼ã¿ããå¤åããã£ãããã£ãããã¼ã«ãã¦ãé©å½ãªééã§ãµã¼ãã«éä¿¡ããã¾ãã
ãµã¼ãå´ã§ã¯ãéããã¦ããã¡ãã»ã¼ã¸ãå
¨ã¦ãã¼ã¸ãã¦åã¯ã©ã¤ã¢ã³ãã¸é
ä¿¡ãã¾ããããã¦ãå¥ã®ã¯ã©ã¤ã¢ã³ãã§ãã®ãã¼ã¿ãåä¿¡ããã¨ãã«ãã«ãã¯ã¢ãããã¼ãã«ã« ID ã§è©²å½ã®ã³ã³ãã¼ãã³ããåãåãããç¡ãã£ããä¸ãããã PrefabPath
ã® Prefab ã Instantiate()
ããããã«ã¢ã¿ãããããã³ã³ãã¼ãã³ããç»é²ããµã¼ãããéããã¦ãããã©ã¡ã¿ãéç¥ãã¾ããããã«ãã£ã¦è¤æ°ã¯ã©ã¤ã¢ã³ãéã®ã³ã³ãã¼ãã³ãã®åæãå¯è½ã«ãªããé¢åãªçæã®æé ãçããã¨ãåºæ¥ã¾ããã¾ãããã®ããã«ã«ãã¯ã¢ãããã¼ãã«ãç¨æãã¦ãããã¨ã§ ID ããã¼ã«è¦ç´ ãåå¾ã§ããã®ã§ãFind()
ããã GetComponent()
ããã³ã¹ããåæ¸ã§ãã¾ãã
å¾ã¯ãOnDestroy()
æã«èªåçã«ç ´æ£ã®ã¡ãã»ã¼ã¸ãéç¥ãã¦ãå
¨ã¯ã©ã¤ã¢ã³ãã§è©²å½ã® GameObject ã Destroy()
ãã¾ããçªç¶çµäºããå ´åã¯ãããæ¤ç¥ã§ããªãã®ã§ã HeartBeat()
ã¨ããå½¢ã§ã«ææ°ã®ãã©ã¡ã¿ãä¸å®æéããã« Send()
ãããã¨ã«ãããã°ããéç¥ãæ¥ãªãå ´åã¯èªåçã« GameObject ãç ´æ£ or æãã¢ã¯ã»ã¹æéã®é·ãã¯ã©ã¤ã¢ã³ãã«å¼ãç¶ãã¾ããã¾ããããã«ãããã°ããåãã¦ãªããªãã¸ã§ã¯ãããã¡ãã»ã¼ã¸ãéãããªããã¨ã§ãæ°ããåå ããã¯ã©ã¤ã¢ã³ãä¸ã§ãã®ãªãã¸ã§ã¯ãã Instantiate()
ãããªãã®ãé²ããã¨ãåºæ¥ã¾ãã
å¾ã¯ããµã¼ãå´ã«å¹¾ã¤ãã®ã³ã³ãã¼ãã³ãã®ãã©ã¡ã¿ã¯ä¿åããããã«ãããã®ã§ãããã¾ã æªå®è£ ã§ããç¾ç¶ã®ãµã¼ãå´ï¼Node.jsï¼ã®ã³ã¼ãã¯æ¬¡ã®ããã«ãªãã¾ãã
var ws = require('ws').Server; var server = new ws({ port: 3000 }); var messages = []; var fps = 30; server.broadcast = function(data) { var self = this; this.clients.forEach(function(client, index) { var info = ['i', (index === 0 ? 'true' : 'false'), +new Date(), self.clients.length].join('\t'); var message = info + '\n' + data; client.send(message, function(err) { if (err) console.error(err); }); }); }; server.on('connection', function(socket) { socket.on('message', function(message) { messages.push(message.toString()); }); setInterval(function() { if (messages.length === 0) return; isSendToClientsProcessing = true; { var currentMessages = messages; messages = []; if (currentMessages.length > 0) { server.broadcast(currentMessages.join('\n')); } } }, 1000 / fps); }); process.on('uncaughtException', function(e) { console.error("Unexpected Exception:", e.stack); });
ç¥è¦
JSON ã¯ãã©ã¤ãç¬èªãã©ã¼ããããéã
æåã¯ã¡ãã»ã¼ã¸ã®ããåãã JSON ã§ãããã¨æã£ã¦ãã¦ãSimpleJSON ã MiniJSON ã使ã£ã¦ããã®ã§ãããã¨ã³ã³ã¼ãã¨ãã³ã¼ãã«ããªãã®æéãè²»ããããããã¾ããã
- http://wiki.unity3d.com/index.php/SimpleJSON
- Unity3D: MiniJSON Decodes and encodes simple JSON strings. Not intended for use with massive JSON strings, probably < 32k preferred. Handy for parsing JSON from inside Unity3d. · GitHub
ãã®å¾ã@mohammedari ããã« MessagePack ã® Unity å®è£ ãæãã¦ããã ããMiniMessagePack ã使ã£ã¦ã¿ãã®ã§ãããããã§ãããã©ã¼ãã³ã¹çã«ååã§ã¯ããã¾ããã§ããã
@hecomi Google Message Packã¨ãã©ãã§ããï¼ http://t.co/D7yrwRnlsj
— Kazuyuki Arimatsu@OMSCS (@mohammedari) 2014å¹´12æ30æ¥
ããã§ãéãã¡ãã»ã¼ã¸ãã·ã³ãã«ãªã®ã§ JSON ã§ãªãã¦ããããã¨æããåè¿°ã®ãããªã·ã³ãã«ãªãã©ã¼ããããä½ã£ã¦ã¿ãã¨ããããã©ã¼ãã³ã¹ãåçã«æ¹åããã¦ããããã¡ã¤ã©ãè¦ã¦ã ToString()
ã Concat()
ãSplit()
ãæ¯é
çã«ãªã£ã¦ããã®ã§ãç¬èªãã©ã¼ãããã§è¡ããã¨ã«ãã¾ããããã¤ããªã«ããã°ãã£ã¨éãã¨æãã¾ããçµæã¨ãã¦å®è£
ãã·ã³ãã«ã«ãªã£ã¦è¯ãã£ãã§ãã
WebSockets.unitypackage ã§åä¿¡ãä¸æããããªã
å
æ¥ç´¹ä»ãããWebSockets.unitypackage
ã§ãããã©ãã«ãåä¿¡ããã¾ãåããªãããã§ã...ãUnity Editor ä¸ã§ã¯ãã¾ãåãã®ã§ãããif ãã£ã¬ã¯ãã£ã㧠Editor 㨠WebSocket ãã«ãããæã®åä½ãåãåããã¦ãããDLL ä¸ã«å®è£
ãããã¦ããã®ã§è«¦ããUnity 5 x WebGL について詳しく調べてみた - 凹みTips ã§è¡ã£ãããã«ãã©ã¦ã¶ã¨ WebSocket éä¿¡ã®ããã¨ããããèãã©ããã¼ãæ¸ãã¦è§£æ±ºãã¾ããã
- UWO/WebSocketSyncServer.cs at 4c3d50a98473f9259e2ed09b52092e1fce57b2d3 · hecomi/UWO · GitHub
- UWO/WebSocketBrowser.cs at 4c3d50a98473f9259e2ed09b52092e1fce57b2d3 · hecomi/UWO · GitHub
Editor ä¸ã§ã¯æ®éã« WebSockets.unitypackage
ãå©ç¨ãããã«ãå¾ã¯ãã©ã¦ã¶ã¨éä¿¡ããå½¢ã§ãããã©ã¦ã¶ã§ã¯æ¬¡ã®ãããªã³ã¼ããæ¸ãã¦ãããã¨ã§ããã¨ãã§ãã¾ãã
var ws = new WebSocket('ws://127.0.0.1:3000'); window.addEventListener('load', function() { var isInitialized = false; window.init = function() { isInitialized = true; SendMessage('Local Player', 'SetMessage', 'Hello!'); }; ws.onmessage = function(event) { if (isInitialized) { SendMessage('Synchronizer', 'PushWebSocketData', event.data); } }; });
init()
㯠Unity ã®ä¸çããå¼ã°ããé¢æ°ã§ãæºåãã§ãããã¨ãæå³ãã¾ãããã®å¾ Unity ã®ä¸ã®ä¸çã¸ã¨éããã¦ããã¡ãã»ã¼ã¸ãéç¥ãã¾ãã
Editor ä¸ã§é ã
ãã«ããã¦åããã¨ãµã¯ãµã¯åãã®ã§ããããªãã Editor ä¸ã§éä¿¡ããªããåããã¨é ã...ãããã¯éçºã«ãå½±é¿ããã®ã§èª¿æ»ãã¦åããæ¬¡ç¬¬å ±åãã¾ãã
ãããã«
ã¾ã å°ããªãã®ã§ãã試ãã¦ãªãã®ã§ããã©ã¼ãã³ã¹çã«ãéçºçã«ãã¹ã±ã¼ã«ãããã¯ä¸æã§ããããã®ã¾ã¾è²ã ä½ã£ã¦ããããã§ããHTML5 ã®åãé©å®åããªããè²ã ã¨åºæ¥ãããã«ã¨ãã£ãå®é¨ããã¦ããããã¨æã£ã¦ãã¾ãã次ã¯ã²ã¼ã ã¨ãã¦çã§éã¹ããããªå½¢ã«ãããã§ãã