using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; /* 界面-PK匹配中 */ public class PKMatchingView : MonoBehaviour { [SerializeField] Sprite[] matchHeadBGList; float waitingTime = 0; //viewUUID [NonSerialized] public string viewUUID = System.Guid.NewGuid().ToString(); //邀请者需要输入的信息-好友PK [NonSerialized] public int targetInvitePlayerID; [NonSerialized] public bool isFriendPKInviter; //被邀请者需要输入的信息-好友PK [NonSerialized] public string targetJoinRoomKey; [NonSerialized] public bool isFriendPKInvitee; //邀请者需要输入的信息-再来一次 [NonSerialized] public bool isTryAgainInviter; //被邀请者需要输入的信息-再来一次 [NonSerialized] public string targetJoinTryAgainRoomKey; [NonSerialized] public bool isTryAgainInvitee; public static PKMatchingView Create() { if (ins && ins.gameObject) Destroy(ins.gameObject); GameObject o = GameObject.Instantiate(Resources.Load("Prefabs/Views/PKMatchingView")); o.SetActive(true); return o.GetComponent(); } public void InitForInviterToTryAgain() { //get other info int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2; MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex]; //set config this.isTryAgainInviter = true; } public static PKMatchingView ins; public void Awake() { ins = this; } public void OnDestroy() { if (ins == this) ins = null; PKComp.ins.cancelRandomMatch(); } void Start() { Sprite avatar = RoleMgr.GetAvatar(LoginMgr.myUserInfo.avatarID); string nickname = LoginMgr.myUserInfo.nickname; RenderPlayerInfo(1, avatar, nickname, true); RenderPlayerInfo(2, null, "", false); if (isFriendPKInviter || isFriendPKInvitee) { RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting1")); } else if (isTryAgainInviter || isTryAgainInvitee) { RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting2")); } else { RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-waiting3")); } if (isFriendPKInviter) { PKComp.ins.inviteFriendGamePK(targetInvitePlayerID, viewUUID); } else if (isFriendPKInvitee) { PKComp.ins.acceptFriendGamePK(targetJoinRoomKey); } else if (isTryAgainInviter) { PKComp.ins.inviteOtherTryAgainGamePK(viewUUID); } else if (isTryAgainInvitee) { PKComp.ins.acceptOtherTryAgainGamePK(targetJoinTryAgainRoomKey); } else { PKComp.ins.randomMatch(); } } public void EnterGameSceneOnMatchSuccess() { int otherIndex = (GlobalData.playerIndexInRoom + 1) % 2; MatchPlayerInfo info = GlobalData.matchPlayerInfos[otherIndex]; RenderPlayerInfo(2, RoleMgr.GetAvatar(info.avatarID), info.nickname, true); RenderTip(TextAutoLanguage2.GetTextByKey("pk-match_match-success")); HideBtnBack(); PauseWaitingTime(); JC.Unity.CoroutineStarter.Start(LoadSceneOnEnterGame()); } IEnumerator LoadSceneOnEnterGame() { yield return new WaitForSecondsRealtime(1); GameMgr.gameType = GlobalData.matchGameType; if (GameMgr.gameType == 9) { SceneManager.LoadScene("Game", LoadSceneMode.Single); } else { SceneManager.LoadScene("GameChallenge", LoadSceneMode.Single); } } void Update() { if (waitingTime >= 0) { waitingTime += Time.deltaTime; this.transform.Find("BoxRight/TimeBG").GetComponentInChildren().text = TimeUtil.GetTimeStr(waitingTime, false); } } void PauseWaitingTime() { waitingTime = -1; } void RenderPlayerInfo(int playerID, Sprite avatar, string nickname, bool active) { this.transform.Find($"BoxRight/Player{playerID}/NameBox") .GetComponentInChildren().text = active ? nickname : TextAutoLanguage2.GetTextByKey("pk-match_wait-to-join"); Transform avatarT = this.transform.Find($"BoxRight/Player{playerID}/MatchHeadBG/Avatar"); avatarT.gameObject.SetActive(active); avatarT.Find("Sprite").GetComponent().sprite = avatar; } void ChangeMatchHeadBG(int typeIndex) { Image img = this.transform.Find("BoxRight/Player2/MatchHeadBG").GetComponent(); img.sprite = matchHeadBGList[typeIndex]; img.GetComponent