当前位置:   article > 正文

Unity云渲染demo 使用小记_vue +unity 实现云渲染

vue +unity 实现云渲染

github地址:GitHub - Unity-Technologies/UnityRenderStreaming: Streaming server for Unity

目前我拉取的是 1.2.3这一分支,拉取到本地后,即可开始调试

一、启动WEBRTC服务器

用vscode打开WEBAPP这个文件夹,在该目录下执行终端命令

npm run build

npm run start

即可开启服务

同时终端窗口还会给出浏览器地址,打开该地址

二、启动Unity

要用2019版本的Unity报错才少,打开后,Unity告知PointerPhase类报错,换成TouchPhase即可,activetouches换成touches即可,这个时候就可以运行Unity了。运行Unity成功后即可看到Game窗口内容如下

三、发现操作与浏览器交互并不能操作unity端

经过代码查看,发现应该是InputSystem.QueueStateEvent这个方法的问题,这个方法调用后,并没有促发Unity的操作功能,所以此时无奈,只能自己些交互操作了。

-----------------------------------------------------------------------

这个时候也对两边的代码进行了改动,首先WEBAPP中的代码,首先把Mouse改成了MouseMove,同时新增了MouseClick的枚举:

  1. const InputEvent = {
  2. Keyboard: 0,
  3. MouseMove: 1,
  4. MouseWheel: 2,
  5. Touch: 3,
  6. ButtonClick: 4,
  7. MouseClick: 5,
  8. };

把SendMouse改成了SendMouseMove和SendMouseClick方法:

  1. function sendMouseMove(e) {
  2. const scale = _videoPlayer.videoScale;
  3. const originX = _videoPlayer.videoOriginX;
  4. const originY = _videoPlayer.videoOriginY;
  5. if (x && y) {
  6. const deltaX = (e.clientX - x) / scale;
  7. const deltaY = (e.clientY - y) / scale;
  8. let data = new DataView(new ArrayBuffer(9));
  9. data.setUint8(0, InputEvent.MouseMove);
  10. data.setFloat32(1, deltaX, true);
  11. data.setFloat32(5, deltaY, true);
  12. _videoPlayer && _videoPlayer.sendMsg(data.buffer);
  13. }
  14. x = e.clientX;
  15. y = e.clientY;
  16. }
  17. function sendMouseClick(e) {
  18. if (e.type == 'mouseup') {
  19. let data = new DataView(new ArrayBuffer(3));
  20. data.setUint8(0, InputEvent.MouseClick);
  21. data.setInt16(1, 0, true);
  22. _videoPlayer && _videoPlayer.sendMsg(data.buffer);
  23. }
  24. if (e.type == 'mousedown') {
  25. let data = new DataView(new ArrayBuffer(3));
  26. data.setUint8(0, InputEvent.MouseClick);
  27. data.setInt16(1, 1, true);
  28. _videoPlayer && _videoPlayer.sendMsg(data.buffer);
  29. }
  30. }

最后再修改调用的代码

  1. playerElement.addEventListener('mousedown', sendMouseClick, false);
  2. playerElement.addEventListener('mouseup', sendMouseClick, false);
  3. playerElement.addEventListener('mousemove', sendMouseMove, false);
  4. playerElement.addEventListener('wheel', sendMouseWheel, false);

此时Web端的调用已经修改完成

