Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Glurth · Oct 03, 2019 at 07:13 PM · editorplaymodeinitialization

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PizzaPie · Oct 04, 2019 at 03:16 PM 0
Share

What do you mean with reload? hot reload (compiling) or re enter play mode?

avatar image Glurth PizzaPie · Oct 04, 2019 at 03:52 PM 0
Share

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).

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Oct 07, 2019 at 01:21 PM 0
Share

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.

avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

149 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Singleton and MonoBehaviour in Editor 1 Answer

Why do I need to clic apply after editing a list content via editor script to access the list content in playmode 0 Answers

EditorLoop taking 30-40ms of every frame, any way to reduce this? 1 Answer

Help when playing in Editor , Big memory Leak/problem 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges