From 2328316d7d59083c24363ca166f524a1d791398d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Hodossy?= Date: Fri, 18 Nov 2022 11:23:41 +0000 Subject: [PATCH 1/2] Add synchronisation events to MjScene --- unity/Runtime/Components/MjScene.cs | 30 +++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/unity/Runtime/Components/MjScene.cs b/unity/Runtime/Components/MjScene.cs index 0ecb890a..e3314371 100644 --- a/unity/Runtime/Components/MjScene.cs +++ b/unity/Runtime/Components/MjScene.cs @@ -76,6 +76,10 @@ public class MjScene : MonoBehaviour { private List _orderedComponents; + public EventHandler preUpdateEvent; + public EventHandler ctrlCallback; + public EventHandler postUpdateEvent; + protected unsafe void Start() { CreateScene(); } @@ -85,7 +89,9 @@ public class MjScene : MonoBehaviour { } protected unsafe void FixedUpdate() { + preUpdateEvent?.Invoke(this, EventArgs.Empty); StepScene(); + postUpdateEvent?.Invoke(this, EventArgs.Empty); } public bool SceneRecreationAtLateUpdateRequested = false; @@ -314,7 +320,14 @@ public class MjScene : MonoBehaviour { } Profiler.BeginSample("MjStep"); Profiler.BeginSample("MjStep.mj_step"); - MujocoLib.mj_step(Model, Data); + if (ctrlCallback != null){ + MujocoLib.mj_step1(Model, Data); + ctrlCallback?.Invoke(this, new MjStepArgs(Model, Data)); + MujocoLib.mj_step2(Model, Data); + } + else { + MujocoLib.mj_step(Model, Data); + } Profiler.EndSample(); // MjStep.mj_step CheckForPhysicsException(); @@ -447,5 +460,18 @@ public class MjScene : MonoBehaviour { Debug.LogWarning("Failed to save Xml to a file: " + ex.ToString(), this); } } + + } -} + + public class MjStepArgs : EventArgs + { + public unsafe MjStepArgs(MujocoLib.mjModel_* model, MujocoLib.mjData_* data){ + this.model = model; + this.data = data; + } + public readonly unsafe MujocoLib.mjModel_* model; + public readonly unsafe MujocoLib.mjData_* data; + } + +} \ No newline at end of file From 1fdbfa7061f439d1dedf6caeae106407c57dbbf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Hodossy?= Date: Fri, 18 Nov 2022 11:25:08 +0000 Subject: [PATCH 2/2] Add MjScene property to check if scene has initialized --- unity/Runtime/Components/MjScene.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/unity/Runtime/Components/MjScene.cs b/unity/Runtime/Components/MjScene.cs index e3314371..bdc720c6 100644 --- a/unity/Runtime/Components/MjScene.cs +++ b/unity/Runtime/Components/MjScene.cs @@ -62,6 +62,8 @@ public class MjScene : MonoBehaviour { return _instance; } } + + public static bool InstanceExists { get => _instance != null; } public void Awake() { if (_instance == null) {