• 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 webster · Jan 16, 2013 at 01:46 AM · uieditorwizard

Need help with Editor script

Hey all,

I'm needing to make an editor script which a) starts automatically to init some stuff when I open my project (possible?) b) accepts some input (I know how to make a Wizard, is that the only option?) and c) periodically runs something (like Update, doesn't have to be every frame though).

Is that all possible?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by numberkruncher · Jan 16, 2013 at 01:59 AM

a) starts automatically to init some stuff when I open my project (possible?)

Yes this is possible:

 using UnityEngine;
 using UnityEditor;

 [InitializeOnLoad]
 public static class SomeEditorScript {

     static SomeEditorScript() {
         // Do your stuff here...
     }

 }

See: http://docs.unity3d.com/Documentation/Manual/RunningEditorCodeOnLaunch.html

b) accepts some input (I know how to make a Wizard, is that the only option?)

You can create your own custom interfaces by creating your very own editor windows. As of the time of writing you cannot automatically display a window when your script first loads, but you can add a custom menu item which displays your interface:

 using UnityEngine;
 using UnityEditor;

 public static class SomeEditorWindow : EditorWindow {

     [MenuItem("Your Stuff/Some Editor Window")]
     static void Show() {
         GetWindow<SomeEditorWindow>();
     }

     void OnEnable() {
         title = "My Editor Window";
     }

     void OnGUI() {
         GUILayout.Label("Place your controls here!");

         if (GUILayout.Button("Foo")) {
             // Do something here!
             GUIUtility.ExitGUI();
         }
     }

 }

See: http://docs.unity3d.com/Documentation/ScriptReference/EditorWindow.html

c) periodically runs something (like Update, doesn't have to be every frame though).

There are several ways to do this depending upon what you are trying to achieve. The most generic solution is to attach an event handler as demonstrated below. This will frequently invoke your custom event handler. You can manually limit how often your logic is executed using the .NET DateTime class.

 void SomeFunctionToInitializeEvent() {
     EditorApplication.update += delegate {
         // Do your stuff here!
     };
 }

 // or alternatively you can use this syntax:

 void SomeFunctionToInitializeEvent() {
     EditorApplication.update += new CallbackFunction(MyEventHandler);
 }

 void MyEventHandler() {
     // Do your stuff here!
 }

See: http://docs.unity3d.com/Documentation/ScriptReference/EditorApplication-update.html

The following documentation includes references to other callbacks that may be of use to you: http://docs.unity3d.com/Documentation/ScriptReference/EditorApplication.html

It is also worth noting that you can use the Update message in your custom EditorWindow which is called at regular intervals so long as the window is open.

I hope that this is of help to you.

Comment
Add comment · Show 2 · 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 webster · Jan 16, 2013 at 02:11 AM 0
Share

$$anonymous$$any thanks

avatar image numberkruncher · Jan 16, 2013 at 02:17 AM 0
Share

@web No problem, glad that I could help :-) Happy coding!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Something in this Script keeps activating UI-Buttons 1 Answer

ngui tween in fixedupdate or update 1 Answer

Drop down with sizes 1 Answer

Multiple Text of canvas with One script ?? 1 Answer

UniSciTE in version 3.x Script Edtior 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges