Godot 3.5中C#脚本无法实例化(程序集未加载)问题求助
解决Godot 3.5中“Cannot instance script because the project assembly is not loaded”报错
核心原因
你编写的DragAndDropController类没有继承Godot的节点基类,且_Input方法未正确重写引擎的虚方法,导致Godot无法识别并实例化这个脚本。
修正步骤
- 让类继承Godot节点类:将
DragAndDropController继承自Node(或适合你场景的子类,比如Node2D),这是Godot脚本实例化的必要条件。 - 正确重写
_Input方法:添加override关键字,因为_Input是Godot节点的虚方法,必须显式重写才能被引擎调用。
修正后的代码:
using Godot; using System; public class DragAndDropController : Node { public override void _Input(InputEvent @event) { // Mouse in viewport coordinates. if (@event is InputEventMouseButton eventMouseButton) GD.Print("Mouse Click/Unclick at: ", eventMouseButton.Position); else if (@event is InputEventMouseMotion eventMouseMotion) GD.Print("Mouse Motion at: ", eventMouseMotion.Position); } }
额外检查项
- 确保脚本已正确附加到场景中的某个节点(比如Sprite或空Node),而非未关联任何节点。
- 检查Godot编辑器的输出面板是否有隐藏的编译错误:虽然Visual Studio无报错,但Godot 3.5对.NET 7.0的兼容性不如.NET 6.0,建议切换到.NET 6.0 SDK尝试。
- 确认脚本文件路径
res://scripts/DragAndDropController.cs无拼写错误,文件存在于指定目录。
内容的提问来源于stack exchange,提问作者Ian
相关产品推荐
相关产品推荐

