diff --git a/com.unity.robotics.ros-tcp-connector/CHANGELOG.md b/com.unity.robotics.ros-tcp-connector/CHANGELOG.md index f883a80f..d080d180 100644 --- a/com.unity.robotics.ros-tcp-connector/CHANGELOG.md +++ b/com.unity.robotics.ros-tcp-connector/CHANGELOG.md @@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## Unreleased +Clear static data on destroy + ### Upgrade Notes ### Known Issues diff --git a/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/HudPanel.cs b/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/HudPanel.cs index 3725620e..87d0c2bb 100644 --- a/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/HudPanel.cs +++ b/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/HudPanel.cs @@ -22,7 +22,8 @@ public class HudPanel : MonoBehaviour static SortedList s_HUDTabs = new SortedList(); static SortedList s_HeaderContents = new SortedList(); static List s_ActiveWindows = new List(); - static int s_NextWindowID = 101; + const int INITIAL_NEXT_WINDOW_ID = 101; + static int s_NextWindowID = INITIAL_NEXT_WINDOW_ID; // ROS Message variables internal bool isEnabled; @@ -123,6 +124,13 @@ public static void RegisterHeader(Action headerContent, int index = 0) s_HeaderContents.Add(index, headerContent); } + public static void ClearStaticContent() + { + s_HUDTabs.Clear(); + s_HeaderContents.Clear(); + s_ActiveWindows.Clear(); + s_NextWindowID = INITIAL_NEXT_WINDOW_ID; + } public static void AddWindow(HudWindow window) { @@ -204,24 +212,5 @@ public bool IsFreeWindowRect(Rect rect, out float maxX, out float maxY) return result; } - -#if UNITY_EDITOR - // automatically clear all static content on pressing play - [UnityEditor.InitializeOnLoadMethod] - static void InitializeOnLoad() - { - UnityEditor.EditorApplication.playModeStateChanged += OnPlayMode; - } - - static void OnPlayMode(UnityEditor.PlayModeStateChange change) - { - if (change == UnityEditor.PlayModeStateChange.ExitingEditMode) - { - s_HUDTabs.Clear(); - s_HeaderContents.Clear(); - s_ActiveWindows.Clear(); - } - } -#endif } } diff --git a/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/ROSConnection.cs b/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/ROSConnection.cs index bf85d56c..8c252973 100644 --- a/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/ROSConnection.cs +++ b/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/ROSConnection.cs @@ -491,6 +491,11 @@ void Start() if (ConnectOnStart) Connect(); } + void OnDestroy() + { + HudPanel.ClearStaticContent(); + Disconnect(); + } public void Connect(string ipAddress, int port) { diff --git a/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/TFSystem.cs b/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/TFSystem.cs index af4fd483..689770e1 100644 --- a/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/TFSystem.cs +++ b/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/TFSystem.cs @@ -129,6 +129,14 @@ public static TFSystem GetOrCreateInstance() return instance; } + public static void ClearStaticData() + { + if (instance != null) + { + instance.m_TFTopics.Clear(); + instance = null; + } + } public IEnumerable GetTransformNames(string tfTopic = "/tf") { return GetOrCreateTFTopic(tfTopic).GetTransformNames(); diff --git a/com.unity.robotics.visualizations/CHANGELOG.md b/com.unity.robotics.visualizations/CHANGELOG.md index 412bba73..7ab9a132 100644 --- a/com.unity.robotics.visualizations/CHANGELOG.md +++ b/com.unity.robotics.visualizations/CHANGELOG.md @@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## Unreleased +Clear static data on destroy + ### Upgrade Notes ### Known Issues diff --git a/com.unity.robotics.visualizations/Runtime/DefaultVisualizers/TFSystemVisualizer.cs b/com.unity.robotics.visualizations/Runtime/DefaultVisualizers/TFSystemVisualizer.cs index 7b5d201f..d8058f91 100644 --- a/com.unity.robotics.visualizations/Runtime/DefaultVisualizers/TFSystemVisualizer.cs +++ b/com.unity.robotics.visualizations/Runtime/DefaultVisualizers/TFSystemVisualizer.cs @@ -20,6 +20,10 @@ public void Start() if (color.a == 0) color.a = 1; } + void OnDestroy() + { + TFSystem.ClearStaticData(); + } void EnsureSettings(TFStream stream) {