Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aero80 · Feb 24, 2015 at 04:13 PM · editoreditor-scriptingdebugtesting

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baste · Feb 24, 2015 at 05:07 PM 0
Share

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.

avatar image aero80 · Feb 24, 2015 at 07:35 PM 0
Share

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
     } 
   }
  }
 }



1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
         }
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aero80 · Feb 24, 2015 at 08:36 PM 0
Share

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.

avatar image Baste · Feb 24, 2015 at 10:36 PM 0
Share

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.

avatar image aero80 · Feb 25, 2015 at 12:33 PM 0
Share

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 :)

avatar image Baste · Feb 25, 2015 at 01:08 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges