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");
}

}

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.

@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");
        }
    }
}