using System.Collections; using System.Collections.Generic; using UnityEngine; using JCEngineCore; using UnityEngine.SceneManagement; using DG.Tweening; public class UserPlayer : JCEntity { public static UserPlayer ins; public UserPlayer() { ins = this; } public static void ConnectServer() { if (ins != null) return; if (HomeMgr.ins) HomeMgr.ins.ShowAuthLoginMask(true); JCEngine.boot(CommonConfig.businessServerWsURI, new UserPlayer()); } bool canReconnnect = true; public override void onLoad() { Debug.Log("UserPlayer onLoad()"); authToken(); } public override void onReload() { Debug.Log("UserPlayer onReload()"); authToken(); } public override void onDestroy() { Debug.Log("UserPlayer onDestroy()"); if (canReconnnect) { JCEngine.reboot(this); } } public override void onMiss() { Debug.Log("UserPlayer onMiss()"); if (canReconnnect) { JCEngine.reboot(this); } } public void Close() { if (ins == this) ins = null; this.canReconnnect = false; this.channel.close(); Debug.Log("user player close"); } //向服务端发送的请求 public void authToken() { string p0 = PlayerPrefs.GetString("IdAndToken", ""); #if UNITY_EDITOR if (string.IsNullOrEmpty(p0)) { p0 = "12&f29f72fcba4b4a63aa4f965b5d603a4e"; } #endif call("authToken", p0); } //被服务端调用的函数 public void onAuthRes(bool res) { Debug.Log("onAuthRes," + res); if (res) { UserComp.ins.getUserInfo(delegate(UserInfo userInfo) { LoginMgr.myUserInfo = userInfo; if (HomeView.ins) { HomeView.ins.RenderMyAvatarSprite(); HomeView.ins.RenderNameOrGender(); HomeView.ins.RenderDeviceNames(); } if (HomeMgr.ins) { HomeMgr.ins.ShowAuthLoginMask(false); } }); } else { //clear token info PlayerPrefs.DeleteKey("IdAndToken"); //tip PopupMgr.ins.ShowTip("登录Token失效!"); //delay back login or quit Sequence seq = DOTween.Sequence(); seq.AppendInterval(1.5f); seq.AppendCallback(() => { if (SceneManager.GetActiveScene().name == "Home") { SceneManager.LoadScene("Login", LoadSceneMode.Single); } else { Application.Quit(); } }); //close socket Close(); } } public void onInvitePK(int avatarID, string nickname, int roomID, int roomType) { string tip = ""; if (roomType == 0) { tip = "好友邀请你参加静止靶PK"; } System.Action cb = delegate() { if (SceneManager.GetActiveScene().name == "Home") { PKMatchingView view = PKMatchingView.Create(); view.isFriendPKInvitee = true; view.targetJoinRoomID = roomID; GlobalData.pkMatchType = PKMatchType.OnlinePK; GlobalData.matchRoomType = roomType; } }; PopupMgr.ins.ShowPKInviteNotice(avatarID, nickname, tip, cb); } }