RoleSelectView.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. GlobalData.localPK_playerRoleIDs[0] = LoginMgr.myUserInfo.avatarID;
  15. RenderPlayer(0);
  16. RenderPlayer(1);
  17. }
  18. void OnDestroy() {
  19. HomeMgr.RemoveCacheView(this);
  20. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  21. }
  22. public bool OnMenuBack() {
  23. Destroy(gameObject);
  24. return true;
  25. }
  26. void RenderPlayer(int playerIndex) {
  27. Transform root = this.transform.Find("Player" + (playerIndex + 1));
  28. int roleID = GlobalData.localPK_playerRoleIDs[playerIndex];
  29. if (!inited[playerIndex]) {
  30. inited[playerIndex] = true;
  31. Button left = root.Find("Left").GetComponent<Button>();
  32. left.onClick.AddListener(delegate() {
  33. AudioMgr.ins.PlayBtn();
  34. NextRole(playerIndex, -1);
  35. RenderPlayer(playerIndex);
  36. });
  37. Button right = root.Find("Right").GetComponent<Button>();
  38. right.onClick.AddListener(delegate() {
  39. AudioMgr.ins.PlayBtn();
  40. NextRole(playerIndex, +1);
  41. RenderPlayer(playerIndex);
  42. });
  43. }
  44. (Sprite sprite, string name) = RoleMgr.GetRoleInfo(roleID);
  45. root.Find("Avatar").GetComponent<Image>().sprite = sprite;
  46. root.Find("Name").GetComponent<Text>().text = name;
  47. }
  48. void NextRole(int playerIndex, int variety) {
  49. GlobalData.localPK_playerRoleIDs[playerIndex] += variety;
  50. if (GlobalData.localPK_playerRoleIDs[playerIndex] < 1) {
  51. GlobalData.localPK_playerRoleIDs[playerIndex] = RoleMgr.roleCount;
  52. } else if (GlobalData.localPK_playerRoleIDs[playerIndex] > RoleMgr.roleCount) {
  53. GlobalData.localPK_playerRoleIDs[playerIndex] = 1;
  54. }
  55. if (GlobalData.localPK_playerRoleIDs[playerIndex] == GlobalData.localPK_playerRoleIDs[1 - playerIndex]) {
  56. NextRole(playerIndex, variety);
  57. }
  58. }
  59. public void back() {
  60. AudioMgr.ins.PlayBtn();
  61. Destroy(this.gameObject);
  62. }
  63. public void StartGame() {
  64. AudioMgr.ins.PlayBtn();
  65. GameObject.Instantiate(SceneResMgr.ins.GetPrefab("PKGameOptionView"));
  66. }
  67. }