- Home /
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");
}
}
You might have to write count to playerprefs. and load it up each time.
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.
Thanks for the tips, really helpful to know why it was resetting. I'll use player prefs for the built game - thanks!
Answer by justDeek · Aug 03, 2017 at 08:57 PM
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");
}
}
}
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
Follow this Question
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