ReSharper 2026.1 Help

Unity 代码检查

ReSharper 的代码检查功能同样支持针对 Unity 的 C# 代码。 您可以在 通过代码检查查找代码问题 部分的相应主题中找到有关这些功能的详细信息。

代码检查和快速修复示例

  • ReSharper 会对未知场景、标签和层显示警告。

  • 启用 解决方案范围分析 时,ReSharper 会理解隐式使用的字段和事件函数,并不会将它们标记为未访问。

  • 空的事件函数会显示为无用代码,并提供 快速修复 以将其移除。

  • NetworkBehaviour 之外的任何类中使用 SyncVarAttribute 会被视为错误。

  • 建议的快速修复是从未解析符号的用法创建一个序列化字段:

  • ReSharper 确保您的所有事件函数都有正确的签名:它会高亮显示不正确的签名并提供快速修复以纠正它们:

  • ReSharper 会警告您不要使用 tag 属性进行低效的字符串字面量比较,并提供快速修复以将其重写为调用 CompareTag

  • ReSharper 会在您尝试使用 new 关键字创建从 MonoBehaviourScriptableObject 派生的类的新实例时发出警告。 建议的快速修复是使用 gameObject.AddComponent<T>()ScriptableObject.CreateInstance<T>()

  • 还提供了对 [InitializeOnLoad][InitializeOnLoadMethod] 属性的检查,确保它们具有正确的方法或构造函数签名,并且如果类没有静态构造函数,ReSharper 会将冗余的 [InitializeOnLoad] 属性变灰,并提供快速修复以快速移除该属性或创建构造函数。

Unity 专用代码检查列表

Unity(47 项检查)

此类别中的检查报告特定于 Unity 项目的代码问题。

检查
ID
EditorConfig 属性

默认严重性

可用的非分配方法替代方案
Unity.PreferNonAllocApi
resharper_unity_prefer_non_alloc_api_highlighting

警告

在继承自 'PropertyDrawer' 的类中避免使用 'base.OnGUI()'
Unity.PropertyDrawerOnGUIBase
resharper_unity_property_drawer_on_gui_base_highlighting

警告

无法解析组件或 ScriptableObject
Unity.UnresolvedComponentOrScriptableObject
resharper_unity_unresolved_component_or_scriptable_object_highlighting

警告

DOTS:IAspect 字段必须为以下类型之一:RefRW、RefRO、EnabledRefRW、EnabledRefRO、DynamicBuffer 或 Entity
Unity.Entities.AspectWrongFieldsType
resharper_unity_entities_aspect_wrong_fields_type_highlighting

错误

DOTS:类型关键字不一致
Unity.Entities.InconsistentModifiersForDotsInheritor
resharper_unity_entities_inconsistent_modifiers_for_dots_inheritor_highlighting

错误

DOTS:指定的类型必须为结构体类型
Unity.Entities.MustBeStructForDotsInheritor
resharper_unity_entities_must_be_struct_for_dots_inheritor_highlighting

错误

已声明同名事件函数
Unity.DuplicateEventFunction
resharper_unity_duplicate_event_function_highlighting

警告

期望类型为 'Component' 或 'MonoBehaviour' 的派生类型
Unity.ExpectedComponent
resharper_unity_expected_component_highlighting

警告

期望类型为 'ScriptableObject' 的派生类型
Unity.ExpectedScriptableObject
resharper_unity_expected_scriptable_object_highlighting

警告

'ComponentLookup' 类型的字段在使用前必须先更新。
Unity.Entities.NotUpdatedComponentLookup
resharper_unity_entities_not_updated_component_lookup_highlighting

警告

文件名应与程序集名称一致
MISMATCHED_ASMDEF_FILENAME
resharper_mismatched_asmdef_filename_highlighting

建议

未定义分组路径
Odin.OdinUnknownGroupingPath
resharper_odin_odin_unknown_grouping_path_highlighting

警告

方法签名不正确
Unity.IncorrectMethodSignature
resharper_unity_incorrect_method_signature_highlighting

警告

Odin 分组属性类型不正确
Odin.OdinMemberWrongGroupingAttribute
resharper_odin_odin_member_wrong_grouping_attribute_highlighting

警告

成员同时出现在多个 UI 分组中
Odin.OdinMemberPresentInMultipleGroups
resharper_odin_odin_member_present_in_multiple_groups_highlighting

警告

字符串字面量中引用的方法签名不符合预期
Unity.IncorrectMethodSignatureInStringLiteral
resharper_unity_incorrect_method_signature_in_string_literal_highlighting

警告

