当前位置:   article > 正文

unity学习(57)——选择角色界面--删除角色2

unity学习(57)——选择角色界面--删除角色2

1.客户端添加点击按钮所触发的事件,在selectMenu界面中增加myDelete函数,当点击“删除角色”按钮时触发该函数的内容。

  1. public void myDelete()
  2. {
  3. string message = nowPlayer.id;
  4. //string m = Coding<StringDTO>.encode(message);
  5. NetWorkScript.getInstance().sendMessage(Protocol.USER, 0, 6, message);
  6. Debug.Log(message);
  7. }

2.服务器也成功接受到来自客户端的内容,这里有瑕疵,此时是明文通信。

3.完善remove函数中的内容,这个大改了,从最终的测试结果来看是成功的:

  1. public PlayerModel[] remove(string accId, string playerId)
  2. {
  3. //确实到这里的,但是逻辑需要大改
  4. Console.WriteLine("remove:");
  5. Console.WriteLine(accId);
  6. Console.WriteLine(playerId);
  7. if (this.userPlayerIds.ContainsKey(accId))//当前账号是否注册
  8. {
  9. List<string> stringList1;
  10. this.userPlayerIds.TryGetValue(accId, out stringList1);//得到当前账号下所有的角色信息
  11. if (stringList1 != null && stringList1.Contains(playerId))
  12. {
  13. stringList1.Remove(playerId);//删除或者怎加以后还要写入,类似的操作不久前刚做过的
  14. //借鉴之前put中的相关操作,其实可以很快完成的
  15. //写userPlayerIds.txt
  16. //List<string> stringList;
  17. List<string> stringList_out;
  18. this.userPlayerIds.TryRemove(accId, out stringList_out);//删旧的
  19. bool ur=this.userPlayerIds.TryAdd(accId, stringList1);//写新的--
  20. //原来账号下三个角色,此时应该就剩下2个了
  21. if (ur)//这次应该没问题了
  22. {
  23. StreamWriter file = new StreamWriter("userPlayerIds.txt");
  24. string json = JsonConvert.SerializeObject(this.userPlayerIds);
  25. Console.WriteLine("userPlayerIds.TryAdd:" + json);
  26. file.Write(json);
  27. file.Close();
  28. Console.WriteLine("userPlayerIds删除完成");
  29. }
  30. //到此位置删除 并 文件写入 userPlayerIds
  31. //下面删除players.txt中的内容
  32. bool ur1=this.players.TryRemove(playerId, out PlayerModel _);//这里直接删就行,不用删完再加了
  33. if (ur1)//这次应该没问题了
  34. {
  35. StreamWriter file = new StreamWriter("players.txt");
  36. string json = JsonConvert.SerializeObject(this.players);
  37. Console.WriteLine("userPlayerIds.TryAdd:" + json);
  38. file.Write(json);
  39. file.Close();
  40. Console.WriteLine("players删除完成");
  41. }
  42. }
  43. }
  44. //返回playermodel数组--自己改的
  45. List<string> stringList;
  46. this.userPlayerIds.TryGetValue(accId, out stringList);//创建第一个角色,这里是空的--之前注册角色的id(PlayerIds)--专属名词调试几遍就会了
  47. if (stringList == null)
  48. return (PlayerModel[])null;
  49. PlayerModel[] playerModelArray = new PlayerModel[stringList.Count];
  50. for (int index = 0; index < stringList.Count; ++index)
  51. this.players.TryGetValue(stringList[index], out playerModelArray[index]);
  52. return playerModelArray;
  53. }

4.返回值的部分也需要做对应的修改:command用1,直接更新列表

 5.最后成功删除角色:

6.补充

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/228913
推荐阅读
相关标签
  

闽ICP备14008679号