RoleSelectView.cs 2.2 KB

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