[ExecuteInEditMode] and compilation target

Hi there, I’m writing a GUI sytem. In this system I want to be able to move the position of GUI elements, using viewport coordinates rather than world space (so there positions will be correct across multiple resolutions) and see the results in the editor immediately. To try and achieve this I’m using [ExecuteInEditMode] and the following code in my update:

		#if UNITY_EDITOR 
	 
		 transform.position = referenceToGUICamera.ViewportToWorldPoint ( new Vector3 ( viewPorttarget.x, viewPorttarget.y, referenceToGUICamera.nearClipPlane ) );
			
		#endif	

So the first part of this works - I can change the viewporttarget and it updates it’s position while the game isn’t running, in the editor. However, it’s also running when the game is. I guess this is because of the [ExecuteInEditMode].

So I guess my question is, is there any way I can prevent this code from running when the game is, while still allowing it to run while Im in editor mode?

Thanks, any help much appreciated!

Maybe with Application.isEditor ?

Why do you want actively prevent updating when playing in the editor? When you create a build it won’t update since you have the preprocessor tags ( #if UNITY_EDITOR ) around that part.

edit
In the case you want to prevent the execution when playing the game in the editor, you have to check UnityEditor.EditorApplication.isPlaying. Make sure you wrap this in #if UNITY_EDITOR tags or you can’t build your game:

#if UNITY_EDITOR
if (!UnityEditor.EditorApplication.isPlaying)
    transform.position = referenceToGUICamera.ViewportToWorldPoint ( new Vector3 ( viewPorttarget.x, viewPorttarget.y, referenceToGUICamera.nearClipPlane ) );
#endif