Unity3D自学笔记——UGUI背包系统(四)角色物品数据建立
来源:互联网 发布:域名dns被劫持 编辑:程序博客网 时间:2024/05/02 02:33
角色物品数据建立
ItemInfo 为游戏物品数据,背包中的信息应该是角色自身的物品,之前ItemInfo只是用于测试背包效果。
UserItem对象
表结构
相对于ItemInfo就简单多了,关联了userId 和 itemId,实现user 和 item的多对多关系。
一个User 可以拥有一个以上的相同Item
一个Item也可以被多个User拥有
Lv 用来计算当前Item的属性
count 表示每一组Item的现有数量, itemInfo只规定了最大数量
whereType 表示item所在位置,角色身上,背包里,仓库
建立服务端对象
UserItemEntity
public enum WhereType { Knapsack, Body, Warehouse, } [Serializable] public class UserItemEntity : IEntityIntKey { public virtual int ID { get; set; } public virtual UserEntity UserInfo { get; set; } public virtual ItemEntity ItemInfo { get; set; } public virtual int Count { get; set; } public virtual int Lv { get; set; } public virtual WhereType WhereType { get; set; } }
UserItemMap
public class UserItemMap : ClassMap<UserItemEntity> { public UserItemMap() { Table("useritem"); Id(x => x.ID).Column("id"); Map(x => x.Count).Column("count"); Map(x => x.Lv).Column("lv"); Map(x => x.WhereType).Column("WhereType").CustomType<int>(); References(x => x.UserInfo).Column("userid"); References(x => x.ItemInfo).Column("itemid"); } }
UserItemRepository
public class UserItemRepository : Repository<UserItemEntity> { public UserItemRepository(ISession session) : base(session) { } }
GetUserItemCommand
用UserID去查找角色所有物品
public class GetUserItemCommand : ICommand { public int UserID { get; set; } }
GetUserItemHandler
public class GetUserItemHandler : ICommandHandler<GetUserItemCommand> { private readonly UserItemRepository userItemRepository; private UnitOfWork unitOfWork; public GetUserItemHandler() { unitOfWork = new UnitOfWork(); userItemRepository = new UserItemRepository(unitOfWork.Session); } public ICommandResult Excute(GetUserItemCommand command) { IEnumerable<UserItemEntity> userItems = userItemRepository.FilterBy(x => x.UserInfo.ID == command.UserID); unitOfWork.Commit(); return new CommandResult<UserItemEntity>(true, userItems); } }
OperationCode
public enum OperationCode : byte { Register, Login, GetHeroInfo, CreateHero, GetItemInfo, GetUserItem, }
AliPeer
protected virtual void HandleGetUserItemOperation(OperationRequest operationRequest, SendParameters sendParameters) { try { object json; operationRequest.Parameters.TryGetValue(operationRequest.OperationCode, out json); UserEntity user = JsonMapper.ToObject<UserEntity>(json.ToString()); var command = Mapper.Map<UserEntity, GetUserItemCommand>(user); OperationResponse response = new OperationResponse((byte)OperationCode.GetUserItem); CommandResult<UserItemEntity> result = commandBus.Submit(command) as CommandResult<UserItemEntity>; if (result.Success) { response.ReturnCode = (short)ReturnCode.Sucess; Dictionary<byte, object> dict = new Dictionary<byte, object>(); string strJson = JsonMapper.ToJson(result.Result); dict.Add((byte)ReturnCode.Sucess, strJson); response.Parameters = dict; } else { response.ReturnCode = (short)ReturnCode.Faild; } SendOperationResponse(response, sendParameters); } catch (Exception ex) { if (AliPeer.log.IsDebugEnabled) { AliPeer.log.DebugFormat("Error: {0}", ex.Message); } } }
客户端调用
同样是需要缓存的数据,但是不是在客户端启动的时候进行缓存,而是在角色进入游戏后进行缓存。
所以修改UIBottom的代码
private void BtnEnterOnClick(PointerEventData obj) { if (m_SelectHero.SelectedUser != null) { PhotonDataCache.AddOrUpdateDataCache(PhotonCacheType.SelectedUser, m_SelectHero.SelectedUser); //初始化角色物品 string json = JsonMapper.ToJson(m_SelectHero.SelectedUser); Dictionary<byte, object> parameter = new Dictionary<byte, object>(); parameter.Add((byte)OperationCode.GetUserItem, json); PhotonManager.Instance.Peer.OpCustom((byte)OperationCode.GetUserItem, parameter, true); //切换场景 StartCoroutine(GameMain.Instance.LoadScene(SceneName.Battle)); } else { Debug.Log("Error"); } }
同样在PhotonManager的回调函数中进行缓存
public void OnOperationResponse(OperationResponse operationResponse) { switch ((OperationCode)operationResponse.OperationCode) { case OperationCode.Login: HandleLoginResponse(operationResponse); break; case OperationCode.Register: HandleRegisterResponse(operationResponse); break; case OperationCode.GetHeroInfo: HandleGetHeroInfoResponse(operationResponse); break; case OperationCode.CreateHero: HandleCreateHeroResponse(operationResponse); break; case OperationCode.GetItemInfo: HandleGetItemInfoResponse(operationResponse); break; case OperationCode.GetUserItem: HandleGetUserItemResponse(operationResponse); break; } }private void HandleGetUserItemResponse(OperationResponse operationResponse) { switch ((ReturnCode)operationResponse.ReturnCode) { case ReturnCode.Sucess: object json; operationResponse.Parameters.TryGetValue((byte)ReturnCode.Sucess, out json); if (json != null) { JsonData data = JsonMapper.ToObject(json.ToString()); new JsonToEntity<UserItemEntity>(PhotonCacheType.UserItem, data).BuildEntityToCache(); } var cache = PhotonDataCache.GetAll(PhotonCacheType.UserItem); if (cache != null && cache.Count > 0) { Debug.Log("角色物品数据初始化成功"); } else { Debug.Log("角色物品数据初始化失败"); } break; } }
提示角色物品数据初始化成功
LitJson序列化遇到的坑
最早UserItem的命名是 ItemEntity Item{get;set;},然后服务端调用JsonMapper.ToJson弄死不会把ItemEntity序列化出来,从吃饭前查到吃饭后,写了一堆测试代码,最后把名字换成ItemInfo,终于问题解决了。
看了LitJson的源代码也没看出问题。
1 0
- Unity3D自学笔记——UGUI背包系统(四)角色物品数据建立
- Unity3D自学笔记——UGUI背包系统(三)物品弹出提示框
- Unity3D自学笔记——UGUI背包系统(五)沿用Attribute设计实现物品等级
- Unity3D自学笔记——UGUI背包系统(七)物品的装备与卸下
- Unity3D自学笔记——UGUI背包系统(八)物品的分类与整理
- Unity3D自学笔记——UGUI背包系统(六)角色换装及属性动态改变
- Unity3D自学笔记——UGUI背包系统(二)UI设计实现及数据绑定
- Unity3D自学笔记——UGUI背包系统(一)ItemData
- Unity3D自学笔记——UGUI背包系统(九)装备对HP影响的逻辑及使用药品
- Unity3D自学笔记——UGUI系统介绍
- Unity3d背包系统(四)—— 设计物品管理类——InventoryManager
- Unity3D——UGUI实现背包系统
- 基于Unity3D(UGUI)的背包系统<四>完结篇
- Unity3D自学笔记——架构应用(九)加载角色数据
- Unity3d背包系统系统(一)——第一个类(Item物品类)
- Unity3D自学笔记——UGUI前台架构
- Unity3d背包系统(三)—— 设计物品类的JSON文件
- Unity3D自学笔记——UGUI 利用遮罩显示3D人物(物体)
- Android Spinner不显示下拉箭头解决方案
- java编程思想读书笔记 第十八章 java I/O系统(第四篇 新I/O)
- D3.python第一部分 使用入门
- Matlab函数调用
- Gather v2.4 – 事件 & 会议 登入页 WordPress主题 免费下载
- Unity3D自学笔记——UGUI背包系统(四)角色物品数据建立
- MySQL初学笔记
- idea使用总结
- 【数据结构与算法004】基本数据结构——树
- C++ STL set 学习笔记
- u-boot-2016.09移植(5)-uart
- 收藏博客
- linux 学习系列之一
- 欧拉计划22