RoleSelectView.cs 2.2 KB

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