resharper-unity icon indicating copy to clipboard operation
resharper-unity copied to clipboard

Alert the user when using UnityEditor.dll in non editor scripts

Open rgarat opened this issue 9 years ago • 13 comments

If you use UnityEditor.dll types, in non editor code, everything will compile and run while you are in the editor, but the moment you try to build a player it will play because UnityEditor.dll is not available.

It would be great to get some warning or even error when using UnityEditor.dll types without wrapping those in ifdef UNITY_EDITOR in the regular solution.

rgarat avatar Dec 14 '16 20:12 rgarat

I'm confused - do you mean UnityEditor.dll or UnityEngine.dll? I would expect UnityEngine.dll to be available in a player, but not UnityEditor.dll.

citizenmatt avatar Dec 14 '16 21:12 citizenmatt

Oh, I am sorry, I meant UnityEditor.dll, will edit the issue

rgarat avatar Dec 14 '16 22:12 rgarat

Yeah you would only have to look for 2 things, if the file is has Editor folder in its path, or if it is in a set of UNITY_EDITOR ifdefs. Otherwise anything using UnityEditor; should be flagged with a warning.

cmcpasserby avatar Feb 07 '17 19:02 cmcpasserby

@cmcpasserby It should also check which vs project it is in. Any scripts that have a folder named "Editor" in their filepath will be placed in an "editor only" project, files in that project will not be included when building the project.

Unity3D Documentation: Special Folder Names

sindrijo avatar Feb 14 '17 18:02 sindrijo

Yes, please! I can't count the times I forgot a #IF UNITY_EDITOR and only realized on the next build.

Froghut avatar Feb 15 '17 15:02 Froghut

Using types from UnityEditor.dll is an error when not in the Editor assemblys (Assembly-CSharp-Editor, Assembly-CSharp-Edior-firstpass) or using #if UNITY_EDITOR (or [Conditional("UNITY_EDITOR")]).

CAD97 avatar Feb 20 '17 00:02 CAD97

Will it make sense to modify the csproj generation to avoid the UnityEditor references from Assembly-CSharp and Assembly-CSharp-firstpass?

van800 avatar Jul 10 '17 15:07 van800

Reasoning behind leaving references to UnityEditor.dll untouched. https://answers.unity.com/questions/770006/why-does-assembly-csharp-reference-unityeditordll.html

van800 avatar Feb 23 '18 09:02 van800

There are plenty of reasons for using UnityEditor in runtime code, like validation and updating serialization of ScriptableObjects, but it should always be wrapped in #IF UNITY_EDITOR. This does add the little annoying part that if you generally don't prefer fully qualified references, so usings are automatically added by resharper in the top, you have to wrap the using UnityEditor as well.

Is there a way to make Resharper ignore that rule specifically for UnityEditor, so it prefers to inline the reference qualification when you are in run time code?

gwiz665 avatar Mar 09 '18 12:03 gwiz665

It would be actually pretty useful to have a warning when a non IFDEF enclosed reference to anything from UnityEditor was used in a non-editor type. In fact, make that an error, since that is what it is.

Rider would have to understand the new custom assemblies too, since it would have to read them to know when a type in a custom assembly is a editor type or not.

I'm not a fan of the idea of forcing full references, as it would often make lines very long and would make C# look even more verbose than it already is.

SugoiDev avatar Mar 09 '18 18:03 SugoiDev

Still waiting for this. It`s quite handy and will save a lot of time.

mmalinin avatar Aug 01 '19 11:08 mmalinin

See also RIDER-31718

citizenmatt avatar Aug 16 '19 20:08 citizenmatt

Just found this issue again though google, just to realize I commented on this over 6 years ago. Is there anything we could do to help get this feature done?

Froghut avatar Aug 28 '23 14:08 Froghut