So I’ve figured out previously how to write code for when a scene saves in the editor, but I can’t figure out how to do it for when a scene loads.
From what I understand from the coding api you can add a delegate to EditorSceneManager.activeSceneChanged. The problem is, I’m not exactly sure where to put the code.
I have this:
//First:
using UnityEditor.SceneManagement;
Then this in a method in a class:
EditorSceneManager.activeSceneChanged += HelloWorld;
Then this as the method I want to add to the delegate:
void HelloWorld() {
Debug.Log("Hello world.");
}
My thought is that it would be put inside of an editor script?
Nvm, sorry I think I’ve found a simple solution with this:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class SceneScript : MonoBehaviour {
bool HasRunOnce = false;
void Update () {
if (HasRunOnce == false) {
Debug.Log ("One this once.");
HasRunOnce = true;
}
}
}
And then just attach that to a scene game object (or another game object) within the scene. Something I don’t mind doing since I’m using templates for my scenes.
You were correct with your delegate but they take parameters so you also need your own function to take those parameters too:
UnityAction<Scene, Scene> activeSceneChanged;
UnityAction<Scene, LoadSceneMode> sceneLoaded;
UnityAction<Scene> sceneUnloaded;
e.g.
void MyClassActiveSceneChanged( Scene arg0 , Scene arg1 )
{
Debug.Log("Active Scene Changed");
}
The documentation doesn’t actually tell you the parameters which is pretty annoying.
1 Like