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
2
Question by mojtaba64 · Mar 19, 2013 at 08:12 AM · editorinspectorcallanother

Call inspector editor script function from another editor script

Hi,

 [CustomEditor (typeof(TestClass))]
 
 public class TestClassEditor : Editor
 {
     float a = 1;
     float b = 2;
     public void sum ()
     {
         Debug.Log (a + b);
     }
     
     public override void OnInspectorGUI ()
     {
         a = EditorGUILayout.FloatField (a);
         b = EditorGUILayout.FloatField (b);
         if (GUI.Button(new Rect(20, 300, 100, 30), new GUIContent("sum")))
             sum ();
     }
 }

In editor i can select gameobject and click on "sum" button, but I want to do this (call sum function) from another editor script.

e.g I could access to the game object with this code in another editor script:

 GameObject obj = GameObject.Find ("object name");
 obj.GetComponent <TestClass> (); // not TestClassEditor

but I want to do something like this:

 GameObject obj = GameObject.Find ("object name");
 obj.GetComponent <TestClassEditor> ().sum ();

Any idea?

Thanks

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

2 Replies

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

Answer by Bunny83 · Mar 19, 2013 at 09:16 AM

You have to get a reference to your Editor-instance. Keep in mind that it's possible that there is none or multiple (since you can open multiple Inspectors). With Resources.FindObjectsOfTypeAll you can search for any instances that currently exist.

 TestClassEditor editors = (TestClassEditor[])Resources.FindObjectsOfTypeAll(typeof(TestClassEditor));
 if (editors.Length >0)
 {
     editors[0].sum();
 }

This will execute the sum function of the first instance it can find.

Comment
Add comment · Show 6 · 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 mojtaba64 · Mar 19, 2013 at 09:47 AM 0
Share

Thanks, it's solved my problem. gameObject should be in selection so Resources.FindObjectsOfTypeAll could return in so I add this code before your code:

 Selection.activeTransform = GameObject.Find ("GameObject").transform;
avatar image mojtaba64 · Mar 19, 2013 at 11:11 AM 1
Share

Set Selection.activeTransform didn't work. It seems that when you select an object it won't load instantly

 Selection.activeGameObject = GameObject.Find ("TestObject");
 Debug.Log (Resources.FindObjectsOfTypeAll(typeof(TestClassEditor)).Length);

It prints 0 on first run, but on second run (or if i selected TestObject already) it prints 1.

btw FindObjectsOfTypeAll won't print 2 if I select 2 TestObject (commented line 1!).

avatar image Bunny83 · Mar 19, 2013 at 02:51 PM 1
Share

Of course. If you select two objects but use only one inspector there will be only one editor instance. If you have two or more Inspector windows (by using the "add new tab" menu) you will get multiple instances, even for one selected object unless one inspector is "locked" on a different object.

avatar image Bunny83 · Mar 19, 2013 at 02:55 PM 0
Share

It's actually a bit strange to have local variables in the editor class. They will be lost when you deselect the object. What's the purpose of thoas variables? Usually you would use either static variables or save / load them with EditorPrefs

If you want to store information per "edited object", those variables should be part of your actual inspected object (in your case TestClass).

avatar image mojtaba64 · Mar 19, 2013 at 03:26 PM 0
Share

I used variables just as a sample... Actually I'm using UniTile and I want to optimize it:

link text

I didn't write UniTile so it's a bit hard for me to change it.

I understand the FindObjectsOfTypeAll now. I'm working on a EditorWindow that let me select GameObject, and when it opened (after 1 frame) I use FindObjectsOfTypeAll to get editor script...

If it doesn't work I have to change UniTile scripts (not editor scripts) as you said.

Thanks you very much, It would be great if you post anything that could help.

Show more comments
avatar image
0

Answer by Chronos-L · Mar 19, 2013 at 08:22 AM

static function.

 public class Operate {
    public static void Sum( int a, int b ) {
       Debug.Log( a + b );
    }
 }

To use it:

 if (GUI.Button(new Rect(20, 300, 100, 30), new GUIContent("sum"))) {
    Operate.Sum( a, b );
 }
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 mojtaba64 · Mar 19, 2013 at 08:35 AM 0
Share

Thanks, but I want to access sum from another editor script, how could I access to Sum function from another editor script.

e.g I could access to the game object with this code in another editor script:

 GameObject obj = GameObject.Find ("object name");
 obj.GetComponent <TestClass> (); // not TestClassEditor

but I want to do something like this:

 GameObject obj = GameObject.Find ("object name");
 obj.GetComponent <TestClassEditor> ().sum ();
avatar image Chronos-L · Mar 19, 2013 at 08:46 AM 1
Share

I don't think obj.GetComponent () is feasible.

GetComponent() returns Component, while Component is extended from Object. However, Editor inherits from ScriptableObject, which has nothing to do with Component at all.

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

15 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

Related Questions

Drop down with sizes 1 Answer

Can't access Inspector variable in C# Editor script? 2 Answers

Call function in another script? 1 Answer

Call a Function from another Script 1 Answer

I'm having trouble referencing a function from one script inside another 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