diff --git a/Runtime/ArenaCamera.cs b/Runtime/ArenaCamera.cs index 56f58e9..05f9d43 100644 --- a/Runtime/ArenaCamera.cs +++ b/Runtime/ArenaCamera.cs @@ -14,7 +14,8 @@ namespace ArenaUnity [DisallowMultipleComponent] public class ArenaCamera : PrettyObject { - private const float avatarPublishIntervalSeconds = 1f; + private const float cameraKeepAliveInterval = 1f; // 1 second + private float publishInterval; // varies private string messageType = "object"; private bool persist = false; @@ -41,36 +42,34 @@ public void OnEnable() void Start() { displayColor = ArenaUnity.ColorRandom(); - StartCoroutine(CameraUpdater()); + StartCoroutine(PublishTickThrottle()); } - IEnumerator CameraUpdater() + IEnumerator PublishTickThrottle() { while (true) { if (userid != null && camid != null) { + // send more frequently when changed, otherwise minimum 1 second keep alive + if (transform.hasChanged && ArenaClientScene.Instance) + { + publishInterval = ((float)ArenaClientScene.Instance.camUpdateIntervalMs / 1000f); + transform.hasChanged = false; + } + else + { + publishInterval = cameraKeepAliveInterval; + } PublishCreateUpdate(); } - yield return new WaitForSeconds(avatarPublishIntervalSeconds); + yield return new WaitForSeconds(publishInterval); } } void Update() { - // send only when changed, each publishInterval frames, or stop at 0 frames - if (!ArenaClientScene.Instance || ArenaClientScene.Instance.transformPublishInterval == 0 || - Time.frameCount % ArenaClientScene.Instance.transformPublishInterval != 0) - return; - if (transform.hasChanged && userid != null && camid != null) - { - //TODO: prevent child objects of parent.transform.hasChanged = true from publishing unnecessarily - - if (PublishCreateUpdate()) - { - transform.hasChanged = false; - } - } + // let CameraTickThrottle handle publish frequency } public bool PublishCreateUpdate() diff --git a/Runtime/ArenaClientScene.cs b/Runtime/ArenaClientScene.cs index 4b9b4e2..33ce0e9 100644 --- a/Runtime/ArenaClientScene.cs +++ b/Runtime/ArenaClientScene.cs @@ -59,9 +59,9 @@ protected override void Awake() public bool logMqttEvents = false; [Tooltip("Console log MQTT non-persist messages")] public bool logMqttNonPersist = false; - [Tooltip("Publish per frames frequency to publish detected transform changes (0 to stop)")] - [Range(0, 60)] - public int transformPublishInterval = 30; // in publish per frames + [Tooltip("Publish interval frequency to publish detected transform changes (milliseconds)")] + [Range(100, 1000)] + public int camUpdateIntervalMs = 100; /// /// Browser URL for the scene. diff --git a/Runtime/ArenaObject.cs b/Runtime/ArenaObject.cs index 659b1f7..1ba5df2 100644 --- a/Runtime/ArenaObject.cs +++ b/Runtime/ArenaObject.cs @@ -36,6 +36,7 @@ public class ArenaObject : PrettyObject internal bool Created { get { return created; } set { created = value; } } + private float publishInterval; // varies private bool created = false; private string oldName; // test for rename internal bool externalDelete = false; @@ -68,8 +69,10 @@ void Start() } isJsonValidated = jsonData != null; + StartCoroutine(PublishTickThrottle()); } + public void SetTtlDeleteTimer(float seconds) { StartCoroutine(TtlUpdater(seconds)); @@ -82,26 +85,31 @@ IEnumerator TtlUpdater(float seconds) Destroy(gameObject); } - void Update() + IEnumerator PublishTickThrottle() { - // send only when changed, each publishInterval frames, or stop at 0 frames - if (!ArenaClientScene.Instance || ArenaClientScene.Instance.transformPublishInterval == 0 || - Time.frameCount % ArenaClientScene.Instance.transformPublishInterval != 0) - return; + //TODO: prevent child objects of parent.transform.hasChanged = true from publishing unnecessarily - HasPermissions = ArenaClientScene.Instance.sceneObjectRights; - - if (transform.hasChanged || meshChanged) + while (true) { - //TODO: prevent child objects of parent.transform.hasChanged = true from publishing unnecessarily - - if (PublishCreateUpdate(true)) + // send only when changed, each publishInterval + if ((transform.hasChanged || meshChanged) && ArenaClientScene.Instance) { - transform.hasChanged = false; - meshChanged = false; + publishInterval = ((float)ArenaClientScene.Instance.camUpdateIntervalMs / 1000f); + if (PublishCreateUpdate(true)) + { + transform.hasChanged = false; + meshChanged = false; + } } + yield return new WaitForSeconds(publishInterval); } - else if (oldName != null && name != oldName) + } + + void Update() + { + HasPermissions = ArenaClientScene.Instance.sceneObjectRights; + + if (oldName != null && name != oldName) { HandleRename(); }