Protobuf导入失效:指定proto_path仍提示类型未定义
问题原因
你遇到的错误是因为跨包引用未使用全限定名:
PlayerAppearance定义在tw.waterballsa.protobuf.commons包中operations.proto的包是tw.waterballsa.protobuf.operations
Protobuf 默认只会在当前包内查找类型,直接写PlayerAppearance无法定位到其他包的类型。
解决方案
有两种常用的解决方式:
方式一:使用全限定包名引用
修改 operations.proto 中的字段定义,加上目标类型的完整包路径:
message PlayerEnterSceneOperation { tw.waterballsa.protobuf.commons.PlayerAppearance appearance = 1; }
方式二:使用导入别名简化引用(Protobuf 3.15+ 支持)
如果觉得全限定名过于冗长,可以通过别名语法缩短引用:
syntax = "proto3"; import "commons.proto"; package tw.waterballsa.protobuf.operations; // 为目标包定义别名 import commons = "tw.waterballsa.protobuf.commons"; message PlayerEnterSceneOperation { commons.PlayerAppearance appearance = 1; }
修改后重新执行你的编译命令即可正常生成代码。
内容的提问来源于stack exchange,提问作者OOD Waterball
相关产品推荐
相关产品推荐