这个时候register-event的整个代码如下

  1. const InputEvent = {
  2. Keyboard: 0,
  3. MouseMove: 1,
  4. MouseWheel: 2,
  5. Touch: 3,
  6. ButtonClick: 4,
  7. MouseClick: 5,
  8. };
  9. const KeyboardEventType = {
  10. Up: 0,
  11. Down: 1
  12. }
  13. const PointerPhase = {
  14. None: 0,
  15. Began: 1,
  16. Moved: 2,
  17. Ended: 3,
  18. Canceled: 4,
  19. Stationary: 5
  20. }
  21. const Keymap = {
  22. "Space": 1,
  23. "Enter": 2,
  24. "Tab": 3,
  25. "Backquote": 4,
  26. "Quote": 5,
  27. "Semicolon": 6,
  28. "Comma": 7,
  29. "Period": 8,
  30. "Slash": 9,
  31. "Backslash": 10,
  32. "LeftBracket": 11,
  33. "RightBracket": 12,
  34. "Minus": 13,
  35. "Equals": 14,
  36. "KeyA": 15,
  37. "KeyB": 16,
  38. "KeyC": 17,
  39. "KeyD": 18,
  40. "KeyE": 19,
  41. "KeyF": 20,
  42. "KeyG": 21,
  43. "KeyH": 22,
  44. "KeyI": 23,
  45. "KeyJ": 24,
  46. "KeyK": 25,
  47. "KeyL": 26,
  48. "KeyM": 27,
  49. "KeyN": 28,
  50. "KeyO": 29,
  51. "KeyP": 30,
  52. "KeyQ": 31,
  53. "KeyR": 32,
  54. "KeyS": 33,
  55. "KeyT": 34,
  56. "KeyU": 35,
  57. "KeyV": 36,
  58. "KeyW": 37,
  59. "KeyX": 38,
  60. "KeyY": 39,
  61. "KeyZ": 40,
  62. "Digit1": 41,
  63. "Digit2": 42,
  64. "Digit3": 43,
  65. "Digit4": 44,
  66. "Digit5": 45,
  67. "Digit6": 46,
  68. "Digit7": 47,
  69. "Digit8": 48,
  70. "Digit9": 49,
  71. "Digit0": 50,
  72. "ShiftLeft": 51,
  73. "ShiftRight": 52,
  74. "AltLeft": 53,
  75. "AltRight": 54,
  76. // "AltGr": 54,
  77. "ControlLeft": 55,
  78. "ControlRight": 56,
  79. "MetaLeft": 57,
  80. "MetaRight": 58,
  81. // "LeftWindows": 57,
  82. // "RightWindows": 58,
  83. // "LeftApple": 57,
  84. // "RightApple": 58,
  85. // "LeftCommand": 57,
  86. // "RightCommand": 58,
  87. "ContextMenu": 59,
  88. "Escape": 60,
  89. "ArrowLeft": 61,
  90. "ArrowRight": 62,
  91. "ArrowUp": 63,
  92. "ArrowDown": 64,
  93. "Backspace": 65,
  94. "PageDown": 66,
  95. "PageUp": 67,
  96. "Home": 68,
  97. "End": 69,
  98. "Insert": 70,
  99. "Delete": 71,
  100. "CapsLock": 72,
  101. "NumLock": 73,
  102. "PrintScreen": 74,
  103. "ScrollLock": 75,
  104. "Pause": 76,
  105. "NumpadEnter": 77,
  106. "NumpadDivide": 78,
  107. "NumpadMultiply": 79,
  108. "NumpadPlus": 80,
  109. "NumpadMinus": 81,
  110. "NumpadPeriod": 82,
  111. "NumpadEquals": 83,
  112. "Numpad0": 84,
  113. "Numpad1": 85,
  114. "Numpad2": 86,
  115. "Numpad3": 87,
  116. "Numpad4": 88,
  117. "Numpad5": 89,
  118. "Numpad6": 90,
  119. "Numpad7": 91,
  120. "Numpad8": 92,
  121. "Numpad9": 93,
  122. "F1": 94,
  123. "F2": 95,
  124. "F3": 96,
  125. "F4": 97,
  126. "F5": 98,
  127. "F6": 99,
  128. "F7": 100,
  129. "F8": 101,
  130. "F9": 102,
  131. "F10": 103,
  132. "F11": 104,
  133. "F12": 105,
  134. // "OEM1": 106,
  135. // "OEM2": 107,
  136. // "OEM3": 108,
  137. // "OEM4": 109,
  138. // "OEM5": 110,
  139. // "IMESelected": 111,
  140. };
  141. let isPlayMode = false;
  142. export function registerKeyboardEvents(videoPlayer) {
  143. const _videoPlayer = videoPlayer;
  144. document.addEventListener('keyup', sendKeyUp, false);
  145. document.addEventListener('keydown', sendKeyDown, false);
  146. function sendKeyUp(e) {
  147. sendKey(e, KeyboardEventType.Up);
  148. }
  149. function sendKeyDown(e) {
  150. sendKey(e, KeyboardEventType.Down);
  151. }
  152. function sendKey(e, type) {
  153. const key = Keymap[e.code];
  154. const character = e.key.length === 1 ? e.key.charCodeAt(0) : 0;
  155. console.log("key down " + key + ", repeat = " + e.repeat + ", character = " + character);
  156. _videoPlayer && _videoPlayer.sendMsg(new Uint8Array([InputEvent.Keyboard, type, e.repeat, key, character]).buffer);
  157. }
  158. }
  159. export function registerMouseEvents(videoPlayer, playerElement) {
  160. const _videoPlayer = videoPlayer;
  161. const _playerElement = playerElement;
  162. const _document = document;
  163. // Listen to mouse events
  164. //playerElement.addEventListener('click', sendMouseClick, false);
  165. playerElement.addEventListener('mousedown', sendMouseClick, false);
  166. playerElement.addEventListener('mouseup', sendMouseClick, false);
  167. playerElement.addEventListener('mousemove', sendMouseMove, false);
  168. playerElement.addEventListener('wheel', sendMouseWheel, false);
  169. // ios workaround for not allowing auto-play
  170. // Listen to touch events based on "Touch Events Level1" TR.
  171. //
  172. // Touch event Level1 https://www.w3.org/TR/touch-events/
  173. // Touch event Level2 https://w3c.github.io/touch-events/
  174. //
  175. playerElement.addEventListener('touchend', sendTouchEnd, false);
  176. playerElement.addEventListener('touchstart', sendTouchStart, false);
  177. playerElement.addEventListener('touchcancel', sendTouchCancel, false);
  178. playerElement.addEventListener('touchmove', sendTouchMove, false);
  179. function sendTouch(e, phase) {
  180. const changedTouches = Array.from(e.changedTouches);
  181. const touches = Array.from(e.touches);
  182. const phrases = [];
  183. for (let i = 0; i < changedTouches.length; i++) {
  184. if (touches.find(function (t) {
  185. return t.identifier === changedTouches[i].identifier
  186. }) === undefined) {
  187. touches.push(changedTouches[i]);
  188. }
  189. }
  190. for (let i = 0; i < touches.length; i++) {
  191. touches[i].identifier;
  192. phrases[i] = changedTouches.find(
  193. function (e) {
  194. return e.identifier === touches[i].identifier
  195. }) === undefined ? PointerPhase.Stationary : phase;
  196. }
  197. console.log("touch phase:" + phase + " length:" + changedTouches.length + " pageX" + changedTouches[0].pageX + ", pageX: " + changedTouches[0].pageY + ", force:" + changedTouches[0].force);
  198. let data = new DataView(new ArrayBuffer(2 + 13 * touches.length));
  199. data.setUint8(0, InputEvent.Touch);
  200. data.setUint8(1, touches.length);
  201. let byteOffset = 2;
  202. for (let i = 0; i < touches.length; i++) {
  203. const scale = _videoPlayer.videoScale;
  204. const originX = _videoPlayer.videoOriginX;
  205. const originY = _videoPlayer.videoOriginY;
  206. const x = (touches[i].pageX - originX) / scale;
  207. // According to Unity Coordinate system
  208. // const y = (touches[i].pageX - originY) / scale;
  209. const y = _videoPlayer.videoHeight - (touches[i].pageY - originY) / scale;
  210. data.setInt32(byteOffset, touches[i].identifier, true);
  211. byteOffset += 4;
  212. data.setUint8(byteOffset, phrases[i]);
  213. byteOffset += 1;
  214. data.setInt16(byteOffset, x, true);
  215. byteOffset += 2;
  216. data.setInt16(byteOffset, y, true);
  217. byteOffset += 2;
  218. data.setFloat32(byteOffset, touches[i].force, true);
  219. byteOffset += 4;
  220. }
  221. _videoPlayer && _videoPlayer.sendMsg(data.buffer);
  222. }
  223. function sendTouchMove(e) {
  224. sendTouch(e, PointerPhase.Moved);
  225. e.preventDefault();
  226. }
  227. function sendTouchStart(e) {
  228. sendTouch(e, PointerPhase.Began);
  229. e.preventDefault();
  230. }
  231. function sendTouchEnd(e) {
  232. sendTouch(e, PointerPhase.Ended);
  233. e.preventDefault();
  234. }
  235. function sendTouchCancel(e) {
  236. sendTouch(e, PointerPhase.Canceled);
  237. e.preventDefault();
  238. }
  239. let x, y;
  240. function sendMouseMove(e) {
  241. const scale = _videoPlayer.videoScale;
  242. const originX = _videoPlayer.videoOriginX;
  243. const originY = _videoPlayer.videoOriginY;
  244. if (x && y) {
  245. const deltaX = (e.clientX - x) / scale;
  246. const deltaY = (e.clientY - y) / scale;
  247. let data = new DataView(new ArrayBuffer(9));
  248. data.setUint8(0, InputEvent.MouseMove);
  249. data.setFloat32(1, deltaX, true);
  250. data.setFloat32(5, deltaY, true);
  251. _videoPlayer && _videoPlayer.sendMsg(data.buffer);
  252. }
  253. x = e.clientX;
  254. y = e.clientY;
  255. }
  256. function sendMouseClick(e) {
  257. if (e.type == 'mouseup') {
  258. let data = new DataView(new ArrayBuffer(3));
  259. data.setUint8(0, InputEvent.MouseClick);
  260. data.setInt16(1, 0, true);
  261. _videoPlayer && _videoPlayer.sendMsg(data.buffer);
  262. }
  263. if (e.type == 'mousedown') {
  264. let data = new DataView(new ArrayBuffer(3));
  265. data.setUint8(0, InputEvent.MouseClick);
  266. data.setInt16(1, 1, true);
  267. _videoPlayer && _videoPlayer.sendMsg(data.buffer);
  268. }
  269. }
  270. function sendMouseWheel(e) {
  271. console.log("mouse wheel with delta " + e.wheelDelta);
  272. let data = new DataView(new ArrayBuffer(9));
  273. data.setUint8(0, InputEvent.MouseWheel);
  274. data.setFloat32(1, e.deltaX, true);
  275. data.setFloat32(5, e.deltaY, true);
  276. _videoPlayer && _videoPlayer.sendMsg(data.buffer);
  277. }
  278. }
  279. export function sendClickEvent(videoPlayer, elementId) {
  280. let data = new DataView(new ArrayBuffer(3));
  281. data.setUint8(0, InputEvent.ButtonClick);
  282. data.setInt16(1, elementId, true);
  283. videoPlayer && videoPlayer.sendMsg(data.buffer);
  284. }

