From 9346895cbb71b7ae7bbb4daeb417e0dca3e72502 Mon Sep 17 00:00:00 2001 From: p8qk6wvz2 <3051841343@qq.com> Date: Fri, 27 Oct 2023 22:57:36 +0800 Subject: [PATCH] ADD file via upload --- 新建 文本文档.txt | 145 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 新建 文本文档.txt diff --git a/新建 文本文档.txt b/新建 文本文档.txt new file mode 100644 index 0000000..c3973c9 --- /dev/null +++ b/新建 文本文档.txt @@ -0,0 +1,145 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.XR.Interaction.Toolkit; + +public class SyncTransform : MonoBehaviour +{ + private XRGrabInteractable xRGrab; + private SyncTransformData syncTransformData; + private OriginTransformData originTransformData; + public SyncTransformData SyncTransformData + + + { + get => syncTransformData; + } + public int id = 0; + private Queue syncQueue = new Queue(); + public Queue SyncQueue + { + get => syncQueue; + set => syncQueue = value; + } + private void OnValidate() + { + + syncTransformData.pos = transform.position; + syncTransformData.rot = transform.eulerAngles; + syncTransformData.size = transform.localScale; + syncTransformData.ID = id; + syncTransformData.isGrip = false; + } + private void Awake() + { + SyncManager.SyncTransformList.Add(this); + } + private void Start() + { + + xRGrab = GetComponent(); + StartCoroutine(SyncTrans()); + originTransformData.pos = transform.position; + originTransformData.rot = transform.eulerAngles; + originTransformData.size = transform.localScale; + originTransformData.ID = id; + Debug.Log(originTransformData.pos); + } + + private void Update() + { + UpdateSyncTrans(); + } + + private void UpdateSyncTrans() + { + if (xRGrab!=null) + { + if (xRGrab.isSelected) + { + syncTransformData.isGrip = true; + syncTransformData.time = GetTimeStamp(); + } + else + { + syncTransformData.isGrip = false; + } + syncTransformData.pos = transform.position; + syncTransformData.rot = transform.eulerAngles; + syncTransformData.size = transform.localScale; + syncTransformData.ID = id; + + } + } + private long GetTimeStamp() + { + DateTime currentTime = DateTime.Now; + long unixTimestampSeconds = (long)(currentTime - new DateTime(1970, 1, 1)).TotalSeconds; + return unixTimestampSeconds; + } + + private IEnumerator SyncTrans() + { + while (true) + { + while (syncQueue.Count>0) + { + SyncTransformData data = syncQueue.Dequeue(); + Debug.Log("data" + data.time); + if (data.time>= syncTransformData.time) + { + xRGrab.enabled = false; + float elapsedTime = 0f; + Vector3 startingPosition = transform.position; + Quaternion startRotation = transform.rotation; + while (elapsedTime < SyncManager.syncTime) + { + elapsedTime += Time.deltaTime; + float t = Mathf.Clamp01(elapsedTime / SyncManager.syncTime); // ȡ[0, 1]֮ IJ ֵ + transform.position = Vector3.Lerp(startingPosition, data.pos, t); // ʹ Բ ֵƽ ƶ + transform.rotation = Quaternion.Lerp(startRotation, Quaternion.Euler(data.rot), elapsedTime / SyncManager.syncTime); + yield return null; + } + transform.position = data.pos; + transform.eulerAngles = data.rot; + transform.localScale = data.size; + syncTransformData.time = data.time; + + } + } + xRGrab.enabled = true; + yield return null; + } + } + +} +public struct OriginTransformData +{ + public Vector3 pos; + public Vector3 rot; + public Vector3 size; + public int ID; +} + + +[System.Serializable] +public struct SyncTransformData +{ + public Vector3 pos; + public Vector3 rot; + public Vector3 size; + public bool isGrip; + public int ID; + public long time; +} +[System.Serializable] +public struct SyncTransformDatas +{ + public List syncTransforms; +} +[System.Serializable] +public struct SyncMessage +{ + public List Message; +} \ No newline at end of file