RoleSelectView.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. /* PK模式的角色选择界面 */
  7. public class RoleSelectView : MonoBehaviour, MenuBackInterface
  8. {
  9. bool[] inited = {false, false};
  10. void Start()
  11. {
  12. HomeMgr.CacheView(this);
  13. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  14. TopBarView.NeedShowIt(this);
  15. GlobalData.localPK_playerRoleIDs[0] = LoginMgr.myUserInfo.avatarID;
  16. RenderPlayer(0);
  17. RenderPlayer(1);
  18. }
  19. void OnDestroy() {
  20. HomeMgr.RemoveCacheView(this);
  21. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  22. TopBarView.DontNeedShowIt(this);
  23. }
  24. public bool OnMenuBack() {
  25. Destroy(gameObject);
  26. return true;
  27. }
  28. void RenderPlayer(int playerIndex) {
  29. Transform root = this.transform.Find("Player" + (playerIndex + 1));
  30. int roleID = GlobalData.localPK_playerRoleIDs[playerIndex];
  31. if (!inited[playerIndex]) {
  32. inited[playerIndex] = true;
  33. Button left = root.Find("Left").GetComponent<Button>();
  34. left.onClick.AddListener(delegate() {
  35. AudioMgr.ins.PlayBtn();
  36. NextRole(playerIndex, -1);
  37. RenderPlayer(playerIndex);
  38. });
  39. Button right = root.Find("Right").GetComponent<Button>();
  40. right.onClick.AddListener(delegate() {
  41. AudioMgr.ins.PlayBtn();
  42. NextRole(playerIndex, +1);
  43. RenderPlayer(playerIndex);
  44. });
  45. }
  46. (Sprite sprite, string name) = RoleMgr.GetRoleInfo(roleID);
  47. root.Find("Avatar").GetComponent<Image>().sprite = sprite;
  48. root.Find("Name").GetComponent<Text>().text = name;
  49. }
  50. void NextRole(int playerIndex, int variety) {
  51. GlobalData.localPK_playerRoleIDs[playerIndex] += variety;
  52. if (GlobalData.localPK_playerRoleIDs[playerIndex] < 1) {
  53. GlobalData.localPK_playerRoleIDs[playerIndex] = RoleMgr.roleCount;
  54. } else if (GlobalData.localPK_playerRoleIDs[playerIndex] > RoleMgr.roleCount) {
  55. GlobalData.localPK_playerRoleIDs[playerIndex] = 1;
  56. }
  57. if (GlobalData.localPK_playerRoleIDs[playerIndex] == GlobalData.localPK_playerRoleIDs[1 - playerIndex]) {
  58. NextRole(playerIndex, variety);
  59. }
  60. }
  61. public void back() {
  62. AudioMgr.ins.PlayBtn();
  63. Destroy(this.gameObject);
  64. }
  65. public void StartGame() {
  66. AudioMgr.ins.PlayBtn();
  67. JC.Unity.UI.CanvasUtils.PlusSortOrder(
  68. gameObject,
  69. GameObject.Instantiate(SceneResMgr.ins.GetPrefab("PKGameOptionView")),
  70. 1
  71. );
  72. }
  73. }