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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Kinda Solved] Screen.width + ExecuteInEditMode + OnEnable returns width of an inspector window

Discussion in 'Editor & General Support' started by Kumo-Kairo, Oct 26, 2017.

  1. Kumo-Kairo

    Kumo-Kairo

    Joined:
    Sep 2, 2013
    Posts:
    343
    It's basically the same old question, but I couldn't find a way to workaround this yet.
    https://forum.unity.com/threads/screen-width-is-wrong-in-editor-mode.94572/
    this dates back to 2011 and it's about EditorWindow script. In my case, I have a simple MonoBehaviour that is Executable In Edit Mode and which creates a new Texture with the size of the player screen (at least it has to)
    Code (CSharp):
    1.  
    2. [ExecuteInEditMode]
    3. public class SomeMonobeh : MonoBehaviour
    4. {
    5.    private void OnEnable()
    6.    {
    7.        var width = Screen.width;
    8.        // width is correct when playing, or when adding the script to the object for the first time
    9.        // BUT it refers to the size of the INSPECTOR window when manually clicking the "enable/disable" checkbox
    10.    }
    11. }
    12.  
    width is correct when playing, or when adding the script to the object for the first time, but it refers to the size of the INSPECTOR window when manually clicking the "enable/disable"checkbox. This is quite the contrary to the Scripting API description https://docs.unity3d.com/ScriptReference/Screen-width.html
    Is there a way to get the size of the player window in OnEnable method of an ExecuteInEditMode script?

    -- Update

    Basically even OnEnables are different. It depends what has caused this OnEnable call. If it was due to assembly reload, it returns correct values. If it was caused by manual "enable/disable" checkbox mouse clicking, it returns wrong values. More than that - when first launching Unity it gives 0-0 for the screen size
    upload_2017-10-26_11-18-19.png
     
    Last edited: Oct 26, 2017
    GreenTVlad likes this.
  2. Kumo-Kairo

    Kumo-Kairo

    Joined:
    Sep 2, 2013
    Posts:
    343
    Havokki and Mnemonic426 like this.
  3. mogwhy

    mogwhy

    Joined:
    Nov 20, 2014
    Posts:
    36
    Thank you for sharing!
    You helped me a lot!