Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Use System.Windows.Automation in Unity

Discussion in 'Scripting' started by hsuchc8888, Feb 29, 2020.

  1. hsuchc8888

    hsuchc8888

    Joined:
    Jan 15, 2016
    Posts:
    8
    Hello, the Unity community! I am developing an application that requires the detection of any focus changes such as a textbox that is clicked or an input field that is focused on the Windows system. I searched for the solution and found System.Windows.Automation might be the answer. To put it simply, my application needs to know any focus changes.

    To use System.Windows.Automation, I copy the .DLL files (UIAutomationClient.dll, UIAutomationClientsideProviders.dll, UIAutomationProvider.dll, UIAutomationTypes.dll) from the path "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0" into the Asset folder.

    However, whenever I subscribe to the event using Automation.AddAutomationFocusChangedEventHandler() in my code and hit play. the editor just shuts down without throwing any error or crash message. I tried to make a build and get the same result, and I didn't find anything in the Player.log.

    Have anyone tried using System.Windows.Automation in Unity successfully? If this namespace is not working on Unity, is there any way that I can achieve the same thing (to subscribe to some events so that my application knows there is a focus change)? Thanks a lot and have a good day.

    Here is my code:
    Code (CSharp):
    1. private void Start()
    2. {
    3.     var focusHandler = new AutomationFocusChangedEventHandler(OnFocusChange);
    4.     Automation.AddAutomationFocusChangedEventHandler(focusHandler);
    5. }
    6. private void OnFocusChange(object src, AutomationFocusChangedEventArgs e)
    7. {
    8.     throw new System.NotImplementedException();
    9. }
     
  2. hsuchc8888

    hsuchc8888

    Joined:
    Jan 15, 2016
    Posts:
    8
    I tested this on another machine and the Unity crashes. Here are the screenshots I got:

    2020-03-02 17_44_18.png 2020-03-02 17_45_24.png

    I still don't know why this would happen. Is Unity not working with System.Windows.Automation?
     
  3. unity_Ovzlv1QpnIS_QQ

    unity_Ovzlv1QpnIS_QQ

    Joined:
    May 6, 2019
    Posts:
    1
    Try calling
    Automation.RemoveAutomationFocusChangedEventHandler(focusHandler); in OnDestroy(). That worked for me
     
  4. hsuchc8888

    hsuchc8888

    Joined:
    Jan 15, 2016
    Posts:
    8
    Hi unity_Ovzlv1QpnIS_QQ,

    Thank you for your reply! I tried to remove the handler in OnDestroy but the editor still crashed. I guess my program crashes in Start() and can't move on.

    I think I am using the incompatible DLL file that Unity doesn't support. The ones I use are from .NET but Unity needs ones implemented by Mono. Could you provide or show me where you get your UIAutomation DLL files?