- Home /
difficulty with InitializeOnLoad and Application.isPlaying
I have created the following simple editor-only script in a new project (in the Assets\Editor folder):
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class OnLoadTester
{
static OnLoadTester()
{
Debug.Log("InitializeOnLoad function called. Application.isPlaying: " + Application.isPlaying);
}
}
When I hit play in the Unity editor, the following is displayed in the log:
"InitializeOnLoad function called. Application.isPlaying: False"
Based on the description in the doc's I would have expected the Application.isPlaying
accessor to return TRUE after I hit play. What am I missing?
The main question is; how can I get a function to execute on reloads, but NOT when the user clicks play?
(Unity version 2019.2.01f)
Edit:/Update: I have tried with Varius OnLoad attributes including InitializeOnLoadMethod, UnityEditor.Callbacks.DidReloadScripts(0) and UnityEditor.Callbacks.DidReloadScripts(1). I also tried using EditorApplication, rather than Application. All yielded the same results: isPlaying return false after clicking play.
Update2: I've submitted this as a bug: https://fogbugz.unity3d.com/default.asp?1189404_0teifh8vms2gld6k
What do you mean with reload? hot reload (compiling) or re enter play mode?
reloads I'd consider as- compling a project, opening a project, etc .... basically all the usual occasions specified in [InitializeOnLoad] EXCEPT hitting play. (I'm even ok with it running when I hit "play" also... IF I had a way to see that the application isPlaying, so I can skip the code that I don't want to run in this case).
Answer by PizzaPie · Oct 07, 2019 at 06:04 AM
Try this one EditorApplication.isPlayingOrWillChangePlaymode or the event EditorApplication.playModeStateChanged which fires two events before playmode maybe the first one(event) is what you are looking for (EnteringPlayMode or something like that), of course with the event you ll need to serialize the current state as it can not be cashed in the class which will get reinitialized on each reload.
Overall I wouldn't think this to be a bug, according to docs InitializeOnLoad will get "invoked" (not per se but the script marked with it) before play mode so this is normal, doesn't seem to happen on 2018.4 LTS though.
Other than that it would be more suited to have a way to load scripts on editor only on start up, which I don't think is possible at the moment.
isPlayingOrWillChangePlaymode
that is EXACTLY the answer I was looking for, it returns TRUE, after the user clicks play. Didn't even realize it existed, must have just missed it in the docs, thanks Pizza! Please covert it, or just create a new answer so I can accept it.
Answer by Glurth · Oct 04, 2019 at 03:00 PM
I came up slightly with a convoluted work-around. It runs later than I wanted (have not yet tested if this will cause an issue in my main project). I also have a general dislike of it, on the principle that hooking to EditorApplication.Update should not be necessary for initialization. So, still looking for a BETTER answer:
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class OnLoadThenUpdate
{
static OnLoadThenUpdate()
{
EditorApplication.update += OnEditorUpdate;
}
static public void OnEditorUpdate()
{
Debug.Log("OnEditorUpdate function called. Application.isPlaying: " + Application.isPlaying);
EditorApplication.update -= OnEditorUpdate;
}
}
Output when clicking "play" is:
OnEditorUpdate function called. Application.isPlaying: True