When writing editor scripts, Screen.width / Screen.height refer to the inspector?

I’ve been playing around with writing editor scripts to write custom Unity inspector windows for my objects, and menu entires in the toolbar. I’ve found something really weird with Screen, and I’m not sure what I should be doing differently.

When run in the game engine, Screen.width/height correctly return the width and height of the Game window in preview, or the unity player on publish.

But when I try to grab Screen.width / height from within an Editor script it instead returns the width and height of the inspector window!

Is there some way I can grab the Screen dimensions of the actual “Game” window from within an Editor script? Lacking this ability is putting a serious crimp in my ability to correctly assign values to created game objects.

///////////////////////
//
//In the editor script .cs file...
//
///////////////////////
override public void OnInspectorGUI(){
		MyGameObject s = target as MyGameObject;	
		s.DoSomething(); // Calling from here, Screen dimensions match inspector window...
}

void OnSceneGUI(){
	MyGameObject s = target as MyGameObject;		
	s.DoSomething();  // Calling from here Screen dimensions match Scene window...

}


/////////////////////
//
//In MyGameObject.cs file...
//
/////////////////////
public void DoSomething(){
		Debug.Log(Screen.width + " " + Screen.height);
}

void Update(){
		DoSomething(); // Calling from here, Screen dimensions match the Game window...
}

So… how can I get the “Game” window from within an editor script? All I seem to be able to find is the inspector and the Scene?

It’s working as intended :smile:

Can’t you use OnSceneGUI to set up global variables for Screen.width/height in your class?

Well I could if I had any way to dynamically gather the Game window’s screen dimensions. True, I’ll want the ability to declare the width / height in absolute numbers anyway, but I’m just exploring everything that’s available, like for example creating a floating GUI without relying on the Unity GUI classes, or another library like EZGui.

I’ve seen that iGUI lets you draw your gui elements and manipulate them directly in the game window, so clearly Unity allows you to access Game directly, somehow. How you do that is what I’d like to know.

Another thing I note is that from within Editor scripts, Camera.ScreenToWorldPoint also believes the Screen dimensions to be equal to the dimensions of the inspector window. This one is a bit more problematic because I don’t have any other way of converting a point that is intended to be a screen coordinate in the game window into world space.

For example, if the camera believes the world to have width = 200 because that’s the inspector’s width, it cannot correctly translate the positions I pass into positions seen in the game screen.

Is there some better way to do this? If not, is there at least a way I can force the camera to believe that Screen’s dimensions are those of the Game screen instead of the inspector?

I have finally solved this problem. In order to make the camera believe the Screen.width / height is the Game window’s dimensions, and not the Scene or Inspector while running in the Editor, one must perform the camera.ScreenToWorldPoint conversion from within a Camera’s OnPreRender (or other render related callback) function.

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class CustomCamera : MonoBehaviour {
	
	
	void OnPreRender(){
		if(Application.isPlaying) return;
		
		Foo[] foos = GameObject.FindObjectsOfType(typeof(Foo)) as Foo[];
		foreach(Foo foo in foos){
			foo.DoSomething();	
		}
		
	}
	
}

This is the only place I’ve found in the code that allows one to convert and position based on what one sees in the “Game” window while operating in the Editor. If anyone else knows a different or better way, let me know!