Search Unity

Feedback How to know if a script is running inside Unity Editor when using Device Simulator?

Discussion in 'Scripting' started by SolarianZ, Jun 29, 2020.

  1. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    237
    How to know if a script is running inside Unity Editor when using Device Simulator?

    When using Device Simulator, Application.isEditor will return false.
    In this case, is there anyway to know if a script is running inside Unity Editor without using any Editor API or UNITY_EDITOR macro definition?

    Maybe Unity should add a new API to let us know if we are running inside Unity Editor whether used Device Simulator or not.
     
    matronator likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
  3. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    237
  4. stylophone

    stylophone

    Joined:
    Aug 16, 2012
    Posts:
    37
    I too have this problem. Application.isEditor returns false when using the Device Simulator in Unity Editor which causes a little confusion.
     
    ZO5KmUG6R and paradizIsCool like this.
  5. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    Same problem here...
     
  6. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    Anyone has an idea how to go about this?
     
  7. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Same problem here - solution by Unity please!
     
  8. kyubuns

    kyubuns

    Joined:
    Aug 6, 2013
    Posts:
    138
  9. kyubuns

    kyubuns

    Joined:
    Aug 6, 2013
    Posts:
    138
    I found solution
    `var isEditor = Application.installMode == ApplicationInstallMode.Editor;`
     
  10. Alexander-Fedoseev

    Alexander-Fedoseev

    Joined:
    Jan 1, 2015
    Posts:
    4
    Code (CSharp):
    1. #if UNITY_EDITOR
    2.  
    3.         if (Application.isEditor)
    4.             Debug.Log("Editor");
    5.         else
    6.             Debug.Log("Editor Simulator");
    7.  
    8. #endif
     
  11. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    This doesn't work in 2021.2.5f1
     
  12. SolarianZ

    SolarianZ

    Joined:
    Jun 13, 2017
    Posts:
    237
    This is the way! ;)
     
    ROBYER1 likes this.
  13. OldKing_Wang

    OldKing_Wang

    Joined:
    Jan 25, 2015
    Posts:
    46
    ok first of all
    ths Device Simulator team really NOT doing well with there document. it's take me hours to find out how to detect did i in simulator or not. and how to get the correct resolution of currnt device !

    - How can i know current is GameView or Device Simulator ?

    if you use Unity 2020 LTS or 2019 LTS and import Device Simulator from package manger.
    then you can use
    Code (CSharp):
    1.  
    2. public static bool IsEditorSimulator()
    3. {
    4. #if UNITY_EDITOR
    5.             return !Application.isEditor;
    6. #endif
    7.             return false;
    8. }
    9. public static bool IsEditorGameView()
    10. {
    11. #if UNITY_EDITOR
    12.             return Application.isEditor;
    13. #endif
    14.             return false;
    15. }
    16.  
    but if you use Unity 2021 , and Simulator is part of it. you can't use Application.isEditor or Application.installMode to check. you need use
    Code (CSharp):
    1.  
    2. public static bool IsEditorSimulator()
    3. {
    4. #if UNITY_EDITOR
    5.             return UnityEngine.Device.SystemInfo.deviceType != DeviceType.Desktop;
    6. #endif
    7.             return false;
    8. }
    9. public static bool IsEditorGameView()
    10. {
    11. #if UNITY_EDITOR
    12.             return UnityEngine.Device.SystemInfo.deviceType == DeviceType.Desktop;
    13. #endif
    14.             return false;
    15. }
    16.  
    - How can i get current device resolution ?

    in Unity 2020 LTS or 2019 LTS . you can't ! no kidding. you just can't.
    the only thing i can find relate to that topic is this post
    https://forum.unity.com/threads/new-device-simulator-preview.751067/
    there is no document mention that things at all.
    and if you use Unity 2021. you can simply use UnityEngine.Device.Screen to get the right info.
    Code (CSharp):
    1.  
    2. public static Vector2Int EditorOnly_GetMainGameViewSize()
    3. {
    4.     var T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
    5.     var GetSizeOfMainGameView = T.GetMethod("GetSizeOfMainGameView", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
    6.     var Res = GetSizeOfMainGameView.Invoke(null, null);
    7.     var size = (Vector2) Res;
    8.     return new Vector2Int((int) size.x, (int) size.y);
    9. }
    10. public static Vector2Int GetScreenSize() { return IsEditorGameView() ? EditorOnly_GetMainGameViewSize() : new(UnityEngine.Device.Screen.width, UnityEngine.Device.Screen.height); }
    11.  
     
    vonSchlank, abixbg, Ninja_WS and 7 others like this.
  14. westdev

    westdev

    Joined:
    Sep 12, 2017
    Posts:
    9
    the solution i achieved is :
    (example)
    Code (CSharp):
    1. public static void show()
    2.     {
    3. #if !UNITY_EDITOR
    4.    
    5. if (interstitial.IsLoaded())
    6.         {
    7.             interstitial.Show();
    8.             //do something
    9.         }
    10.         else
    11.         {
    12.            //do something
    13.         }
    14.  
    15. #endif
    16.  
    17.         Debug.Log("not mobile");
    18.  
    19.     }
    because in (#if UNITY_EDITOR)
    you will get error in building to apk, because if you used #else in this way there is no endelse.
     
  15. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539
    any news on this ??
     
  16. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539
    what about this ?

    if you query UnityEngine.Device.Application.platform while in editor you get "OSXEditor" in my case, but if you change to simulator you get IphonePlayer
     
  17. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539
    also if you do
    Code (CSharp):
    1.  
    2. #if UNITY_EDITOR
    3. UnityEngine.Device.Application.isEditor
    4. #endif
    5.  
    while in device simulator you get false, and you get true while in normal editor, hence you can conclude when you are in device simulator, am I wrong ?

    PS: Im working on 2021.3.12
     
  18. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539
    Code (CSharp):
    1.  
    2.  
    3. #if UNITY_EDITOR
    4. if (UnityEngine.Device.Application.isEditor)//Editor
    5. {
    6.     if (Application.isPlaying)
    7.         runmode = RunMode.EditorRunning;
    8.     else
    9.         runmode  = RunMode.EditorStopped;
    10. }
    11. else //Simulator
    12. {
    13.     if (UnityEngine.Device.Application.isPlaying)
    14.         runmode = RunMode.SimulatorRunning;
    15.     else
    16.         runmode = RunMode.SimulatorStopped;
    17. }
    18. #else
    19. runmode= Runmode.Runtime;
    20. #endif
    21.  
    This is what I did in 2021.3.12 and works fine
     
  19. moserchristian

    moserchristian

    Joined:
    Dec 12, 2021
    Posts:
    3
    I would propose this script:

    Code (CSharp):
    1. public static class RunMode
    2. {
    3.     public static ApplicationRunMode Current
    4.     {
    5.         get
    6.         {
    7. #if UNITY_EDITOR
    8.             return Application.isEditor ? ApplicationRunMode.Editor : ApplicationRunMode.Simulator;
    9. #else
    10.             return ApplicationRunMode.Device;
    11. #endif
    12.         }
    13.     }
    14. }
    15.  
    16. public enum ApplicationRunMode
    17. {
    18.     Device,
    19.     Editor,
    20.     Simulator
    21. }
    22.  
     
  20. restush96

    restush96

    Joined:
    May 28, 2019
    Posts:
    144
    @moserchristian you class will always return to editor. Here is a fix.

    Code (CSharp):
    1.  
    2. public static class RunMode {
    3.   public enum ApplicationRunMode {
    4.     Device,
    5.     Editor,
    6.     Simulator
    7.   }
    8.   public static ApplicationRunMode Current {
    9.     get {
    10.       #if UNITY_EDITOR
    11.       return UnityEngine.Device.Application.isEditor && !UnityEngine.Device.Application.isMobilePlatform ? ApplicationRunMode.Editor : ApplicationRunMode.Simulator;
    12.       #else
    13.       return ApplicationRunMode.Device;
    14.       #endif
    15.     }
    16.   }
    17. }
    18.