You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity打包报错:找不到'Editor'类型或命名空间(版本升级后)

问题背景

原本在Unity 2020.3.0f1中可正常打包的Items.cs脚本,升级至2021.3.9f1后打包时出现编译错误。该脚本包含继承自Editor的自定义编辑器类,并引入了UnityEditor命名空间。

报错信息
Assets\Inventory\Items.cs(31,31): error CS0246: The type or namespace name 'Editor' could not be found (are you missing a using directive or an assembly reference?)
Assets\Inventory\Items.cs(30,5): error CS0246: The type or namespace name 'CustomEditorAttribute' could not be found (are you missing a using directive or an assembly reference?)
Assets\Inventory\Items.cs(30,5): error CS0246: The type or namespace name 'CustomEditor' could not be found (are you missing a using directive or an assembly reference?)
Assets\Inventory\Items.cs(32,28): error CS0115: 'Items.ItemsEditor.OnInspectorGUI()': no suitable method found to override
原代码
using UnityEngine;
using UnityEditor;

public class Items : MonoBehaviour {
   public enum itemTypes { Weapon, Potion, Amulet }
   public enum potionType { Health, Poison, Strong, Beer }
   public enum amuletType { Health, Defense, Speed }

   [Header("Main Settings")]
   public itemTypes ItemTypes;
   [HideInInspector] public GameObject Model;
   [HideInInspector] public Texture2D Icon;
   [HideInInspector] public string itemName;
   [HideInInspector] public int itemNum;

   // Weapon
   [HideInInspector] public float damage;
   [HideInInspector] public float atackSpeed;
   [HideInInspector] public int broken;

   // Potion
   [HideInInspector] public potionType Potion = potionType.Health;
   [HideInInspector] public float healthOfset;
   [HideInInspector] public bool processingEffect;

   // Amulet
   [HideInInspector] public amuletType Amulet = amuletType.Health; 
   [HideInInspector] public float amuletValue;
    
   [CustomEditor(typeof(Items))]
   public class ItemsEditor : Editor {
      public override void OnInspectorGUI() {
         base.OnInspectorGUI();
         Items items = (Items) target;
         DrawMain(items);
         EditorGUILayout.Space();
       
         switch (items.ItemTypes) {
            case itemTypes.Weapon:
               WeaponSettings();
               break;
            case itemTypes.Potion:
               PotionSettings();
               break;
            case itemTypes.Amulet:
               AmuletSettings();
               break;
         }
         serializedObject.ApplyModifiedProperties();
      }

      void DrawMain(Items items) {
         EditorGUILayout.PropertyField(serializedObject.FindProperty("Icon"));
         EditorGUILayout.PropertyField(serializedObject.FindProperty("Model"));
         EditorGUILayout.PropertyField(serializedObject.FindProperty("itemNum"));
         EditorGUILayout.PropertyField(serializedObject.FindProperty("itemName"));
         EditorGUILayout.Space();
      }

      void WeaponSettings() {
         EditorGUILayout.PropertyField(serializedObject.FindProperty("damage"));
         EditorGUILayout.PropertyField(serializedObject.FindProperty("atackSpeed"));
      }

      void PotionSettings() {
         EditorGUILayout.PropertyField(serializedObject.FindProperty("Potion"));
         EditorGUILayout.PropertyField(serializedObject.FindProperty("healthOfset"));
         EditorGUILayout.PropertyField(serializedObject.FindProperty("processingEffect"));
      }

      void AmuletSettings() {
         EditorGUILayout.PropertyField(serializedObject.FindProperty("Amulet"));
         EditorGUILayout.PropertyField(serializedObject.FindProperty("amuletValue"));
      }
   }
}
解决方案

问题根源是Unity 2021版本对编辑器代码的打包逻辑更严格:UnityEditor命名空间下的类仅在编辑器环境中可用,打包时会被自动排除,导致脚本找不到相关类型。

解决步骤如下:

  1. 拆分脚本:将运行时代码与编辑器代码分离
    • 创建Items.cs,仅保留运行时逻辑:
      using UnityEngine;
      
      public class Items : MonoBehaviour {
         public enum itemTypes { Weapon, Potion, Amulet }
         public enum potionType { Health, Poison, Strong, Beer }
         public enum amuletType { Health, Defense, Speed }
      
         [Header("Main Settings")]
         public itemTypes ItemTypes;
         [HideInInspector] public GameObject Model;
         [HideInInspector] public Texture2D Icon;
         [HideInInspector] public string itemName;
         [HideInInspector] public int itemNum;
      
         // Weapon
         [HideInInspector] public float damage;
         [HideInInspector] public float atackSpeed;
         [HideInInspector] public int broken;
      
         // Potion
         [HideInInspector] public potionType Potion = potionType.Health;
         [HideInInspector] public float healthOfset;
         [HideInInspector] public bool processingEffect;
      
         // Amulet
         [HideInInspector] public amuletType Amulet = amuletType.Health; 
         [HideInInspector] public float amuletValue;
      }
      
    • 创建ItemsEditor.cs,仅包含编辑器逻辑,并且将该脚本放在名为Editor的文件夹中(Unity会自动识别该文件夹下的脚本仅用于编辑器,打包时不会包含):
      using UnityEditor;
      using UnityEngine;
      
      [CustomEditor(typeof(Items))]
      public class ItemsEditor : Editor {
         public override void OnInspectorGUI() {
            base.OnInspectorGUI();
            Items items = (Items) target;
            DrawMain(items);
            EditorGUILayout.Space();
         
            switch (items.ItemTypes) {
               case Items.itemTypes.Weapon:
                  WeaponSettings();
                  break;
               case Items.itemTypes.Potion:
                  PotionSettings();
                  break;
               case Items.itemTypes.Amulet:
                  AmuletSettings();
                  break;
            }
            serializedObject.ApplyModifiedProperties();
         }
      
         void DrawMain(Items items) {
            EditorGUILayout.PropertyField(serializedObject.FindProperty("Icon"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("Model"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("itemNum"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("itemName"));
            EditorGUILayout.Space();
         }
      
         void WeaponSettings() {
            EditorGUILayout.PropertyField(serializedObject.FindProperty("damage"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("atackSpeed"));
         }
      
         void PotionSettings() {
            EditorGUILayout.PropertyField(serializedObject.FindProperty("Potion"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("healthOfset"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("processingEffect"));
         }
      
         void AmuletSettings() {
            EditorGUILayout.PropertyField(serializedObject.FindProperty("Amulet"));
            EditorGUILayout.PropertyField(serializedObject.FindProperty("amuletValue"));
         }
      }
      
  2. 验证设置:确保ItemsEditor.cs位于Editor文件夹内,Unity会自动将该文件夹下的代码排除在打包流程外,同时在编辑器中正常生效。

此处理方式可让打包时运行时代码不引用编辑器命名空间,从而解决编译错误,同时保留原有的编辑器功能。

内容的提问来源于stack exchange,提问作者Raleyph

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 03:20:15