RoleSelectView.cs 2.3 KB

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