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

All Editor only Script without #if UNITY_EDITOR in Runtime Assembly should result in Error

Open Muchaszewski opened this issue 5 years ago • 1 comments

Recently I have used a plugin that heavily relies on editor scripts that extends the functionality of the inspector from Runtime Assembly. This means I heavily use the preprocessor directive in Runtime Assembly:

#if UNITY_EDITOR
// code
#endif

Usage Example:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using CustomEditorNamespace;
#endif

class RuntimeClass
{
    [ListDrawerSettings(CustomAddFunction = "CustomAddFunction", CustomRemoveElementFunction = "CustomRemoveFunction")
    public List<OtherClass> SomeList;
    public string SomeField;
}

#if UNITY_EDITOR
    private void CustomAddFunction()
    {
       // Some operations on Assets and Scriptable Objects
        var element = ScriptableObjectEditorUtils.GenericCustomListAddFunction<OtherClass>(this, nameof(SomeList), "SomeList");
        SomeField = AssetDatabase.GetAssetPath(element);
    }

    private void CustomRemoveFunction(OtherClass item)
    {
        ScriptableObjectEditorUtils.GenericCustomListRemoveFunction(SomeList, item);
    }
#endif

Example with Build Error

using UnityEngine;
using UnityEditor;  //This line will result in build error but not in the editor
using CustomEditorNamespace; //This line will result in build error but not in the editor

class RuntimeClass
{
    [ListDrawerSettings(CustomAddFunction = "CustomAddFunction", CustomRemoveElementFunction = "CustomRemoveFunction")
    public List<OtherClass> SomeList;
    public string SomeField;
}

#if UNITY_EDITOR
    private void CustomAddFunction()
    {
       // Some operations on Assets and Scriptable Objects
        var element = ScriptableObjectEditorUtils.GenericCustomListAddFunction<OtherClass>(this, nameof(SomeList), "SomeList");
        SomeField = AssetDatabase.GetAssetPath(element);
    }

    private void CustomRemoveFunction(OtherClass item)
    {
        ScriptableObjectEditorUtils.GenericCustomListRemoveFunction(SomeList, item);
    }
#endif

And because I heavily rely on auto-completion for using directive, all the usings are added automatically (usually outside of preprocessor directive) which results in errors not visible in the editor, that turns out during the build. Because the build lasts everywhere from 10minutes to 1 hour or longer they are pretty annoying to go back to when the CI slot will be free and my build resulted in an error.

This feature would result in great improvements in productivity when we would not have to go back to the code with such errors.

The cherry on the cake would be if auto-cleanup would find such issues and fix them automatically.

PS: I hope I didn't miss a setting that is already part of R# that does exactly that 🤞

Muchaszewski avatar Nov 13 '20 11:11 Muchaszewski

I would love this! There should be suggestion (lightbulb) to put things in #if UNITY_EDITOR

spellfusion avatar Aug 05 '24 15:08 spellfusion