'MonoBehaviour' 实例必须通过 'GameObject.AddComponent<T>()' 实例化,而非 'new'
Unity.IncorrectMonoBehaviourInstantiation
resharper_unity_incorrect_mono_behaviour_instantiation_highlighting

警告

参数应为 'Component' 的派生类型
Unity.ParameterNotDerivedFromComponent
resharper_unity_parameter_not_derived_from_component_highlighting

警告

属性可能被错误地应用于多个字段
Unity.PossibleMisapplicationOfAttributeToMultipleFields
resharper_unity_possible_misapplication_of_attribute_to_multiple_fields_highlighting

警告

对派生自 'UnityEngine.Object' 的类型进行 null 合并操作会绕过底层 Unity 引擎对象的生命周期检查,可能导致生命周期检查被非预期绕过
Unity.NoNullCoalescing
resharper_unity_no_null_coalescing_highlighting

已禁用

对派生自 'UnityEngine.Object' 的类型进行 null 传播操作会绕过底层 Unity 引擎对象的生命周期检查,可能导致生命周期检查被非预期绕过
Unity.NoNullPropagation
resharper_unity_no_null_propagation_highlighting

已禁用

对派生自 'UnityEngine.Object' 的类型进行模式匹配 null 检查会绕过底层 Unity 引擎对象的生命周期检查,可能导致生命周期检查被非预期绕过
Unity.NoNullPatternMatching
resharper_unity_no_null_pattern_matching_highlighting

已禁用

建议优先使用基于 GUID 的程序集引用
Unity.PreferGuidReference
resharper_unity_prefer_guid_reference_highlighting

提示

属性用法多余(应用于此声明类型时属性多余)
Unity.RedundantAttributeOnTarget
resharper_unity_redundant_attribute_on_target_highlighting

警告

属性用法多余('FormerlySerializedAs' 属性多余)
Unity.RedundantFormerlySerializedAsAttribute
resharper_unity_redundant_formerly_serialized_as_attribute_highlighting

警告

属性用法多余('HideInInspector' 属性多余)
Unity.RedundantHideInInspectorAttribute
resharper_unity_redundant_hide_in_inspector_attribute_highlighting

警告

属性用法多余('InitializeOnLoad' 属性多余)
Unity.RedundantInitializeOnLoadAttribute
resharper_unity_redundant_initialize_on_load_attribute_highlighting

警告

属性用法多余('SerializeField' 属性多余)
Unity.RedundantSerializeFieldAttribute
resharper_unity_redundant_serialize_field_attribute_highlighting

警告

Unity 事件函数多余
Unity.RedundantEventFunction
resharper_unity_redundant_event_function_highlighting

警告

多次访问内置组件的属性效率低下
Unity.InefficientPropertyAccess
resharper_unity_inefficient_property_access_highlighting

已禁用

场景不存在
Unity.LoadSceneUnexistingScene
resharper_unity_load_scene_unexisting_scene_highlighting

警告

场景在构建设置中被禁用
Unity.LoadSceneDisabledSceneName
resharper_unity_load_scene_disabled_scene_name_highlighting

警告

'ScriptableObject' 实例必须通过 'ScriptableObject.CreateInstance<T>()' 实例化,而非 'new'
Unity.IncorrectScriptableObjectInstantiation
resharper_unity_incorrect_scriptable_object_instantiation_highlighting

警告

对象实例化后立即设置 'parent' 属性效率低下
Unity.InstantiateWithoutParent
resharper_unity_instantiate_without_parent_highlighting

警告

场景短名称不唯一
Unity.LoadSceneAmbiguousSceneName
resharper_unity_load_scene_ambiguous_scene_name_highlighting

警告

基于字符串的图形属性查找效率低下
Unity.PreferAddressByIdToGraphicsParams
resharper_unity_prefer_address_by_id_to_graphics_params_highlighting

警告

基于字符串的组件类型查找效率低下
Unity.PreferGenericMethodOverload
resharper_unity_prefer_generic_method_overload_highlighting

警告

构建设置中缺少索引
Unity.LoadSceneWrongIndex
resharper_unity_load_scene_wrong_index_highlighting

警告

输入名称未在 Input 管理器中定义
Unity.UnknownInputAxes
resharper_unity_unknown_input_axes_highlighting

警告

'标签与图层' 中未定义该层
Unity.UnknownLayer
resharper_unity_unknown_layer_highlighting

警告

项目中未定义该资源
Unity.UnknownResource
resharper_unity_unknown_resource_highlighting

警告

已为其他菜单项定义了相同的快捷键
Unity.DuplicateShortcut
resharper_unity_duplicate_shortcut_highlighting

警告

