- Home /
Setup parameters and play scene from editor script
I am making a game where player needs to avoid a preset waves of obstacles. I have that data stored in a scriptable object and I populate it using an editor script.
I also have some setup, which is basically a couple of parameters whether i want to test and which wave i want to test, on my spawner. What I want to do is a clean way of triggering that setup from editor script. I can start the play mode just fine but I couldn't find a way to set those parameters on my spawner. Any help is appreciated.
Thanks
Is this spawner and the scriptable object the same thing?
In essence, what you want is a button that sets the correct settings, and puts the game in play mode. I'm doing something similar with hotkeys, similar to this:
[$$anonymous$$enuItem("Custom/PlayWithCoolSettings %F12")]
static void PlayWithCoolSettings() {
Game$$anonymous$$anager.SetSettings(cool_settings);
EditorApplication.isPlaying = true;
}
Which makes ctrl+f12 launch the game with the specified settings. If that's not enough to get you on the correct path, post a snippet of the script where you need to set the parameters on, and it'll be easier to help you.
Below you can see the relevant bits of the code. Spawner and the scriptable object isn't the same thing. I tried having static methods/parameters on the spawnmanager just like in your example but it didnt work for me. I would like to set isdebug and index params inside that play button curlies.
public class Spawn$$anonymous$$anager: $$anonymous$$onoBehaviour
{
public $$anonymous$$yScriptableObject Data;
public bool IsDebug;
public int index;
WaveData GetRandomWaveData()
{
if(IsDebug){return Data.Waves[index]}
return Data.Waves.GetRandom();
}
}
public class $$anonymous$$yScriptableObjectEditor:Editor
{
public override void OnInspectorGUI()
{
for (int i = 0; i < _targetObject.Waves.Count; i++)
{
if (GUILayout.Button("Play"))
{
EditorApplication.isPlaying = true;
//Should set spawnmanager debug params somewhere here
}
}
}
}
Answer by Baste · Feb 24, 2015 at 08:08 PM
An editor class needs to be set as the editor for the correct class. The example code you provided lacks a [CustomEditor] tag, but it seems like it's a custom editor for the MyScriptableObject class?
Anyways, to do what you're asking for, this is a decent example:
[CustomEditor(typeof(SpawnManager))]
public class SpawnManagerEditor : Editor {
public override void OnInspectorGUI() {
DrawDefaultInspector();
// the target variable is the selected object of the type defined in the CustomEditor tag
SpawnManager manager = (SpawnManager) target;
if (GUILayout.Button("Play in debug mode")) {
manager.IsDebug = true;
EditorApplication.isPlaying = true;
}
if(GUILayout.Button("Play in normal mode")) {
manager.IsDebug = false;
EditorApplication.isPlaying = true;
}
}
}
I think you misunderstood what I was trying to achieve. I want to be able to go into my debug mode from my scriptableobject's editor. For example I create a new wave. Each wave has its own properties..etc along with some buttons play being one of them. When I hit play button for that wave and I want it to start the scene with the correct wave index and debug status.
Right now I do this manually from Spawn$$anonymous$$anagers inspector which is not very intuitive.
Something like this, perhaps:
[CustomEditor(typeOf($$anonymous$$yScriptableObject))]
public class $$anonymous$$yScriptableObjectEditor:Editor
{
public override void OnInspectorGUI()
{
$$anonymous$$yScriptableObject _targetObject = ($$anonymous$$yScriptableObject) target;
for (int i = 0; i < _targetObject.Waves.Count; i++)
{
if (GUILayout.Button("Play"))
{
Spawn$$anonymous$$anager manager = GameObject.Find<Spawn$$anonymous$$anager>();
manager.IsDebug = true;
manager.index = i;
EditorApplication.isPlaying = true;
}
}
}
}
That's assu$$anonymous$$g that there's a Spawn$$anonymous$$anager in the scene.
Thanks for the suggestions but I am afraid they don't work. They were actually one of the few things I tried first. But values get overridden as soon as scene actually becomes playable which is expected i guess.
Anyways what I ended up doing is moving those debug params to scriptableobject. It is not the best solution but for now gets the job done. I just have to remember to check them before actually publishing the game :)
Woops, I forgot the important part:
if (GUILayout.Button("Play"))
{
Spawn$$anonymous$$anager manager = GameObject.Find<Spawn$$anonymous$$anager>();
manager.IsDebug = true;
manager.index = i;
**EditorUtility.SetDirty(target);**
EditorApplication.isPlaying = true;
}
If you don't do a SetDirty, Unity doesn't know that the values has been changed, and thus the new values won't be serialized (saved). if you add SetDirty, it should work.
Your answer
Follow this Question
Related Questions
Is it possible to extend the playing and debugging process? 0 Answers
Debug.Log doesn't work properly in a ContextMenu function 3 Answers
Compiler Error List Archive Solutions 1 Answer
OnInteractivePreviewGUI multiple selection. Works but spams errors. 0 Answers
How do I fix error code cs1056: Unexpected Character 1 Answer