-----------------------------------------------------------------------------

再修改Unity的脚本

首先新建一个EventManger.cs的脚本来管理事件 代码如下

  1. using System;
  2. using System.Collections.Generic;
  3. namespace EventManager
  4. {
  5. public delegate void ActionHandler(Message message);
  6. public class Message
  7. {
  8. public float arg1;
  9. public float arg2;
  10. public string message;
  11. public Message(int arg1, int arg2, string message)
  12. {
  13. this.arg1 = arg1;
  14. this.arg2 = arg2;
  15. this.message = message;
  16. }
  17. public Message(float arg1, float arg2, string message)
  18. {
  19. this.arg1 = arg1;
  20. this.arg2 = arg2;
  21. this.message = message;
  22. }
  23. public Message() { }
  24. }
  25. public class EventManager
  26. {
  27. #region Instance
  28. private static EventManager instance;
  29. public static EventManager GetInstance()
  30. {
  31. if (instance == null)
  32. {
  33. instance = new EventManager();
  34. }
  35. return instance;
  36. }
  37. private EventManager() { }
  38. #endregion
  39. private Dictionary<string, ActionHandler> actions = new Dictionary<string, ActionHandler>();
  40. public void AddListener(string actionKey, ActionHandler action)
  41. {
  42. ActionHandler handler;
  43. bool exist = actions.TryGetValue(actionKey, out handler);
  44. if (exist)
  45. {
  46. //避免重复添加
  47. Delegate[] delegates = handler.GetInvocationList();
  48. if (Array.IndexOf(delegates, action) == -1)
  49. {
  50. handler += action;
  51. actions[actionKey] = handler;
  52. }
  53. }
  54. else
  55. {
  56. actions.Add(actionKey, action);
  57. }
  58. }
  59. public void RemoveListener(string actionKey, ActionHandler action)
  60. {
  61. ActionHandler handler;
  62. bool exist = actions.TryGetValue(actionKey, out handler);
  63. if (exist)
  64. {
  65. handler -= action;
  66. if (handler == null)
  67. {
  68. actions.Remove(actionKey);
  69. }
  70. else
  71. {
  72. actions[actionKey] = handler;
  73. }
  74. }
  75. }
  76. public bool BroadcastMessage(string actionKey, Message message)
  77. {
  78. ActionHandler handler;
  79. bool exist = actions.TryGetValue(actionKey, out handler);
  80. if (exist)
  81. {
  82. handler(message);
  83. return true;
  84. }
  85. else
  86. {
  87. return false;
  88. }
  89. }
  90. }
  91. }

同步修改传来值类型的枚举值,在RemoteInput.cs里

  1. enum EventType
  2. {
  3. Keyboard = 0,
  4. MouseMove = 1,
  5. MouseWheel = 2,
  6. Touch = 3,
  7. ButtonClick = 4,
  8. MouseClick = 5,
  9. }

 把ProcessMouseEvent拆成PorcessMouseMove和ProcessMouseClick两个方法

  1. static void ProcessMouseMoveEvent(float x, float y)
  2. {
  3. //var position = new Vector2Int(x, y);
  4. //var delta = position - Mouse.current.position.ReadValue();
  5. EventManager.EventManager.GetInstance().BroadcastMessage("mousemove", new EventManager.Message(x, y, "mouseclick"));
  6. //InputSystem.QueueStateEvent(RemoteMouse, new MouseState { position = position, delta = delta, buttons = button });
  7. }
  8. static void ProcessMouseClickEvent(int state)
  9. {
  10. EventManager.EventManager.GetInstance().BroadcastMessage("mouseclick", new EventManager.Message(state, 0,"mouseclick"));
  11. }

最后再改调用方法,注意 这里只改了Move方法和Click方法

  1. public static void ProcessInput(byte[] bytes)
  2. {
  3. switch ((EventType)bytes[0])
  4. {
  5. case EventType.Keyboard:
  6. var type = (KeyboardEventType)bytes[1];
  7. var repeat = bytes[2] == 1;
  8. var key = bytes[3];
  9. var character = (char)bytes[4];
  10. ProcessKeyEvent(type, repeat, key, character);
  11. InputSystem.Update();
  12. break;
  13. case EventType.MouseMove:
  14. var deltaX = BitConverter.ToSingle(bytes, 1);
  15. var deltaY = BitConverter.ToSingle(bytes, 5);
  16. //var button = bytes[5];
  17. ProcessMouseMoveEvent(deltaX, deltaY);
  18. InputSystem.Update();
  19. break;
  20. case EventType.MouseWheel:
  21. var scrollX = BitConverter.ToSingle(bytes, 1);
  22. var scrollY = BitConverter.ToSingle(bytes, 5);
  23. Debug.Log(scrollX);
  24. ProcessMouseWheelEvent(scrollX, scrollY);
  25. InputSystem.Update();
  26. break;
  27. case EventType.Touch:
  28. var length = bytes[1];
  29. var index = 2;
  30. var touches = new TouchState[length];
  31. for (int i = 0; i < length; i++)
  32. {
  33. var identifier = BitConverter.ToInt32(bytes, index);
  34. index += 4;
  35. var phase = (TouchPhase)bytes[index];
  36. index += 1;
  37. var pageX = BitConverter.ToInt16(bytes, index);
  38. index += 2;
  39. var pageY = BitConverter.ToInt16(bytes, index);
  40. index += 2;
  41. var force = BitConverter.ToSingle(bytes, index);
  42. index += 4;
  43. touches[i] = new TouchState
  44. {
  45. touchId = identifier,
  46. phase = phase,
  47. position = new Vector2Int(pageX, pageY),
  48. pressure = force
  49. };
  50. }
  51. ProcessTouchMoveEvent(touches);
  52. InputSystem.Update();
  53. if (Touchscreen.current.touches.Count > length)
  54. {
  55. ChangeEndStateUnusedTouches(touches);
  56. InputSystem.Update();
  57. }
  58. break;
  59. case EventType.ButtonClick:
  60. var elementId = BitConverter.ToInt16(bytes, 1);
  61. ProcessButtonClickEvent(elementId);
  62. break;
  63. case EventType.MouseClick:
  64. var mouseState = BitConverter.ToInt16(bytes, 1);
  65. ProcessMouseClickEvent(mouseState);
  66. InputSystem.Update();
  67. break;
  68. }
  69. }

