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 usernameHed · Apr 05, 2019 at 09:41 PM · animationinspectoreditorwindowreflectionreflections

How to Access the "play button" of a AnimatiorWindow from script editor

EDIT: My Goal is to find the method "play Button" or wathever the name is, and invoke it. I Woulld like to have some information about how, from a given unity editor window (here, the animation Window), access to all method, and have the information of the methods and their parameters that can be used by it.

  • Where can I find some documentation about it

  • Where to search in the unity source code to rapidly find, from a given goal, the method & parametter of a given EditorWindow. -

Here is how I get the Unity Animation Editor Window:

 System.Reflection.Assembly editorAssembly = System.Reflection.Assembly.GetAssembly(typeof(EditorWindow));
 System.Type animationWindowType = ExtReflexion.GetTypeFromAssembly("AnimationWindow", editorAssembly);
 System.Object animationWindowObject = EditorWindow.GetWindow(animationWindowType);
     

here the function GetTypeFromAssembly:

  public static System.Type GetTypeFromAssembly(string typeName, System.Reflection.Assembly assembly, System.StringComparison.ignoreCase = StringComparison.CurrentCultureIgnoreCase)
     {
            if (assembly == null)
                  return (null);
         
           System.Type[] types = assembly.GetTypes();
           foreach (System.Type type in types)
           {
                  if (type.Name.Equals(typeName, ignoreCase) || type.Name.Contains('+' + typeName))
                         return (type);
           }
            return (null);
     }

alt text

So, now I have my Type animationWindowObject. I have try to do

 MethodInfo[] allMathod = animationWindowType.GetMethods()

and parse it, but I don't see the method I'm looking for.

I know I have to do something like:

 System.Reflection.MethodInfo previewMethod = animationWindowType.GetMethod("PlayButton", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);

and then previewMethod.Invoke or something... but my knowledge is limited in that domain

animation.png (24.6 kB)
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 RobAnthem · Apr 05, 2019 at 10:27 PM 0
Share

I'm going to guess you don't actually need the animation window and you just want to play animations in editor mode. As such, I would read this article Playing animations from a custom EditorWindow

avatar image usernameHed · Apr 06, 2019 at 10:16 AM 0
Share

Thanks RobAnthem. In Fact I would like most generally if it is possible to access some function of the built in editor window, and I don't want to create my own editorWindow. I just want to execute some function of the built in editorWindow already present

2 Replies

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

Answer by Delvatron · Apr 11, 2019 at 12:55 PM

You should look into AnimEditor in the PlayButtonOnGUI method here

This line in particular:

 controlInterface.StartPlayback();

Which you may be able to call with (Non tested pseudo code)

 FieldfInfo animEditorFI = 
 animationWindowType.GetField("m_AnimEditor", BindingFlags.NonPublic | BindingFlags.Instance);
 PropertyInfo controlInterfacePI = 
 animEditorFI.fieldType.GetProperty("controlInterface", BindingFlags.Public | BindingFlags.Instance);
 MethodInfo playMI = 
 controlInterfacePI.propertyType.GetMethod("StartPlayback", BindingFlags.Public | BindingFlags.Instance);
 object controlInterface = 
 controlInterfacePI.GetValue(animEditorFI.GetValue(animationWindowObject));
 playMI.Invoke(controlInterface, new object[0]);
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
avatar image
0

Answer by usernameHed · Apr 16, 2019 at 07:33 AM

Here, thanks to @valentin4311, my final static function for play/unplay a button (who use 3 other static function), all here:

 /// <summary>
 /// play button on animator
 /// </summary>
 public static void SetPlayButton()
  {
       //open Animation Editor Window
       System.Type animationWindowType = null;        
       EditorWindow animationWindowEditor = ShowAndReturnEditorWindow("AnimationWindow", ref animationWindowType);
     
       //Get animationWindow Type
       animationWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.AnimationWindow");
     
        //Get field m_AnimEditor
        FieldInfo animEditorFI = animationWindowType.GetField("m_AnimEditor", GetFullBinding());
     
         //Get the propertue of animEditorFI
         PropertyInfo controlInterfacePI = animEditorFI.FieldType.GetProperty("controlInterface", GetFullBinding());
     
         //Get property i splaying or not
         PropertyInfo isPlaying = controlInterfacePI.PropertyType.GetProperty("playing", GetFullBinding());
             
         //get object controlInterface
         object controlInterface = controlInterfacePI.GetValue(animEditorFI.GetValue(animationWindowEditor));
         bool playing = (bool)isPlaying.GetValue(controlInterface);
     
          if (!playing)
          {
              MethodInfo playMI = controlInterfacePI.PropertyType.GetMethod("StartPlayback", GetFullBinding());
               playMI.Invoke(controlInterface, new object[0]);
          }
          else
          {
              MethodInfo playMI = controlInterfacePI.PropertyType.GetMethod("StopPlayback", GetFullBinding());
              playMI.Invoke(controlInterface, new object[0]);
          }
   }

And here the other static function used:

   public static System.Reflection.BindingFlags GetFullBinding()
   {
       return (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Static);
  }

//

   /// <summary>
   /// from a given name, return and open/show the editorWindow
   /// usage:
   /// System.Type animationWindowType = null;
   /// EditorWindow animationWindowEditor = ShowAndReturnEditorWindow(ExtReflexion.AllNameAssemblyKnown.AnimationWindow, ref animationWindowType);
   /// </summary>
   public static EditorWindow ShowAndReturnEditorWindow(string editorWindow, ref System.Type animationWindowType)
   {
          System.Reflection.Assembly editorAssembly = System.Reflection.Assembly.GetAssembly(typeof(EditorWindow));
          animationWindowType = GetTypeFromAssembly(editorWindow, editorAssembly);
          EditorWindow animationWindowEditor = EditorWindow.GetWindow(animationWindowType);
     
          return (animationWindowEditor);
   }

//

   /// <summary>
   /// System.Reflection.Assembly editorAssembly = System.Reflection.Assembly.GetAssembly(typeof(EditorWindow));
   /// GetTypeFromAssembly("AnimationWindow", editorAssembly);
   /// </summary>
   /// <returns></returns>
   public static System.Type GetTypeFromAssembly(string typeName, System.Reflection.Assembly assembly, System.StringComparison ignoreCase = StringComparison.CurrentCultureIgnoreCase)
   {
          if (assembly == null)
              return (null);
     
          System.Type[] types = assembly.GetTypes();
          foreach (System.Type type in types)
          {
              if (type.Name.Equals(typeName, ignoreCase) || type.Name.Contains('+' + typeName))
                  return (type);
          }
          return (null);
   }
 
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

273 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 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 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 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 have two inspectors of different types thats objects they are inspecting change 0 Answers

EDITOR: How to Change Animation Curve Colors 0 Answers

Can I make animations snap to a frame? 1 Answer

Customize Vector3 Inspector 1 Answer

3D model doesn't render unless I look at it in scene view. 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