有三个proto文件BaseProto.proto
、MessageProto.proto
和ServerProto.proto
,文件内容如下:
BaseProto.proto:
syntax = "proto3";
package BaseProto;
message MsgBaseData
{
...
}
MessageProto.proto:
syntax = "proto3";
package GameProto;
import BaseProto.proto; // 引入BaseProto.proto
message MsgWeaponSkillData
{
int32 skillID = 1;
int32 curExp = 2;
int32 maxExp = 3;
}
ServerProto.proto:
syntax = "proto3";
package ServerProto.proto;
message ServerWeaponData
{
string guid = 1;
int32 playerID = 2;
string accountName = 3;
int32 weaponID = 4;
int32 weaponLv = 5;
int32 slotID = 6;
}
ServerProto.proto
中的ServerWeaponData
需要添加一个字段,字段类型是GameProto.proto
的MsgWeaponSkillData
。
需要在ServerProto.proto
中import GameProto.proto
, 如下所示:
syntax = "proto3";
package ServerProto.proto;
import GameProto.proto; // 引入GameProto.proto
message ServerWeaponData
{
string guid = 1;
int32 playerID = 2;
string accountName = 3;
int32 weaponID = 4;
int32 weaponLv = 5;
int32 slotID = 6;
GameProto.MsgWeaponSkillData skillData = 7; // 需要加上包名GameProto
}
如果还想使用BaseProto.proto
中的MsgBaseData
, 你不能因为ServerProto.proto
引用了GameProto.proto
, 而GameProto.proto
又引用了BaseProto.proto
,就可以直接在ServerProto.proto
中使用BaseProto.proto
的对象,这里不具有传递性,BaseProto.proto
在ServerProto.proto中
依旧不可见。
有两种方法可以解决:
- 在ServerProto中
import BaseProto.proto
。 - 在ServerProto中
import public GameProto.proto
。
参考资料:
protobuf之导入其他proto文件