Compare commits
12 Commits
08db445a53
...
ef941cd344
Author | SHA1 | Date | |
---|---|---|---|
![]() |
ef941cd344 | ||
![]() |
0810be6809 | ||
![]() |
7d799457f6 | ||
![]() |
2a445343a2 | ||
![]() |
af9e800716 | ||
![]() |
72113dc2f2 | ||
![]() |
a83220ea7c | ||
![]() |
3faaba9d0c | ||
![]() |
3a30091559 | ||
![]() |
51132ceb5c | ||
![]() |
258f299fa0 | ||
![]() |
a06db78d42 |
@ -7,8 +7,11 @@ using UnityEngine.Networking;
|
|||||||
public class CSNC : MonoBehaviour
|
public class CSNC : MonoBehaviour
|
||||||
{
|
{
|
||||||
public string ip;
|
public string ip;
|
||||||
private class EZtransform //ez consistent serialization of transforms
|
public GameObjectManager manager;
|
||||||
|
|
||||||
|
public class EZtransform //ez consistent serialization of transforms
|
||||||
{
|
{
|
||||||
|
|
||||||
public List<float> position;
|
public List<float> position;
|
||||||
public List<float> rotation;
|
public List<float> rotation;
|
||||||
|
|
||||||
@ -26,26 +29,38 @@ public class CSNC : MonoBehaviour
|
|||||||
rotation.Add(transform.rotation.z);
|
rotation.Add(transform.rotation.z);
|
||||||
rotation.Add(transform.rotation.w);
|
rotation.Add(transform.rotation.w);
|
||||||
}
|
}
|
||||||
|
public EZtransform()
|
||||||
|
{
|
||||||
|
position = new List<float>();
|
||||||
|
rotation = new List<float>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
private class CSNCObject
|
public class CSNCObject
|
||||||
{
|
{
|
||||||
|
public string id;
|
||||||
public string type;
|
public string type;
|
||||||
public EZtransform transform;
|
public EZtransform transform;
|
||||||
|
public CSNCObject()
|
||||||
public CSNCObject(string type, Transform trans)
|
|
||||||
{
|
{
|
||||||
|
id = "";
|
||||||
|
type = "";
|
||||||
|
transform = new EZtransform();
|
||||||
|
}
|
||||||
|
public CSNCObject(string type, Transform trans, string id)
|
||||||
|
{
|
||||||
|
this.id = id;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.transform = new EZtransform(trans);
|
this.transform = new EZtransform(trans);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CSNCObject(string type, EZtransform trans)
|
public CSNCObject(string type, EZtransform trans, string id)
|
||||||
{
|
{
|
||||||
|
this.id = id;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.transform = trans;
|
this.transform = trans;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private class SendData
|
public class SendData
|
||||||
{
|
{
|
||||||
public string uuid;
|
public string uuid;
|
||||||
public List<CSNCObject> gameObjects;
|
public List<CSNCObject> gameObjects;
|
||||||
@ -74,7 +89,7 @@ public class CSNC : MonoBehaviour
|
|||||||
|
|
||||||
foreach (SyncData obj in GameObjectRegistry.instance.registeredObjects)
|
foreach (SyncData obj in GameObjectRegistry.instance.registeredObjects)
|
||||||
{
|
{
|
||||||
gameObjects.Add(new CSNCObject(obj.type, obj.transform));
|
gameObjects.Add(new CSNCObject(obj.type, obj.transform, obj.id));
|
||||||
}
|
}
|
||||||
|
|
||||||
SendData sendData = new SendData(gameObjects);
|
SendData sendData = new SendData(gameObjects);
|
||||||
@ -90,9 +105,7 @@ public class CSNC : MonoBehaviour
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
manager.SyncObjects(JsonConvert.DeserializeObject<GameObjectManager.RecievedData>(req.downloadHandler.text));
|
||||||
Debug.Log(req.downloadHandler.text);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ GameObject:
|
|||||||
- component: {fileID: 7121321073155658785}
|
- component: {fileID: 7121321073155658785}
|
||||||
- component: {fileID: 7629741995735410931}
|
- component: {fileID: 7629741995735410931}
|
||||||
- component: {fileID: 5596437124550187285}
|
- component: {fileID: 5596437124550187285}
|
||||||
|
- component: {fileID: 1768507704600374837}
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: CSNC
|
m_Name: CSNC
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
@ -45,6 +46,9 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: 1c20e30fdef1b0841aa0fdb48748a6a6, type: 3}
|
m_Script: {fileID: 11500000, guid: 1c20e30fdef1b0841aa0fdb48748a6a6, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
|
uuid:
|
||||||
|
gameObjects: []
|
||||||
|
ids: []
|
||||||
--- !u!114 &5596437124550187285
|
--- !u!114 &5596437124550187285
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@ -57,3 +61,17 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: 6772f36d5b93fd5498e8f3f1d8155ff2, type: 3}
|
m_Script: {fileID: 11500000, guid: 6772f36d5b93fd5498e8f3f1d8155ff2, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
|
ip:
|
||||||
|
manager: {fileID: 1768507704600374837}
|
||||||
|
--- !u!114 &1768507704600374837
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 9009233966360587498}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 7f3dbff6bcfe57f4fb9ba717c6fecc0e, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
99
Client/Unity/GameObjectManager.cs
Normal file
99
Client/Unity/GameObjectManager.cs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class GameObjectManager : MonoBehaviour
|
||||||
|
{
|
||||||
|
|
||||||
|
/*
|
||||||
|
This MonoBehaviour Synchronizes the position of All the game objects recieved from other clients
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
// ID GAMEOBJECT+TYPE
|
||||||
|
Dictionary<string, GameObjectDataWrapper> inSceneGameObjects = new Dictionary<string, GameObjectDataWrapper>();
|
||||||
|
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RecievedData
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
{
|
||||||
|
"gameObjects": //list of CSNCObjects
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": "uuid here",
|
||||||
|
"type": "razorblade",
|
||||||
|
"transform": {
|
||||||
|
"position": [
|
||||||
|
0.0,
|
||||||
|
0.53,
|
||||||
|
0.0
|
||||||
|
],
|
||||||
|
"rotation": [
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
1.0
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
public RecievedData(List<CSNC.CSNCObject> gameObjects)
|
||||||
|
{
|
||||||
|
this.gameObjects = gameObjects;
|
||||||
|
}
|
||||||
|
public List<CSNC.CSNCObject> gameObjects;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GameObjectDataWrapper
|
||||||
|
{
|
||||||
|
public GameObject gameObject;
|
||||||
|
public string type;
|
||||||
|
public GameObjectDataWrapper(string type, GameObject gameObject)
|
||||||
|
{
|
||||||
|
this.type = type;
|
||||||
|
this.gameObject = gameObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void SyncObjects(RecievedData data)
|
||||||
|
{
|
||||||
|
|
||||||
|
//Debug.Log(JsonConvert.SerializeObject(data));
|
||||||
|
foreach (CSNC.CSNCObject obj in data.gameObjects)
|
||||||
|
{
|
||||||
|
if (inSceneGameObjects.ContainsKey(obj.id)) //if in registry update
|
||||||
|
{
|
||||||
|
Quaternion rot = new Quaternion(obj.transform.rotation[0], obj.transform.rotation[1], obj.transform.rotation[2], obj.transform.rotation[3]);
|
||||||
|
Debug.Log(JsonConvert.SerializeObject(obj.transform.position));
|
||||||
|
Vector3 pos = new Vector3(obj.transform.position[0], obj.transform.position[1], obj.transform.position[2]);
|
||||||
|
IEnumerator cor = LerpGameObjectFromFixedUpdate(inSceneGameObjects[obj.id].gameObject, rot, pos);
|
||||||
|
StartCoroutine(cor);
|
||||||
|
|
||||||
|
} else //otherwise add to registry
|
||||||
|
{
|
||||||
|
inSceneGameObjects.Add(obj.id, new GameObjectDataWrapper(obj.type,Instantiate(GameObjectRegistry.instance.registry[obj.type]))); //Dubious ahh line of code
|
||||||
|
|
||||||
|
GameObject g = inSceneGameObjects[obj.id].gameObject;
|
||||||
|
g.SetActive(true);
|
||||||
|
g.transform.position = new Vector3(obj.transform.position[0], obj.transform.position[1], obj.transform.position[2]); //these 2 might be even more dubious than the last
|
||||||
|
g.transform.rotation = new Quaternion(obj.transform.rotation[0], obj.transform.rotation[1], obj.transform.rotation[2], obj.transform.rotation[3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
IEnumerator LerpGameObjectFromFixedUpdate(GameObject obj, Quaternion rot, Vector3 pos)
|
||||||
|
{
|
||||||
|
obj.transform.position = pos;
|
||||||
|
obj.transform.rotation = rot;
|
||||||
|
|
||||||
|
yield return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
11
Client/Unity/GameObjectManager.cs.meta
Normal file
11
Client/Unity/GameObjectManager.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7f3dbff6bcfe57f4fb9ba717c6fecc0e
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -8,11 +8,12 @@ public class GameObjectRegistry : MonoBehaviour
|
|||||||
public static GameObjectRegistry instance; //instance of gameobject registry in scene
|
public static GameObjectRegistry instance; //instance of gameobject registry in scene
|
||||||
public string uuid; //unique identifier for this client
|
public string uuid; //unique identifier for this client
|
||||||
public List<GameObject> gameObjects; //list to make it easy to register new gameobjects
|
public List<GameObject> gameObjects; //list to make it easy to register new gameobjects
|
||||||
public List<string> ids;
|
public List<string> types;
|
||||||
|
|
||||||
|
|
||||||
public Dictionary<string, GameObject> registry = new Dictionary<string, GameObject>(); //registry of game objects that can be synced (needs to be identical on both clients
|
public Dictionary<string, GameObject> registry = new Dictionary<string, GameObject>(); //registry of game objects that can be synced (needs to be identical on both clients
|
||||||
public List<SyncData> registeredObjects = new List<SyncData>(); //game objcets that are currently being synced
|
public List<SyncData> registeredObjects = new List<SyncData>(); //game objcets that are currently being synced
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
GameObjectRegistry.instance = this;
|
GameObjectRegistry.instance = this;
|
||||||
@ -25,7 +26,7 @@ public class GameObjectRegistry : MonoBehaviour
|
|||||||
void initializeRegistry()
|
void initializeRegistry()
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
foreach (string id in ids)
|
foreach (string id in types)
|
||||||
{
|
{
|
||||||
registry.Add(id, gameObjects[i]);
|
registry.Add(id, gameObjects[i]);
|
||||||
i++;
|
i++;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# Unity Setup
|
# Unity Setup
|
||||||
First, set the ip of the csnc compatible server.
|
First, set the ip of the csnc compatible server.
|
||||||
|
(it needs to be formatted as such http://192.51.100.123:1234/ or http://mydomain.tld:1234/)
|
||||||

|

|
||||||
|
|
||||||
Then schedule the GameObjectRegistry script to be\
|
Then schedule the GameObjectRegistry script to be\
|
||||||
|
@ -6,9 +6,12 @@ public class SyncData //structure for gameobjects and data sent over to the serv
|
|||||||
{
|
{
|
||||||
public SyncData(Transform pos, string type)
|
public SyncData(Transform pos, string type)
|
||||||
{
|
{
|
||||||
|
this.id = System.Guid.NewGuid().ToString();
|
||||||
this.transform = pos;
|
this.transform = pos;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Transform transform;
|
public Transform transform;
|
||||||
|
public string id;
|
||||||
public string type; //key in registry data that defines which gameobject to instantiate with transform: position
|
public string type; //key in registry data that defines which gameobject to instantiate with transform: position
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user