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 SophieK555 · Aug 03, 2017 at 04:50 PM · c#script.editor-scriptingplaymode

Keep track of how many times the editor goes into play mode?

I'm using an [ExecuteInEditMode] script where I would like to keep track of how many times the editor goes into play mode. I want to be able to determine if it's an even or odd number of plays. I have the following code but it does not work as I expected, it seems to minus one from count when exiting play mode and returns to 0 every time.

I've never really used the ExecuteInEditMode scripts so I'm probably missing something really simple but does anybody know why this might be happening and how I can achieve what I want?

Thanks!

[ExecuteInEditMode]

public class EditorPlayCount : MonoBehaviour {

 public bool state = false;
 public int count = 0;

 void OnEnable(){

     Debug.Log ("enabled");

     EditorApplication.playmodeStateChanged += StateChange;
 }


 void StateChange(){

     if (EditorApplication.isPlaying) {

         count++;
         state = true;
     }

 }

 void disable(){

     Debug.Log ("disabled");
 }

}

Comment
Add comment · Show 1
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 ShadyProductions · Aug 03, 2017 at 04:53 PM 0
Share

You might have to write count to playerprefs. and load it up each time.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Aug 03, 2017 at 08:56 PM

Yeah, Unity resets the scene to the last pre-play version after the current play is over. That includes all variables of all behaviours. I don't think you're going to get it to work with an ExecuteInEditMode MonoBehaviour.

Do you need it to alternate in the built game too? If so, use PlayerPrefs like @ShadyProductions suggested.

If you need it in the editor, especially outside of play mode, I would probably have it read/write to a small config file in the My Documents directory. Then you can have it increment in the Start() function if it's not in the editor.

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 SophieK555 · Aug 04, 2017 at 10:12 AM 0
Share

Thanks for the tips, really helpful to know why it was resetting. I'll use player prefs for the built game - thanks!

avatar image
0

Answer by justDeek · Aug 03, 2017 at 08:57 PM

@SophieK555

This is the best I could come up with. Either copy it over or use it as a reference. You have to attach it to a GameObject that is only in your first scene. Commented where it's needed and the result shows both in the component and as Debug.Log + the ability to reset the counter.

It utilizes EditorPrefs, a special form of PlayerPrefs that also saves Editor-related stuff. Since it uses UnityEditor and EditorPrefs it only works in the Editor, but it shouldn't be in an 'Editor'-Folder because it derives from MonoBehaviour.

 using UnityEngine;
 using UnityEditor;
 
 //Usage: Add to a GameObject in the first started scene
 //       to count the amount Play-Mode has entered/exited
 
 //Remove "[ExecuteInEditMode]" to only count the entered 
 //amount and set the default EditorPrefs value from -1 to 0
 [ExecuteInEditMode]
 public class EditorPlayCount : MonoBehaviour
 {   
     public int count = 0;
     public bool isEven = false;
     public bool reset = false;
 
     //you can also use Awake(), but then you can't disable the component
     //alternatively use Awake() and create an empty Start() {}
     public void Start()
     {
         StateChange();
     }
 
     public void StateChange()
     {
         if (reset) {
             EditorPrefs.SetInt("PlayCount", 0);
             reset = false;
         }
 
         count = EditorPrefs.GetInt("PlayCount", -1) + 1; //-1 to neutralize the first time the component gets attached
         EditorPrefs.SetInt("PlayCount", count);
         UnityEngine.Debug.Log(count);
 
         if (count % 2 == 0) {
            isEven = true;
            UnityEngine.Debug.Log("Even Amount");
         } else {
            isEven = false;
            UnityEngine.Debug.Log("Odd Amount");
         }
     }
 }

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 SophieK555 · Aug 04, 2017 at 10:10 AM 0
Share

Hi @pik$$anonymous$$36 thanks for the detailed reply! Just tried this out, it was incredibly close although counted exiting play mode as an increment too. Found the solution by adding in a check to see if the editor was playing and now it keeps track of what I want! Thanks for the help!

 #if UNITY_EDITOR
 // Editor specific code here
 
 using UnityEngine;
 using UnityEditor;
 
 //Usage: Add to a GameObject in the first started scene
 //       to count the amount Play-$$anonymous$$ode has entered/exited
 
 //Remove "[ExecuteInEdit$$anonymous$$ode]" to only count the entered 
 //amount and set the default EditorPrefs value from -1 to 0
 [ExecuteInEdit$$anonymous$$ode]
 public class EditorPlayCount : $$anonymous$$onoBehaviour
 {   
     public int count = 0;
     public bool isEven = false;
     public bool reset = false;
 
     //you can also use Awake(), but then you can't disable the component
     //alternatively use Awake() and create an empty Start() {}
     public void Start()
     {
         StateChange();
     }
 
     public void StateChange()
     {
 
         if (reset) {
             EditorPrefs.SetInt ("PlayCount", 0);
             reset = false;
         }
 
         if (EditorApplication.isPlaying) {
             count = EditorPrefs.GetInt ("PlayCount", -1) + 1; //-1 to neutralize the first time the component gets attached
             EditorPrefs.SetInt ("PlayCount", count);
             UnityEngine.Debug.Log (count);
 
             if (count % 2 == 0) {
                 isEven = true;
                 UnityEngine.Debug.Log ("Even Amount");
             } else {
                 isEven = false;
                 UnityEngine.Debug.Log ("Odd Amount");
             }
         }
     }
 }
 #endif

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

372 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 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 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

How can i add a second camera that will be showing only specific gameobject in game window ? 1 Answer

Where can I view the code created by the editor? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

DestroyImmediate(component.gameObject) destroys component but not gameObject 1 Answer


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