此时整个RemoteInput代码如下

  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4. using UnityEngine.InputSystem.LowLevel;
  5. using TouchPhase = UnityEngine.InputSystem.TouchPhase;
  6. namespace Unity.RenderStreaming
  7. {
  8. enum KeyboardEventType
  9. {
  10. KeyUp = 0,
  11. KeyDown = 1,
  12. }
  13. enum EventType
  14. {
  15. Keyboard = 0,
  16. MouseMove = 1,
  17. MouseWheel = 2,
  18. Touch = 3,
  19. ButtonClick = 4,
  20. MouseClick = 5,
  21. }
  22. public static class RemoteInput
  23. {
  24. public static Keyboard Keyboard { get; private set; }
  25. public static Mouse RemoteMouse { get; private set; }
  26. public static Touchscreen Touch { get; private set; }
  27. public static Action<int> ActionButtonClick;
  28. static bool m_isInitialized = false;
  29. static TDevice GetOrAddDevice<TDevice>() where TDevice : InputDevice
  30. {
  31. var device = InputSystem.GetDevice<TDevice>();
  32. if(device != null)
  33. {
  34. return device;
  35. }
  36. return InputSystem.AddDevice<TDevice>();
  37. }
  38. public static void Initialize()
  39. {
  40. Keyboard = GetOrAddDevice<Keyboard>();
  41. RemoteMouse = InputSystem.AddDevice<Mouse>();
  42. Touch = GetOrAddDevice<Touchscreen>();
  43. m_isInitialized = true;
  44. }
  45. //---------------------------------------------------------------------------------------------------------------------
  46. public static void Destroy()
  47. {
  48. InputSystem.RemoveDevice(RemoteMouse);
  49. RemoteMouse = null;
  50. m_isInitialized = false;
  51. }
  52. //---------------------------------------------------------------------------------------------------------------------
  53. public static void ProcessInput(byte[] bytes)
  54. {
  55. switch ((EventType)bytes[0])
  56. {
  57. case EventType.Keyboard:
  58. var type = (KeyboardEventType)bytes[1];
  59. var repeat = bytes[2] == 1;
  60. var key = bytes[3];
  61. var character = (char)bytes[4];
  62. ProcessKeyEvent(type, repeat, key, character);
  63. InputSystem.Update();
  64. break;
  65. case EventType.MouseMove:
  66. var deltaX = BitConverter.ToSingle(bytes, 1);
  67. var deltaY = BitConverter.ToSingle(bytes, 5);
  68. //var button = bytes[5];
  69. ProcessMouseMoveEvent(deltaX, deltaY);
  70. InputSystem.Update();
  71. break;
  72. case EventType.MouseWheel:
  73. var scrollX = BitConverter.ToSingle(bytes, 1);
  74. var scrollY = BitConverter.ToSingle(bytes, 5);
  75. Debug.Log(scrollX);
  76. ProcessMouseWheelEvent(scrollX, scrollY);
  77. InputSystem.Update();
  78. break;
  79. case EventType.Touch:
  80. var length = bytes[1];
  81. var index = 2;
  82. var touches = new TouchState[length];
  83. for (int i = 0; i < length; i++)
  84. {
  85. var identifier = BitConverter.ToInt32(bytes, index);
  86. index += 4;
  87. var phase = (TouchPhase)bytes[index];
  88. index += 1;
  89. var pageX = BitConverter.ToInt16(bytes, index);
  90. index += 2;
  91. var pageY = BitConverter.ToInt16(bytes, index);
  92. index += 2;
  93. var force = BitConverter.ToSingle(bytes, index);
  94. index += 4;
  95. touches[i] = new TouchState
  96. {
  97. touchId = identifier,
  98. phase = phase,
  99. position = new Vector2Int(pageX, pageY),
  100. pressure = force
  101. };
  102. }
  103. ProcessTouchMoveEvent(touches);
  104. InputSystem.Update();
  105. if (Touchscreen.current.touches.Count > length)
  106. {
  107. ChangeEndStateUnusedTouches(touches);
  108. InputSystem.Update();
  109. }
  110. break;
  111. case EventType.ButtonClick:
  112. var elementId = BitConverter.ToInt16(bytes, 1);
  113. ProcessButtonClickEvent(elementId);
  114. break;
  115. case EventType.MouseClick:
  116. var mouseState = BitConverter.ToInt16(bytes, 1);
  117. ProcessMouseClickEvent(mouseState);
  118. InputSystem.Update();
  119. break;
  120. }
  121. }
  122. public static void Reset()
  123. {
  124. if (!m_isInitialized)
  125. return;
  126. InputSystem.QueueStateEvent(RemoteMouse, new MouseState());
  127. InputSystem.QueueStateEvent(Keyboard, new KeyboardState());
  128. InputSystem.QueueStateEvent(Touch, new TouchState());
  129. InputSystem.Update();
  130. }
  131. static void ProcessKeyEvent(KeyboardEventType state, bool repeat, byte keyCode, char character)
  132. {
  133. switch(state)
  134. {
  135. case KeyboardEventType.KeyDown:
  136. if (!repeat)
  137. {
  138. InputSystem.QueueStateEvent(Keyboard, new KeyboardState((Key)keyCode));
  139. }
  140. if(character != 0)
  141. {
  142. InputSystem.QueueTextEvent(Keyboard, character);
  143. }
  144. break;
  145. case KeyboardEventType.KeyUp:
  146. InputSystem.QueueStateEvent(Keyboard, new KeyboardState());
  147. break;
  148. }
  149. }
  150. static void ProcessMouseMoveEvent(float x, float y)
  151. {
  152. //var position = new Vector2Int(x, y);
  153. //var delta = position - Mouse.current.position.ReadValue();
  154. EventManager.EventManager.GetInstance().BroadcastMessage("mousemove", new EventManager.Message(x, y, "mouseclick"));
  155. //InputSystem.QueueStateEvent(RemoteMouse, new MouseState { position = position, delta = delta, buttons = button });
  156. }
  157. static void ProcessMouseClickEvent(int state)
  158. {
  159. EventManager.EventManager.GetInstance().BroadcastMessage("mouseclick", new EventManager.Message(state, 0,"mouseclick"));
  160. }
  161. static void ProcessMouseWheelEvent(float scrollX, float scrollY)
  162. {
  163. InputSystem.QueueStateEvent(RemoteMouse, new MouseState { scroll = new Vector2(scrollX, scrollY) });
  164. }
  165. static void ProcessTouchMoveEvent(TouchState[] touches)
  166. {
  167. for (var i = 0; i < touches.Length; i++)
  168. {
  169. InputSystem.QueueStateEvent(Touch, touches[i]);
  170. }
  171. }
  172. static void ChangeEndStateUnusedTouches(TouchState[] touches)
  173. {
  174. for (var i = 0; i < Touchscreen.current.touches.Count; i++)
  175. {
  176. var touchId = Touchscreen.current.touches[i].touchId.ReadValue();
  177. if (!Array.Exists(touches, v => v.touchId == touchId))
  178. {
  179. InputSystem.QueueStateEvent(Touch, new TouchState
  180. {
  181. touchId = touchId,
  182. phase = TouchPhase.Ended
  183. });
  184. }
  185. }
  186. }
  187. static void ProcessButtonClickEvent(int elementId)
  188. {
  189. if (ActionButtonClick != null)
  190. {
  191. ActionButtonClick(elementId);
  192. }
  193. }
  194. }
  195. }

 最后我们再创建一个Player.cs脚本来接收监听事件,同时处理EventManger传来的事件,此处我们只做平移事件,旋转事件需要进一步的修改WEBAPP中的方法。此处Player脚本需要挂在在场景的一个Object下,同时还要把RemoteCamera的SimpleCameraController的对象传给它才能正常使用,代码如下,

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. using UnityTemplateProjects;
  6. public class Player : MonoBehaviour
  7. {
  8. // Start is called before the first frame update
  9. int mouseState = 0;
  10. public SimpleCameraController cameraController;
  11. void Start()
  12. {
  13. EventManager.EventManager.GetInstance().AddListener("mouseclick", (e) =>
  14. {
  15. mouseState = (int)e.arg1;
  16. });
  17. EventManager.EventManager.GetInstance().AddListener("mousemove", (e) =>
  18. {
  19. if (mouseState == 1)
  20. {
  21. var x = e.arg1;
  22. var y = e.arg2;
  23. Vector3 translation = Vector3.right * x * 0.01f;
  24. translation += Vector3.back * y * 0.01f;
  25. translation *= Mathf.Pow(2.0f, 3.5f);
  26. print(translation);
  27. cameraController.m_TargetCameraState.Translate(translation);
  28. }
  29. });
  30. }
  31. }

四、最后成果

目前就只做了平移功能,目的只是为了简单的跑起来看看云渲染的延迟

Unity云渲染Demo

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/89594
推荐阅读
相关标签
  

闽ICP备14008679号