AirConsole / airconsole-unity-plugin

A plugin for Unity to develop for AirConsole
http://developers.airconsole.com/#/guides/unity
GNU Lesser General Public License v3.0
69 stars 19 forks source link

Open Exported Port throws exception due to DontDestroyOnLoad #64

Closed bbeinder closed 3 months ago

bbeinder commented 3 months ago

Using the Open Exported Port button for the first time causes the following exception:

InvalidOperationException: The following game object is invoking the DontDestroyOnLoad method: AirConsole. Notice that DontDestroyOnLoad can only be used in play mode and, as such, cannot be part of an editor script.
NDream.AirConsole.AirConsole.get_instance () (at Assets/AirConsole/scripts/AirConsole.cs:88)
NDream.AirConsole.AirConsole.GetUrl (NDream.AirConsole.StartMode mode) (at Assets/AirConsole/scripts/AirConsole.cs:1705)
NDream.AirConsole.Editor.Extentions.OpenBrowser (NDream.AirConsole.AirConsole controller, System.String startUpPath) (at Assets/AirConsole/scripts/editor/Extentions.cs:114)
NDream.AirConsole.Editor.Inspector.OnInspectorGUI () (at Assets/AirConsole/scripts/editor/Inspector.cs:82)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass72_0.<CreateInspectorElementUsingIMGUI>b__0 () (at <5d5ebefe97114215928ac1d9cd096522>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

Offending line: https://github.com/AirConsole/airconsole-unity-plugin/blob/89a54f21a1bfbc1acf2cb871432f6ade75994b20/Assets/AirConsole/scripts/AirConsole.cs#L95-L97

Possible fix:

if (_instance != null && Application.isPlaying) {
    DontDestroyOnLoad (_instance.gameObject);
}