Game Development Reference
In-Depth Information
This gives your editor scripts the ability to react to whatever the editor does by attaching
to the hierarchyWindowChanged event when the script is enabled (making sure to
unattach it when the script is disabled).
Mixing it up
In more advanced cases, you can build a framework that combines with the previous ap-
proaches effectively to create a complete editor manager. This needs to be implemented in
a class with a static constructor so that it is initialized as soon as the editor starts.
To demonstrate this, let's create a simple script that will save the scene for us when we hit
the play button. First, create a new script called SaveSceneOnPlay in As-
sets\Scripts\Editor and replace its contents with the following code:
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class SaveSceneOnPlay
{
// Static class constructor,
// this is initialized as soon as Unity Starts
static SaveSceneOnPlay()
{
}
}
This gives us the framework for an [InitializeOnLoad] script that will run when
Unity starts. Then, we add our static function to do the work of saving the scene:
static void SaveSceneIfPlaying()
{
if (EditorApplication.isPlayingOrWillChangePlaymode &&
!EditorApplication.isPlaying)
{
Debug.Log("Automaticly saving scene (" +
EditorApplication.currentScene +
") before entering play mode ");
Search WWH ::




Custom Search