Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
8
Question by Simeon · Oct 09, 2016 at 11:58 PM · editorcallbackfocus

A way to detect when the Editor Application is focused

I'm doing an Editor extension and I want to update an application wide state once the Editor has gained focus. For example once the user Alt+Tabs into another window and then returns back to the Editor.

Comment
Add comment · Show 1
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 feomedia · Jan 09, 2018 at 01:51 PM 0
Share

I'm curious about this too

7 Replies

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

Answer by Simeon · Jan 10, 2018 at 07:08 PM

I Found a cheaty way. Apparently the EditorWindow has a private property called hasFocus. I just use reflection to access it. This will give delegate:

 Delegate.CreateDelegate(typeof(Func<EditorWindow,bool>),typeof(EditorWindow).GetProperty("hasFocus", BindingFlags.NonPublic | BindingFlags.Instance).GetGetMethod(true));
Comment
Add comment · Show 7 · 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 AntonLind · Mar 16, 2018 at 02:27 PM 0
Share

How would you go about subscribing to this event? Or just getting the value.

avatar image Simeon · Mar 16, 2018 at 03:47 PM 0
Share

@AntonLind the method i provided returns a func delegate you can call. You just need to provide it a window argument.

avatar image bstockwell Simeon · Sep 03, 2018 at 08:41 PM 0
Share

Can you share any more info about this?

From what I've been able to get working, I assume I should be using this in some "EditorWindow" Class correct? Like this :

 using System;
 using System.Reflection;
 using UnityEngine;
 using UnityEditor;
 
 public class $$anonymous$$yDebugWindow : EditorWindow
 {
 
     Func<EditorWindow, bool> isWindowFocused = (Func<EditorWindow, bool>)Delegate.CreateDelegate( typeof( Func<EditorWindow, bool> ), typeof( EditorWindow ).GetProperty( "hasFocus", BindingFlags.NonPublic | BindingFlags.Instance ).GetGet$$anonymous$$ethod( true ) );
 
     public void OnGUI()
     {
         if ( isWindowFocused( GetWindow<EditorWindow>() ) )
             doSomething();
     }
 
 }

And then you say "You just need to provide it a window argument" so I tried "GetWindow()"

The issue is that it seems like it FORCES Unity to be focused when ever I call the function "isWindowFocused", I need to actually close the "$$anonymous$$yDebugWindow" to focus on any other application.

So where did I go wrong? Should this be a $$anonymous$$onoBehavior? or and Editor class? or some static class?

Thanks!

avatar image Simeon bstockwell · Sep 03, 2018 at 08:45 PM 1
Share

just use the instance of the window. In your case isWindowFocused(this)

Show more comments
avatar image
4

Answer by llMarty · Apr 16, 2019 at 06:35 PM

UnityEditorInternal.InternalEditorUtility.isApplicationActive will return true, if the Editor window is focused in any way. Was pretty hard to find, because it's not documented, but InternalEditorUtility is a gem, there is much to discover. ;)

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 Edy · Apr 16, 2019 at 06:37 PM 1
Share

Confirmed to work on Windows :)

avatar image
1

Answer by Nikolay-Lezhnev · May 29, 2019 at 10:12 AM

You can also add this script to Assets/Editor:

 using System;
 using UnityEditor;
 using UnityEngine;

 [InitializeOnLoad]
 public class EditorWindowFocusUtility
 {
     public static event Action<bool> OnUnityEditorFocus = (focus) => { };
     private static bool _appFocused;

     static EditorWindowFocusUtility()
     {
         EditorApplication.update += Update;
     }
     
     private static void Update()
     {
         if (!_appFocused && UnityEditorInternal.InternalEditorUtility.isApplicationActive)
         {
             _appFocused = UnityEditorInternal.InternalEditorUtility.isApplicationActive;
             OnUnityEditorFocus(true);
             Debug.Log("On focus window!");
         }
         else if (_appFocused && !UnityEditorInternal.InternalEditorUtility.isApplicationActive)
         {
             _appFocused = UnityEditorInternal.InternalEditorUtility.isApplicationActive;
             OnUnityEditorFocus(false);
             Debug.Log("On lost focus");
         }
     }
 }

and you can subscribe in any place on event:

 EditorWindowFocusUtility.OnUnityEditorFocus += (focus) => {/*do something*/}

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
1

Answer by aurelioprovedo · Feb 12 at 02:01 AM

The solutions proposed in other answers (@Nikolay-Lezhnev & @llMarty) already work correctly, but let me suggest an alternative method.

Using reflection, we can access the exact same event that Unity uses internally.

 public static Action<bool> UnityEditorFocusChanged
 {
     get
     {
         var fieldInfo = typeof(EditorApplication).GetField("focusChanged",
             System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
         return (Action<bool>)fieldInfo.GetValue(null);
     }
     set
     {
         var fieldInfo = typeof(EditorApplication).GetField("focusChanged",
             System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
         fieldInfo.SetValue(null, value);
     }
 }

You can then register to the event like you would with any delegate:

 UnityEditorFocusChanged += OnEditorFocusChanged;

And the signature for your callbacks must be as follows:

 private static void OnEditorFocusChanged(bool hasFocus)
 {
     if (hasFocus)
     {
         EditorWindow.focusedWindow?.ShowNotification(new GUIContent("Welcome back! ^_^"));
     }
 }
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 jposey · Feb 17 at 08:48 PM 0
Share

Nicely done. I love when I google something and find an elegant solution posted 5 days ago, to a question posted 5 1/2 years ago.

avatar image
0

Answer by electric_jesus · Jul 13, 2018 at 11:20 AM

Here is what I use:

 public static bool IsUnityEditorFocused() {
     return EditorWindow.mouseOverWindow != null;
 }
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 Skelly1983 · Jul 13, 2018 at 06:18 PM 0
Share

If the user switches to the editor using Alt/C$$anonymous$$D-Tab, or using Taskbar/Dock then the mouse may not be over the window but still be focused, this example function would return false.

  • 1
  • 2
  • ›

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

77 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

Related Questions

On Unity focus callback? 1 Answer

How do I get Unity to ask Windows for focus? 0 Answers

How can I create a editor button that can not be selected 0 Answers

Callback before Unity reloads editor assemblies? 3 Answers

How can an editor script know when another script was removed from the project? 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