JetBrains Rider 2026.1 Help

Unity 代码检查器

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

代码检查和快速修复示例

  • JetBrains Rider 会显示未知场景、标签和层的警告。

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

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

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

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

    未解析符号快速修复
  • JetBrains Rider 确保您的所有事件函数都有正确的签名:它会突出显示不正确的签名,并提供快速修复以纠正它们:

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

    比较标签快速修复
  • JetBrains Rider 会警告您如果尝试使用 new 关键字创建从 MonoBehaviourScriptableObject 派生的类的新实例。 建议的快速修复是使用 gameObject.AddComponent<T>()ScriptableObject.CreateInstance<T>()

  • 还有针对 [InitializeOnLoad][InitializeOnLoadMethod] 属性的检查,确保它们具有正确的方法或构造函数签名,并且如果类没有静态构造函数,JetBrains Rider 会将冗余的 [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

警告

无法解析组件或可脚本化对象
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

警告

可能无意中绕过了底层 Unity 引擎对象的生命周期检查(对派生自 'UnityEngine.Object' 的类型进行空合并操作会绕过底层 Unity 引擎对象的生命周期检查)
Unity.NoNullCoalescing
resharper_unity_no_null_coalescing_highlighting

已禁用

可能无意中绕过了底层 Unity 引擎对象的生命周期检查(对派生自 'UnityEngine.Object' 的类型进行空传播操作会绕过底层 Unity 引擎对象的生命周期检查)
Unity.NoNullPropagation
resharper_unity_no_null_propagation_highlighting

已禁用

可能无意中绕过了底层 Unity 引擎对象的生命周期检查(对派生自 'UnityEngine.Object' 的类型进行模式匹配空检查会绕过底层 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

警告

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

警告

图层未在 'Tags & Layers' 中定义
Unity.UnknownLayer
resharper_unity_unknown_layer_highlighting

警告

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

警告

相同的快捷键已为另一个菜单项定义
Unity.DuplicateShortcut
resharper_unity_duplicate_shortcut_highlighting

警告

标签未在 'Tags & Layers' 中定义
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 Compiler 在代码实际编译之前的警告。

检查
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

警告

从非只读静态字段加载是不支持的
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

提示

高成本的空值比较
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日