JetBrains / resharper-unity

Unity support for both ReSharper and Rider
Apache License 2.0
1.21k stars 134 forks source link

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

Open rgarat opened 7 years ago

rgarat commented 7 years ago

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.

citizenmatt commented 7 years ago

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.

rgarat commented 7 years ago

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

cmcpasserby commented 7 years ago

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.

sindrijo commented 7 years ago

@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

Froghut commented 7 years ago

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

CAD97 commented 7 years ago

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")]).

van800 commented 7 years ago

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

van800 commented 6 years ago

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

gwiz665 commented 6 years ago

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?

SugoiDev commented 6 years ago

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.

mmalinin commented 5 years ago

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

citizenmatt commented 5 years ago

See also RIDER-31718

Froghut commented 1 year ago

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?