From 988f39144d2349f0bfe64965296a1180398992ed Mon Sep 17 00:00:00 2001 From: APEX FIGHT Date: Thu, 2 Jan 2025 16:21:58 -0500 Subject: [PATCH] CSNC script --- Client/Unity/CSNC.cs | 99 +++++++++++++++++++++++++++++++++++++++ Client/Unity/CSNC.cs.meta | 11 +++++ 2 files changed, 110 insertions(+) create mode 100644 Client/Unity/CSNC.cs create mode 100644 Client/Unity/CSNC.cs.meta diff --git a/Client/Unity/CSNC.cs b/Client/Unity/CSNC.cs new file mode 100644 index 0000000..da5776c --- /dev/null +++ b/Client/Unity/CSNC.cs @@ -0,0 +1,99 @@ +using Newtonsoft.Json; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Networking; + +public class CSNC : MonoBehaviour +{ + public string ip; + private class EZtransform //ez consistent serialization of transforms + { + public List position; + public List rotation; + + public EZtransform(Transform transform) + { + position = new List(3); + rotation = new List(4); + + position.Add(transform.position.x); + position.Add(transform.position.y); + position.Add(transform.position.z); + + rotation.Add(transform.rotation.x); + rotation.Add(transform.rotation.y); + rotation.Add(transform.rotation.z); + rotation.Add(transform.rotation.w); + } + + } + private class CSNCObject + { + public string type; + public EZtransform transform; + + public CSNCObject(string type, Transform trans) + { + this.type = type; + this.transform = new EZtransform(trans); + } + + public CSNCObject(string type, EZtransform trans) + { + this.type = type; + this.transform = trans; + } + } + private class SendData + { + public string uuid; + public List gameObjects; + public SendData(List gameobjects) + { + uuid = GameObjectRegistry.instance.uuid; + gameObjects = gameobjects; + } + } + + + void Start() + { + ip = CHANGEME; //CHANGE THIS VALUE to the ip address + } + + void FixedUpdate() + { + IEnumerator req = request(); + StartCoroutine(req); + } + IEnumerator request() + { + //Debug.Log(GameObjectRegistry.instance.registeredObjects); + List gameObjects = new List(); + + foreach (SyncData obj in GameObjectRegistry.instance.registeredObjects) + { + gameObjects.Add(new CSNCObject(obj.type, obj.transform)); + } + + SendData sendData = new SendData(gameObjects); + //Debug.Log(JsonConvert.SerializeObject(sendData)); + using (UnityWebRequest req = UnityWebRequest.Post(ip, JsonConvert.SerializeObject(sendData), "application/json")) + { + req.SetRequestHeader("request-type", "csnc"); + yield return req.SendWebRequest(); + + if (req.result != UnityWebRequest.Result.Success) + { + Debug.LogError(req.error); + } + else + { + + Debug.Log(req.downloadHandler.text); + + } + } + } +} diff --git a/Client/Unity/CSNC.cs.meta b/Client/Unity/CSNC.cs.meta new file mode 100644 index 0000000..8f732c1 --- /dev/null +++ b/Client/Unity/CSNC.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6772f36d5b93fd5498e8f3f1d8155ff2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: