From 4896d802ac1dcc9ff3d5891b3a98c26e5706aa71 Mon Sep 17 00:00:00 2001 From: Anton Franzluebbers Date: Mon, 15 Aug 2022 12:53:49 -0400 Subject: [PATCH] older velnet changes - seems to be mostly samples --- .github/workflows/npm-publish.yml | 19 ++++ README.md | 2 +- .../Prefabs/TestNetworkedGameObject.prefab | 6 ++ .../Example/Scripts/CustomMessageTest.cs | 29 +++--- .../1.0.4/Example/Scripts/MouseDragger.cs | 59 ++++++------ .../1.0.4/Example/Scripts/NetworkGUI.cs | 4 +- .../1.0.4/Example/Scripts/PlayerController.cs | 3 +- .../VelNet/1.0.4/Example/Scripts/RPCTest.cs | 31 ++++--- .../1.0.4/Example/Scripts/SyncedTextbox.cs | 31 ++++--- .../VelNet/1.0.4/Example/Scripts/VelNetMan.cs | 19 ++-- .../VelNetUnity/Runtime/Util/SyncTransform.cs | 92 +++++++++++++------ .../VelNetUnity/Runtime/VelNetManager.cs | 6 +- .../RiderScriptEditorPersistedState.asset | 2 +- 13 files changed, 191 insertions(+), 112 deletions(-) create mode 100644 .github/workflows/npm-publish.yml diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..a8e94e6 --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,19 @@ +name: Publish to Verdaccio + +on: + push: + branches: + - main + +jobs: + publish-npm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 16 + registry-url: http://npm.ugavel.com/ + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.VERDACCIO_TOKEN}} \ No newline at end of file diff --git a/README.md b/README.md index 247b833..7345482 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Custom networking package for Unity --- -See [VelNetServer](https://github.com/velaboratory/VelNetServer) for description of the server. +See [VelNetServerRust](https://github.com/velaboratory/VelNetServerRust) for description of the server. By contrast to the server, the Unity side is pretty complex. diff --git a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Prefabs/TestNetworkedGameObject.prefab b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Prefabs/TestNetworkedGameObject.prefab index 3be87ea..5efbefe 100644 --- a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Prefabs/TestNetworkedGameObject.prefab +++ b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Prefabs/TestNetworkedGameObject.prefab @@ -29,6 +29,7 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 8565720275311462455} m_RootOrder: 0 @@ -52,6 +53,7 @@ MeshRenderer: m_CastShadows: 1 m_ReceiveShadows: 1 m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 m_MotionVectors: 1 m_LightProbeUsage: 1 m_ReflectionProbeUsage: 1 @@ -123,6 +125,7 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 m_Children: - {fileID: 7099230484513283147} m_Father: {fileID: 0} @@ -162,6 +165,9 @@ MonoBehaviour: networkObject: {fileID: 3951900052977689805} serializationRateHz: 60 hybridOnChangeCompression: 1 + position: 1 + rotation: 1 + scale: 0 useLocalTransform: 0 teleportDistance: 0 teleportAngle: 0 diff --git a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/CustomMessageTest.cs b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/CustomMessageTest.cs index 789e70d..db1574f 100644 --- a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/CustomMessageTest.cs +++ b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/CustomMessageTest.cs @@ -1,22 +1,25 @@ using UnityEngine; using VelNet; -public class CustomMessageTest : MonoBehaviour +namespace VelNetExample { - private void Start() + public class CustomMessageTest : MonoBehaviour { - VelNetManager.OnJoinedRoom += _ => + private void Start() { - byte[] testPacket = { 244 }; - VelNetManager.SendCustomMessage(testPacket, true, true, false); - }; - - VelNetManager.CustomMessageReceived += (senderId, dataWithCategory) => - { - if (dataWithCategory[0] == 244) + VelNetManager.OnJoinedRoom += _ => { - Debug.Log($"Received test packet from {senderId}"); - } - }; + byte[] testPacket = { 244 }; + VelNetManager.SendCustomMessage(testPacket, true, true, false); + }; + + VelNetManager.CustomMessageReceived += (senderId, dataWithCategory) => + { + if (dataWithCategory[0] == 244) + { + Debug.Log($"Received test packet from {senderId}"); + } + }; + } } } \ No newline at end of file diff --git a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/MouseDragger.cs b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/MouseDragger.cs index e77cd8f..2a630f4 100644 --- a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/MouseDragger.cs +++ b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/MouseDragger.cs @@ -1,44 +1,47 @@ using UnityEngine; using VelNet; -public class MouseDragger : MonoBehaviour +namespace VelNetExample { - private Camera cam; - public string[] draggableTags = { "draggable" }; - private NetworkObject draggingObject; - - private void Start() + public class MouseDragger : MonoBehaviour { - cam = Camera.main; - } + private Camera cam; + public string[] draggableTags = { "draggable" }; + private NetworkObject draggingObject; - private void Update() - { - if (Input.GetMouseButtonDown(0)) + private void Start() { - if (Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out RaycastHit hit)) + cam = Camera.main; + } + + private void Update() + { + if (Input.GetMouseButtonDown(0)) { - foreach (string draggableTag in draggableTags) + if (Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out RaycastHit hit)) { - if (hit.transform.CompareTag(draggableTag) || (hit.transform.parent != null && hit.transform.parent.CompareTag(draggableTag))) + foreach (string draggableTag in draggableTags) { - NetworkObject netObj = hit.transform.GetComponent(); - netObj ??= hit.transform.GetComponentInParent(); - if (netObj == null) break; - netObj.TakeOwnership(); - draggingObject = netObj; - break; + if (hit.transform.CompareTag(draggableTag) || (hit.transform.parent != null && hit.transform.parent.CompareTag(draggableTag))) + { + NetworkObject netObj = hit.transform.GetComponent(); + netObj ??= hit.transform.GetComponentInParent(); + if (netObj == null) break; + netObj.TakeOwnership(); + draggingObject = netObj; + break; + } } } } - } - else if (Input.GetMouseButtonUp(0)) - { - draggingObject = null; - } - else if (Input.GetMouseButton(0) && draggingObject != null) - { - draggingObject.transform.position = cam.ScreenPointToRay(Input.mousePosition).direction * Vector3.Distance(draggingObject.transform.position, cam.transform.position) + cam.transform.position; + else if (Input.GetMouseButtonUp(0)) + { + draggingObject = null; + } + else if (Input.GetMouseButton(0) && draggingObject != null) + { + draggingObject.transform.position = cam.ScreenPointToRay(Input.mousePosition).direction * Vector3.Distance(draggingObject.transform.position, cam.transform.position) + cam.transform.position; + } } } } \ No newline at end of file diff --git a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/NetworkGUI.cs b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/NetworkGUI.cs index f938720..13035be 100644 --- a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/NetworkGUI.cs +++ b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/NetworkGUI.cs @@ -3,8 +3,10 @@ using System.Collections.Generic; using Dissonance; using UnityEngine; using UnityEngine.UI; +using VelNet; -namespace VelNet + +namespace VelNetExample { public class NetworkGUI : MonoBehaviour { diff --git a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/PlayerController.cs b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/PlayerController.cs index 50d7b24..2fa853c 100644 --- a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/PlayerController.cs +++ b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/PlayerController.cs @@ -2,9 +2,10 @@ using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; +using VelNet; using Random = UnityEngine.Random; -namespace VelNet +namespace VelNetExample { public class PlayerController : NetworkSerializedObjectStream { diff --git a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/RPCTest.cs b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/RPCTest.cs index ca3de9a..0f71dff 100644 --- a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/RPCTest.cs +++ b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/RPCTest.cs @@ -4,23 +4,26 @@ using System.IO; using UnityEngine; using VelNet; -public class RPCTest : NetworkComponent +namespace VelNetExample { - private void Update() + public class RPCTest : NetworkComponent { - if (Input.GetKeyDown(KeyCode.R)) + private void Update() { - SendRPC(nameof(TestRPC), true); + if (Input.GetKeyDown(KeyCode.R)) + { + SendRPC(nameof(TestRPC), true); + } + } + + private void TestRPC() + { + Debug.Log("RPC RECEIVED!"); + } + + public override void ReceiveBytes(byte[] message) + { + Debug.Log("WOW. BYTES"); } } - - private void TestRPC() - { - Debug.Log("RPC RECEIVED!"); - } - - public override void ReceiveBytes(byte[] message) - { - Debug.Log("WOW. BYTES"); - } } \ No newline at end of file diff --git a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/SyncedTextbox.cs b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/SyncedTextbox.cs index af83c6d..24cd27e 100644 --- a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/SyncedTextbox.cs +++ b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/SyncedTextbox.cs @@ -2,23 +2,26 @@ using System.IO; using UnityEngine.UI; using VelNet; -public class SyncedTextbox : NetworkSerializedObjectStream +namespace VelNetExample { - public InputField text; - - - protected override void SendState(BinaryWriter binaryWriter) + public class SyncedTextbox : NetworkSerializedObjectStream { - binaryWriter.Write(text.text); - } + public InputField text; - protected override void ReceiveState(BinaryReader binaryReader) - { - text.text = binaryReader.ReadString(); - } - public void TakeOwnership() - { - networkObject.TakeOwnership(); + protected override void SendState(BinaryWriter binaryWriter) + { + binaryWriter.Write(text.text); + } + + protected override void ReceiveState(BinaryReader binaryReader) + { + text.text = binaryReader.ReadString(); + } + + public void TakeOwnership() + { + networkObject.TakeOwnership(); + } } } \ No newline at end of file diff --git a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/VelNetMan.cs b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/VelNetMan.cs index 2fa6149..8838577 100644 --- a/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/VelNetMan.cs +++ b/TestVelGameServer/Assets/Samples/VelNet/1.0.4/Example/Scripts/VelNetMan.cs @@ -1,16 +1,19 @@ using UnityEngine; using VelNet; -public class VelNetMan : MonoBehaviour +namespace VelNetExample { - public GameObject playerPrefab; - - // Start is called before the first frame update - private void Start() + public class VelNetMan : MonoBehaviour { - VelNetManager.OnJoinedRoom += player => + public GameObject playerPrefab; + + // Start is called before the first frame update + private void Start() { - VelNetManager.NetworkInstantiate(playerPrefab.name); - }; + VelNetManager.OnJoinedRoom += player => + { + VelNetManager.NetworkInstantiate(playerPrefab.name); + }; + } } } \ No newline at end of file diff --git a/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/SyncTransform.cs b/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/SyncTransform.cs index dddd19f..54b23ea 100644 --- a/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/SyncTransform.cs +++ b/TestVelGameServer/Packages/VelNetUnity/Runtime/Util/SyncTransform.cs @@ -9,12 +9,18 @@ namespace VelNet [AddComponentMenu("VelNet/VelNet Sync Transform")] public class SyncTransform : NetworkSerializedObjectStream { - public bool useLocalTransform; - [Tooltip("0 to disable.")] - public float teleportDistance; - [Tooltip("0 to disable.")] - public float teleportAngle; + [Space] + public bool position = true; + public bool rotation = true; + [Tooltip("Scale is always local")] public bool scale; + [Space] + public bool useLocalTransform; + + [Tooltip("0 to disable.")] public float teleportDistance; + [Tooltip("0 to disable.")] public float teleportAngle; + + private Vector3 targetScale; private Vector3 targetPosition; private Quaternion targetRotation; private float distanceAtReceiveTime; @@ -32,6 +38,8 @@ namespace VelNet targetPosition = transform.position; targetRotation = transform.rotation; } + + targetScale = transform.localScale; } /// @@ -39,8 +47,9 @@ namespace VelNet /// protected override void SendState(BinaryWriter writer) { - writer.Write(transform.localPosition); - writer.Write(transform.localRotation); + if (position) writer.Write(transform.localPosition); + if (rotation) writer.Write(transform.localRotation); + if (scale) writer.Write(transform.localScale); } /// @@ -49,8 +58,9 @@ namespace VelNet /// protected override void ReceiveState(BinaryReader reader) { - targetPosition = reader.ReadVector3(); - targetRotation = reader.ReadQuaternion(); + if (position) targetPosition = reader.ReadVector3(); + if (rotation) targetRotation = reader.ReadQuaternion(); + if (scale) targetScale = reader.ReadVector3(); // record the distance from the target for interpolation if (useLocalTransform) @@ -61,6 +71,7 @@ namespace VelNet { transform.localPosition = targetPosition; } + if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime) { transform.localRotation = targetRotation; @@ -74,6 +85,7 @@ namespace VelNet { transform.position = targetPosition; } + if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime) { transform.rotation = targetRotation; @@ -87,28 +99,52 @@ namespace VelNet if (useLocalTransform) { - transform.localPosition = Vector3.MoveTowards( - transform.localPosition, - targetPosition, - Time.deltaTime * distanceAtReceiveTime * serializationRateHz - ); - transform.localRotation = Quaternion.RotateTowards( - transform.localRotation, - targetRotation, - Time.deltaTime * angleAtReceiveTime * serializationRateHz - ); + if (position) + { + transform.localPosition = Vector3.MoveTowards( + transform.localPosition, + targetPosition, + Time.deltaTime * distanceAtReceiveTime * serializationRateHz + ); + } + + if (rotation) + { + transform.localRotation = Quaternion.RotateTowards( + transform.localRotation, + targetRotation, + Time.deltaTime * angleAtReceiveTime * serializationRateHz + ); + } } else { - transform.position = Vector3.MoveTowards( - transform.position, - targetPosition, - Time.deltaTime * distanceAtReceiveTime * serializationRateHz - ); - transform.rotation = Quaternion.RotateTowards( - transform.rotation, - targetRotation, - Time.deltaTime * angleAtReceiveTime * serializationRateHz + if (position) + { + transform.position = Vector3.MoveTowards( + transform.position, + targetPosition, + Time.deltaTime * distanceAtReceiveTime * serializationRateHz + ); + } + + if (rotation) + { + transform.rotation = Quaternion.RotateTowards( + transform.rotation, + targetRotation, + Time.deltaTime * angleAtReceiveTime * serializationRateHz + ); + } + } + + + if (scale) + { + transform.localScale = Vector3.Lerp( + transform.localScale, + targetScale, + Time.deltaTime * serializationRateHz ); } } diff --git a/TestVelGameServer/Packages/VelNetUnity/Runtime/VelNetManager.cs b/TestVelGameServer/Packages/VelNetUnity/Runtime/VelNetManager.cs index b8d8761..bf12ae2 100644 --- a/TestVelGameServer/Packages/VelNetUnity/Runtime/VelNetManager.cs +++ b/TestVelGameServer/Packages/VelNetUnity/Runtime/VelNetManager.cs @@ -1053,8 +1053,8 @@ namespace VelNet { VelNetLogger.Error("Room name is null. Can't get info for this room."); return; - } - + } + MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); @@ -1206,7 +1206,7 @@ namespace VelNet NetworkObject prefab = instance.prefabs.Find(p => p.name == prefabName); if (prefab == null) { - VelNetLogger.Error("Couldn't find a prefab with that name: " + prefabName); + VelNetLogger.Error("Couldn't find a prefab with that name: " + prefabName + "\nMake sure to add the prefab to list of prefabs in VelNetManager"); return null; } diff --git a/TestVelGameServer/ProjectSettings/RiderScriptEditorPersistedState.asset b/TestVelGameServer/ProjectSettings/RiderScriptEditorPersistedState.asset index 4531f5c..04817fe 100644 --- a/TestVelGameServer/ProjectSettings/RiderScriptEditorPersistedState.asset +++ b/TestVelGameServer/ProjectSettings/RiderScriptEditorPersistedState.asset @@ -12,4 +12,4 @@ MonoBehaviour: m_Script: {fileID: 0} m_Name: m_EditorClassIdentifier: Unity.Rider.Editor:Packages.Rider.Editor:RiderScriptEditorPersistedState - lastWriteTicks: -8585433455978485828 + lastWriteTicks: -8585432596162049605