Created
December 17, 2017 13:06
-
-
Save tarukosu/5694ffe33a0234d3f110eaa7786645bb to your computer and use it in GitHub Desktop.
UnetTangoController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using UnetLLAPISample; | |
using UnityEngine; | |
using UnityEngine.Networking; | |
public class UnetTangoController : MonoBehaviour | |
{ | |
public LLAPINetworkManager NetworkManager; | |
public bool SelectButton = false; | |
public void Start() | |
{ | |
StartCoroutine(SendTransformCoroutine()); | |
} | |
private IEnumerator SendTransformCoroutine() | |
{ | |
while (true) | |
{ | |
SendTransform(); | |
yield return new WaitForSeconds(0.06f); | |
} | |
} | |
public void Update() | |
{ | |
bool currentSelectButton = Input.GetKey("space"); | |
if (Input.touchCount > 0) | |
{ | |
currentSelectButton = true; | |
} | |
if (Input.GetMouseButton(0)) | |
{ | |
currentSelectButton = true; | |
} | |
if (SelectButton != currentSelectButton) | |
{ | |
SelectButton = currentSelectButton; | |
SendInput(SelectButton); | |
} | |
} | |
void SendTransform() | |
{ | |
int headerSize = 1; | |
byte[] buff = new byte[headerSize + sizeof(float) * 7]; | |
buff[0] = 0; | |
var pos = Camera.main.transform.position; | |
Buffer.BlockCopy(BitConverter.GetBytes(pos.x), 0, buff, headerSize + 0 * sizeof(float), sizeof(float)); | |
Buffer.BlockCopy(BitConverter.GetBytes(pos.y), 0, buff, headerSize + 1 * sizeof(float), sizeof(float)); | |
Buffer.BlockCopy(BitConverter.GetBytes(pos.z), 0, buff, headerSize + 2 * sizeof(float), sizeof(float)); | |
var rot = Camera.main.transform.rotation; | |
Buffer.BlockCopy(BitConverter.GetBytes(rot.x), 0, buff, headerSize + 3 * sizeof(float), sizeof(float)); | |
Buffer.BlockCopy(BitConverter.GetBytes(rot.y), 0, buff, headerSize + 4 * sizeof(float), sizeof(float)); | |
Buffer.BlockCopy(BitConverter.GetBytes(rot.z), 0, buff, headerSize + 5 * sizeof(float), sizeof(float)); | |
Buffer.BlockCopy(BitConverter.GetBytes(rot.w), 0, buff, headerSize + 6 * sizeof(float), sizeof(float)); | |
NetworkManager.SendPacketData(buff, QosType.Unreliable); | |
} | |
void SendInput(bool select) | |
{ | |
byte[] buff = { 1, BitConverter.GetBytes(select)[0] }; | |
NetworkManager.SendPacketData(buff, QosType.Reliable); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment