JetBrains / JetBrains/resharper-unity

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

Open
#1,919 1 comment 4 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
1.2k
Forks
142
PR merge metrics
No merged PRs in 30d

Description

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:**
``` csharp
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using CustomEditorNamespace;
#endif

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

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

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

**Example with Build Error**
``` csharp
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 SomeList;
public string SomeField;
}

#if UNITY_EDITOR
private void CustomAddFunction()
{
// Some operations on Assets and Scriptable Objects
var element = ScriptableObjectEditorUtils.GenericCustomListAddFunction(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 🤞

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the Runtime Assembly example and its build failure, comparing code with and without #if UNITY_EDITOR around UnityEditor and custom editor usings. Locate the inspection entry point for using directives or runtime-assembly compilation, then define when the diagnostic should appear and whether automatic cleanup is in scope. No repository file or test is named in the issue, so identify the relevant inspection tests before implementing.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, unity
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.