Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 eskivor · Jul 11, 2017 at 05:42 PM · script.editor-scriptingtypeprocessprocessing

How to execute a method when I create a new script, or I saved an existing one ? (and get his type)

I want to know how to execute a method when I create a new script, or rename an existing one ?

I know Unity do some processes when scripts are created. For example, when you create a script, the script is added to the list of components in the window Component > Scripts > NamespaceOfYourScript > NameOfYOurScript. I want to do something like that, but for my own custom window. I'm pretty sure that the process is accessible, but I don't know its name and how to access and use it.

I have already searched on the documentation, unity answers, stack overflow, but I don't find yet what I was looking for (without to know what is the exact process keyword you are looking for, it's not easy to search).

I want to get a script a bit like this kind :

 using UnityEngine;
 using UnityEditor;
 
 public class MyScript : SomeScriptProcess ()
 {
     void OnScriptSaved (System.Type TheScriptThatWasJustSaved)
     {
         //I execute my stuff here
     }
 }

Any helps would be great. :)

Comment
Add comment · Show 4
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 ShadyProductions · Jul 11, 2017 at 06:06 PM 0
Share

classes are objects, and you can use those in methods like so:

 public class Book {
 
 }
 
  public class $$anonymous$$yScript : SomeScriptProcess ()
  {
      void OnScriptSaved (Book book)
      {
          //I execute my stuff here
      }
  }
avatar image eskivor ShadyProductions · Jul 11, 2017 at 06:36 PM 0
Share

Sorry that's not what I'm looking for. I'll try to explain better :

When I create a new script in the editor. Unity execute some processes. I want to access to this process to add the execution of my own method. But I know now what is the name of the process (I don't know if it's inherited from PreProcess, PostProcess or another one).

In my example SomeScriptProcess and OnScriptSaved are placeholder names for the Unity process real names (which I don't know and I am looking for).

Am I more clear ?

avatar image ShadyProductions eskivor · Jul 11, 2017 at 07:22 PM 0
Share

I believe unity uses Reflection to access these methods like start method etc, you cannot change the existing methods that unity uses because they are internal.

avatar image Jwizard93 · Jul 11, 2017 at 09:11 PM 0
Share

A Decent portion of the editor is available as Open Source. Did you search that?

2 Replies

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

Answer by Nischo · Jul 11, 2017 at 09:37 PM

Something like this is possible, but not the way you try to approach it. First of all, unity does not now if a script is saved/created recently, at least they aren't telling. They just recompile the whole assemblies.

But you could write an editor script that when triggered collects all the MonoBehaviour classes found inside the project assembly. Something like this:

 [UnityEditor.Callbacks.DidReloadScripts]
 private static void OnScriptsReloaded()
 {
     Debug.Log("OnScriptsReloaded");
 
     foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
     {
         if (a.GetName().Name != "Assembly-CSharp") continue;
 
         foreach (Type t in a.GetTypes())
         {
             if (typeof(MonoBehaviour).IsAssignableFrom(t))
             {
                 // do something with the script
                 //
             }
         }
     }
 }

This method will find all your scripts as Type. You then can store that inside a list and check it next time for any added/removed scripts by checking the difference. That list can then be used to display in a List to select a type for example.

Hope that helps.

Comment
Add comment · Show 1 · 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 eskivor · Jul 11, 2017 at 10:05 PM 0
Share

Thanks, I will try it. :)

avatar image
0

Answer by eskivor · Jul 11, 2017 at 10:19 PM

@Nischo : I tried. It works very well. Good job. Thanks a lot ! :)

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

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

79 People are following this question.

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

Related Questions

How to get ANY component type 2 Answers

Make custom editor for generic class to apply to all current and future child classes 1 Answer

Editor Window - How to center a window. 3 Answers

Use Single Editor Script on Multiple Script Types 0 Answers

Script Takes time to compile in unity 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