Unity开发NodeEditor脚本报CS0101/CS0111重复定义错误如何解决
Unity NodeEditor 编译错误排查方案
问题复现代码
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; public class NodeEditor : EditorWindow { [MenuItem("Editors/Node Editor")] private static void Init () { EditorWindow w = EditorWindow.GetWindow(typeof(NodeEditor), true,"Node Editor"); w.Show (); } private void OnGUI () { SetupHeader (); NodeLoop (); Repaint (); } private void SetupHeader () { Rect header = r (0, 0, width, 90); GUI.Box (header, ""); Rect placeNode = o (header, 20, 5, 100, 20); if (GUI.Button(placeNode, "Place Node")); { } } private void NodeLoop() { } private float width { get { return Screen.width; } } private float height { get { return Screen.height; } } private Rect r (float xPos, float yPos, float xSize, float ySize) { return new Rect (new Vector2(xPos,yPos), new Vector2(xSize,ySize)); } private Rect o (Rect r,float xOff, float yOff, float xSize, float ySize) { return new Rect (new Vector2(r.position.x + xOff, r.position.y + yOff), new Vector2(xSize,ySize)); } }
编译报错信息
Assets\Node Editor\Editor\NodeEditor.cs(6,14): error CS0101: 命名空间''已包含“NodeEditor”的定义
Assets\Node Editor\Editor\NodeEditor.cs(9,24): error CS0111: 类型“NodeEditor”已定义了具有相同参数类型的名为“Init”的成员
Assets\Node Editor\Editor\NodeEditor.cs(15,17): error CS0111: 类型“NodeEditor”已定义了具有相同参数类型的名为“OnGUI”的成员
解决步骤
- 优先处理CS0101重名类错误:这类报错说明项目中已经存在另一个同名的
NodeEditor类。在项目视图全局搜索class NodeEditor,检查是否有插件自带、旧版本备份、其他协作成员写的脚本用了相同类名。找到重名项后,把自己写的自定义类重命名为不冲突的名称(比如CustomWorkflowNodeEditor),同时保证文件名和类名一致即可。 - 处理CS0111重复成员错误:如果确认没有重名类仍然报这个错,直接在当前脚本内搜索
Init和OnGUI两个方法名,大概率是之前复制粘贴代码时,不小心把两个方法的定义重复粘贴了一遍,删掉多余的重复方法定义即可。 - 排查残留文件:如果以上两步都没找到问题,检查脚本所在的
Node Editor/Editor目录,看是否存在隐藏的、自动生成的同名.cs文件(比如Unity导入资源时生成的临时脚本、版本控制拉取时残留的重复文件),删除这类无效残留文件后重新编译。 - 附带修复代码里的隐性bug:你写的按钮判断行
if (GUI.Button(placeNode, "Place Node"));末尾多了一个分号,这个问题不会触发编译报错,但会导致if条件和后面的执行代码块完全脱钩,后续写按钮逻辑时不会正常触发,记得把这个多余的分号删掉。
内容的提问来源于stack exchange,提问作者PoshBoi01
相关产品推荐
相关产品推荐

