namespace ET.Server { [MessageHandler(SceneType.DungeonRoom)] [FriendOfAttribute(typeof(ET.DungeonRoom))] public class C2Dungeon_VoteOneHandler : MessageLocationHandler<Unit, C2Dungeon_VoteOne> { protected override async ETTask Run(Unit unit, C2Dungeon_VoteOne message) { DungeonRoom dungeonRoom = unit.Scene().GetComponent<DungeonRoom>(); dungeonRoom.SetVote(dungeonRoom.Vote + 1); var info = dungeonRoom.GetGenProtoAndDirtyClear(); var unitComponent = unit.Scene().GetComponent<UnitComponent>(); unitComponent.Broadcast(info, LocationType.GateSession); await ETTask.CompletedTask; } } }
实体通过特性与协议类绑定
using System.Collections.Generic; namespace ET { [ComponentOf(typeof(Scene))] [AutoSync(typeof(DungeonRoomInfo))] public partial class DungeonRoom : Entity, IAwake { public int Vote; public List<long> UnitIdList; } }
using System.Collections.Generic;
namespace ET
{
[ComponentOf(typeof(Scene))] [AutoSync(typeof(DungeonRoomInfo))] public partial class DungeonRoom : Entity, IAwake { public int Vote; public List<long> UnitIdList; }
}
这是 proto 定义
message DungeonRoomInfo
int64 Id = 1;
int32 Vote = 2;
通过绑定自动生成一个 System
//Is auto gen. using System.Collections.Generic; using System.Linq; using Unity.Mathematics; namespace ET { [FriendOfAttribute(typeof(ET.DungeonRoom))] public static partial class DungeonRoomSystemGen { public static DungeonRoomInfo GenCreate(bool isFromPool = false) { DungeonRoomInfo info = DungeonRoomInfo.Create(isFromPool); info.Id = long.MinValue; info.Vote = int.MinValue; return info; } public static DungeonRoomInfo GetGenProto(this DungeonRoom entity, bool syncAll = false) { DungeonRoomInfo info = GenCreate(); if(syncAll || entity.CheckNeedSync("Id")) info.Id = entity.Id; if(syncAll || entity.CheckNeedSync("Vote")) info.Vote = entity.Vote; return info; } public static DungeonRoomInfo GetGenProtoAndDirtyClear(this DungeonRoom entity, bool syncAll = false) { DungeonRoomInfo info = entity.GetGenProto(syncAll); entity.DirtyList.Clear(); return info; } public static void FromProto(this DungeonRoom entity, DungeonRoomInfo info) { if (!CompareHelper.IsEqual(info.Vote, int.MinValue)) entity.Vote = info.Vote; } public static bool CheckNeedSync(this DungeonRoom entity, string propertyName) { if (propertyName == "Id") return true; return entity.DirtyList.Contains(propertyName); } public static void DirtyClear(this DungeonRoom entity) { entity.DirtyList.Clear(); } public static void SetVote(this DungeonRoom entity, int Vote) { if(CompareHelper.IsEqual(entity.Vote, Vote)) return; entity.Vote = Vote; entity.DirtyList.Add("Vote"); } } }
//Is auto gen.
using System.Linq;
using Unity.Mathematics;
[FriendOfAttribute(typeof(ET.DungeonRoom))] public static partial class DungeonRoomSystemGen { public static DungeonRoomInfo GenCreate(bool isFromPool = false) { DungeonRoomInfo info = DungeonRoomInfo.Create(isFromPool); info.Id = long.MinValue; info.Vote = int.MinValue; return info; } public static DungeonRoomInfo GetGenProto(this DungeonRoom entity, bool syncAll = false) { DungeonRoomInfo info = GenCreate(); if(syncAll || entity.CheckNeedSync("Id")) info.Id = entity.Id; if(syncAll || entity.CheckNeedSync("Vote")) info.Vote = entity.Vote; return info; } public static DungeonRoomInfo GetGenProtoAndDirtyClear(this DungeonRoom entity, bool syncAll = false) { DungeonRoomInfo info = entity.GetGenProto(syncAll); entity.DirtyList.Clear(); return info; } public static void FromProto(this DungeonRoom entity, DungeonRoomInfo info) { if (!CompareHelper.IsEqual(info.Vote, int.MinValue)) entity.Vote = info.Vote; } public static bool CheckNeedSync(this DungeonRoom entity, string propertyName) { if (propertyName == "Id") return true; return entity.DirtyList.Contains(propertyName); } public static void DirtyClear(this DungeonRoom entity) { entity.DirtyList.Clear(); } public static void SetVote(this DungeonRoom entity, int Vote) { if(CompareHelper.IsEqual(entity.Vote, Vote)) return; entity.Vote = Vote; entity.DirtyList.Add("Vote"); } }
unitComponent.Broadcast(info, LocationType.GateSession);这行改掉,太丑了