'标签与图层' 中未定义该标签
Unity.UnknownTag
resharper_unity_unknown_tag_highlighting

警告

项目中不存在同名动画状态
Unity.UnknownAnimatorStateName
resharper_unity_unknown_animator_state_name_highlighting

警告

构建设置中不存在同名场景
Unity.LoadSceneUnknownSceneName
resharper_unity_load_scene_unknown_scene_name_highlighting

警告

如需在函数中使用 'GetSingleton' 调用的结果,'OnCreate' 必须包含 'RequireForUpdate' 调用
Unity.Entities.SingletonMustBeRequested
resharper_unity_entities_singleton_must_be_requested_highlighting

警告

请使用 'CompareTag' 替代显式字符串比较
Unity.ExplicitTagComparison
resharper_unity_explicit_tag_comparison_highlighting

警告

Unity Burst 编译器警告(17 项检查)

此类别中的检查在代码实际编译之前报告 Unity Burst 编译器的警告。

检查
ID
EditorConfig 属性

默认严重性

不支持访问托管索引器
Unity.BurstAccessingManagedIndexer
resharper_unity_burst_accessing_managed_indexer_highlighting

警告

不支持访问托管方法
Unity.BurstAccessingManagedMethod
resharper_unity_burst_accessing_managed_method_highlighting

警告

不支持装箱操作
Unity.BurstBoxingNotSupported
resharper_unity_burst_boxing_not_supported_highlighting

警告

Burst:String.Format(format, ...) 参数类型无效
Unity.BurstStringFormatInvalidArgument
resharper_unity_burst_string_format_invalid_argument_highlighting

警告

Burst:变量解析为 System.String,可能导致 Burst 编译调用栈中出现无效的托管方法调用
Unity.BurstLocalStringVariableDeclaration
resharper_unity_burst_local_string_variable_declaration_highlighting

建议

不支持创建托管类型
Unity.BurstCreatingManagedType
resharper_unity_burst_creating_managed_type_highlighting

警告

调试日志函数仅接受字符串
Unity.BurstDebugLogInvalidArgument
resharper_unity_burst_debug_log_invalid_argument_highlighting

警告

函数签名不能包含托管类型
Unity.BurstFunctionSignatureContainsManagedTypes
resharper_unity_burst_function_signature_contains_managed_types_highlighting

警告

不支持从非 readonly 静态字段加载
Unity.BurstLoadingStaticNotReadonly
resharper_unity_burst_loading_static_not_readonly_highlighting

警告

不支持加载托管类型
Unity.BurstLoadingManagedType
resharper_unity_burst_loading_managed_type_highlighting

警告

共享静态类型参数需要非托管约束
Unity.SharedStaticUnmanagedType
resharper_unity_shared_static_unmanaged_type_highlighting

警告

部分 SharedStatic`1.GetOrCreate 重载会导致编译器错误
Unity.BurstSharedStaticCreate
resharper_unity_burst_shared_static_create_highlighting

警告

String.Format(format, ...) 仅接受字符串字面量和常量字符串
Unity.BurstStringFormatInvalidFormat
resharper_unity_burst_string_format_invalid_format_highlighting

警告

不支持 'foreach' 结构
Unity.BurstForeachNotSupported
resharper_unity_burst_foreach_not_supported_highlighting

警告

不支持 try 语句
Unity.BurstTryNotSupported
resharper_unity_burst_try_not_supported_highlighting

警告

在 Burst 中禁止使用 typeof
Unity.BurstTypeofExpression
resharper_unity_burst_typeof_expression_highlighting

警告

不支持写入静态字段
Unity.BurstWriteStaticField
resharper_unity_burst_write_static_field_highlighting

警告

Unity 性能检查(5 项检查)

此类别中的检查报告 Unity 项目中的计算效率低下的模式。

检查
ID
EditorConfig 属性

默认严重性

'Camera.main' 性能开销较大
Unity.PerformanceCriticalCodeCameraMain
resharper_unity_performance_critical_code_camera_main_highlighting

提示

高开销方法调用
Unity.PerformanceCriticalCodeInvocation
resharper_unity_performance_critical_code_invocation_highlighting

提示

高开销 null 比较
Unity.PerformanceCriticalCodeNullComparison
resharper_unity_performance_critical_code_null_comparison_highlighting

提示

乘法运算顺序效率低下
Unity.InefficientMultiplicationOrder
resharper_unity_inefficient_multiplication_order_highlighting

警告

建议使用交错数组或一维数组替代多维数组
Unity.InefficientMultidimensionalArrayUsage
resharper_unity_inefficient_multidimensional_array_usage_highlighting

警告

2026年 5月 8日