跳到主要内容
知仓学习社ZHICANG

unity-version-split

Split a C# file into Unity 6.5+ and pre-Unity 6.5 variants. Use when a file needs different implementations for different Unity versions due to API …

不碰外部(只输出文字)无严重或高危命中IvanMurzak/Unity-MCP

它会碰到什么

扫了多少1 个文本文件,4 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

Unity Version Split

Split a C# source file into two variants: one for Unity 6.5+ (UNITY_6000_5_OR_NEWER) and one for older versions (pre-Unity 6.5).

When to Use

Unity 6.5 introduced breaking changes including:

  • EntityId replaces int for instance IDs (GetEntityId() replaces GetInstanceID())
  • EditorUtility.EntityIdToObject(EntityId) replaces EditorUtility.InstanceIDToObject(int)
  • Implicit EntityId <-> int conversions are marked [Obsolete(..., true)] (compile error)

When a file needs different code for these Unity versions, split it into two files following the project convention.

File Naming Convention

| File | Purpose | Preprocessor Guard |

|---|---|---|

| FileName.cs | Unity 6.5+ (newer version) | #if UNITY_6000_5_OR_NEWER |

| FileName.pre-Unity.6.5.cs | Pre-Unity 6.5 (older version) | #if !UNITY_6000_5_OR_NEWER |

For files that also have Editor/Runtime variants, combine the guards:

| File | Guard |

|---|---|

| FileName.Editor.cs | #if UNITY_EDITOR && UNITY_6000_5_OR_NEWER |

| FileName.Editor.pre-Unity.6.5.cs | #if UNITY_EDITOR && !UNITY_6000_5_OR_NEWER |

| FileName.Runtime.cs | #if !UNITY_EDITOR && UNITY_6000_5_OR_NEWER |

| FileName.Runtime.pre-Unity.6.5.cs | #if !UNITY_EDITOR && !UNITY_6000_5_OR_NEWER |

Step-by-Step Process

Step 1 — Read the Source File

Read $ARGUMENTS (the target file path). Identify which parts need version-specific code.

Step 2 — Identify the Split Points

Common differences between Unity 6.5+ and pre-6.5:

| Unity 6.5+ | Pre-Unity 6.5 |

|---|---|

| EntityId | int |

| EntityId.None | 0 |

| go.GetEntityId() | go.GetInstanceID() |

| EditorUtility.EntityIdToObject(entityId) | EditorUtility.InstanceIDToObject(instanceID) |

| ObjectRef.InstanceID is EntityId | ObjectRef.InstanceID is int? |

Step 3 — Create the Two Files

  1. Update the original .cs file to contain only the Unity 6.5+ version:
  • Set the preprocessor guard to include UNITY_6000_5_OR_NEWER
  • Use EntityId, GetEntityId(), etc.
  1. Create the .pre-Unity.6.5.cs file with the pre-6.5 version:
  • Set the preprocessor guard to include !UNITY_6000_5_OR_NEWER
  • Use int, GetInstanceID(), etc.

Both files must:

  • Start with #nullable enable
  • Have the same copyright header as the original
  • Use the same namespace and class name (partial classes)
  • End with #endif matching the opening #if

Step 4 — Refresh Assets

Run assets-refresh tool to let Unity generate .meta files and recompile.

IMPORTANT: Do NOT manually create .meta files. Unity auto-generates them after assets-refresh or any AssetDatabase.Refresh() call.

Step 5 — Verify

Check for compilation errors using console-get-logs tool.

Example

Given GameObjectUtils.Runtime.cs with #if !UNITY_EDITOR that uses both EntityId and int with #if UNITY_6000_5_OR_NEWER inside:

Before (single file with nested #if):

#if !UNITY_EDITOR
// ...
#if UNITY_6000_5_OR_NEWER
        public static GameObject? FindByInstanceID(EntityId instanceID) { ... }
#else
        public static GameObject? FindByInstanceID(int instanceID) { ... }
#endif
#endif

After (two clean files):

GameObjectUtils.Runtime.cs:

#if !UNITY_EDITOR && UNITY_6000_5_OR_NEWER
// ...
        public static GameObject? FindByInstanceID(EntityId instanceID) { ... }
#endif

GameObjectUtils.Runtime.pre-Unity.6.5.cs:

#if !UNITY_EDITOR && !UNITY_6000_5_OR_NEWER
// ...
        public static GameObject? FindByInstanceID(int instanceID) { ... }
#endif

Reference Files

Existing examples of this pattern in the codebase:

  • Runtime/Data/ObjectRef.cs / ObjectRef.pre-Unity.6.5.cs
  • Runtime/Data/GameObjectRef.cs / GameObjectRef.pre-Unity.6.5.cs
  • Runtime/Utils/GameObjectUtils.Editor.cs / GameObjectUtils.Editor.pre-Unity.6.5.cs
  • Tests/Editor/Tool/Assets/AssetsPrefabCreateTests.cs / AssetsPrefabCreateTests.pre-Unity.6.5.cs

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。

它属于哪个仓库

星标★ 4,278
本站分层T1
该仓技能数80
原文件路径Unity-MCP-Plugin/.claude/skills/unity-version-split/SKILL.md

同一个仓库里的其他技能

看这个仓库的全部 80 个技能