void OnLeave()
 {
     Main.Ins.GameStateMgr.SaveState();
     Main.Ins.GameBattleEx.Pause();
     Main.Ins.StopAllCoroutines();
     Main.Ins.SoundManager.StopAll();
     Main.Ins.BuffMng.Clear();
     Main.Ins.MeteorManager.Clear();
     OnBackPress();
     Main.Ins.ExitState(Main.Ins.FightState);
     if (GameOverlayDialogState.Exist())
     {
         GameOverlayDialogState.Instance.ClearSystemMsg();
     }
     //离开副本
     if (Main.Ins.CombatData.GLevelMode == LevelMode.MultiplyPlayer)
     {
         UdpClientProxy.LeaveLevel();
     }
     else
     {
         FrameReplay.Instance.OnDisconnected();
         U3D.GoBack();
     }
 }
    public void Update()
    {
        ProtoHandler.Update();
        if (!Started)
        {
            //if (OnUpdates != null)
            //    OnUpdates();
            return;
        }

        AccumilatedTime = AccumilatedTime + Convert.ToInt32((Time.deltaTime * 1000));
        while (AccumilatedTime > LogicFrameLength)
        {
            FrameReplay.deltaTime = delta;
            UdpClientProxy.Update();
            LogicFrame();
            AccumilatedTime = AccumilatedTime - LogicFrameLength;
            time           += (LogicFrameLength / 1000.0f);
        }

        if (Main.Ins.CombatData.Replay)
        {
            if (LogicTurnIndex == Main.Ins.CombatData.GRecord.frames.Count)
            {
                OnBattleFinished();
                U3D.PopupTip("回放结束");
            }
        }
    }
 private void Awake()
 {
     Instance = this;
     TcpClientProxy.Init();
     UdpClientProxy.Init();
     actions = new List <FrameCommand>();
 }
    //自己进入房间的消息被服务器处理,返回给自己
    static void ClientJoinRoomRsp(JoinRoomRsp rsp)
    {
        //如果规则是暗杀或者死斗
        //自己进入房间成功时的信息,跳转到选角色界面,角色选择,就跳转到武器选择界面
        //到最后一步确认后,开始同步服务器场景数据.
        if (rsp.result == 1)
        {
            if (Main.Ins.NetWorkBattle.RoomId == -1)
            {
                //选人,或者阵营,或者
                //UnityEngine.Debug.LogError("OnJoinRoom successful");
                //if (MainLobby.Exist)
                //    MainLobby.Instance.Close();
                //if (RoomOptionWnd.Exist)
                //    RoomOptionWnd.Instance.Close();
                Main.Ins.NetWorkBattle.OnEnterRoomSuccessed((int)rsp.roomId, (int)rsp.levelIdx, (int)rsp.playerId);
                UdpClientProxy.Connect((int)rsp.port, (int)rsp.playerId);
                RoomInfo r = Main.Ins.RoomMng.GetRoom((int)rsp.roomId);
                //如果是盟主模式,无需选择阵营
                //if (r.rule == RoomInfo.RoomRule.MZ)
                //    RoleSelectWnd.Instance.Open();
                //else
                //    CampSelectWnd.Instance.Open();
            }
            //U3D.LoadNetLevel((int)rsp.levelIdx, LevelMode.MultiplyPlayer, GameMode.MENGZHU);
        }
        else
        {
            //显示各种错误信息框
            //rsp.reason
            //2未找到
            //3需要退出当前房间
            //1房间满
            UnityEngine.Debug.LogError(string.Format("error:{0}", rsp.reason));
            switch (rsp.reason)
            {
            case 1: U3D.PopupTip("此房间已满,无法进入"); break;

            case 2: U3D.PopupTip("房间已解散"); break;

            case 3: U3D.PopupTip("需要先退出房间"); break;

            //密码不正确
            case 4:
                Main.Ins.EnterState(Main.Ins.PsdEditDialogState); PsdEditDialogState.Instance.OnConfirm = () =>
                {
                    Common.SendJoinRoom((int)rsp.roomId, PsdEditDialogState.Instance.Control("PsdField").GetComponent <UnityEngine.UI.InputField>().text);
                    PsdEditDialogState.Instance.OnBackPress();
                };
                break;
            }
        }
    }
    ////其他人进入我所在的房间,消息发给我
    //static void OnEnterRoomRsp_(JoinRoomRsp rsp)
    //{
    //    //显示某某进入房间的文字
    //    GameOverlayWnd.Instance.InsertSystemMsg(string.Format("{0} 进入房间", rsp.playerNick));
    //}

    //创建房间OK时自动进入房间.
    static void ClientAutoJoinRoom(CreateRoomRsp rsp)
    {
        if (Main.Ins.NetWorkBattle.RoomId == -1)
        {
            //选人,或者阵营,或者
            //if (MainLobby.Exist)
            //    MainLobby.Instance.Close();
            //if (RoomOptionWnd.Exist)
            //    RoomOptionWnd.Instance.Close();
            Main.Ins.NetWorkBattle.OnEnterRoomSuccessed((int)rsp.roomId, (int)rsp.levelId, (int)rsp.playerId);
            UdpClientProxy.Connect((int)rsp.port, (int)rsp.playerId);
            RoomInfo r = Main.Ins.RoomMng.GetRoom((int)rsp.roomId);
            //如果是盟主模式,无需选择阵营
            //if (r.rule == RoomInfo.RoomRule.MZ)
            //    RoleSelectWnd.Instance.Open();
            //else
            //    CampSelectWnd.Instance.Open();
        }
    }
    //void Update () {
    //if (bSync && RoomId != -1 && TurnStarted && MeteorManager.Instance.LocalPlayer != null && !waitReborn)
    //{
    //    if (waitSend)
    //    {
    //        mLogicTempTime += Time.deltaTime;
    //        if (mLogicTempTime > 0.02f)
    //        {
    //            for (int i = 0; i < mFastForwardSpeed; i++)
    //            {
    //                GameTurn();
    //                mLogicTempTime = 0;
    //            }
    //        }

    //        frameIndex++;
    //        tick++;
    //        if (frameIndex % TurnFrame == 0)
    //        {
    //            turn++;
    //            waitSend = false;
    //            SyncInput();
    //            //SyncAttribute(frame.Players[0]);
    //            //Common.SyncFrame(frame);

    //            if (MeteorManager.Instance.LocalPlayer.Dead)
    //            {
    //                //Debug.LogError("waitreborn hp:" + frame.Players[0].hp);

    //                waitReborn = true;
    //            }
    //        }


    //        //36=3秒个turn内没收到服务器回复的同步信息,算作断开连接.
    //        if (tick >= 360)
    //        {
    //            bSync = false;
    //            ReconnectWnd.Instance.Open();
    //            if (GameBattleEx.Instance != null)
    //                GameBattleEx.Instance.NetPause();
    //        }

    //    }
    //    if (Global.useShadowInterpolate)
    //        SyncInterpolate();
    //}
    //}

    //public void SyncInterpolate()
    //{
    //    if (NetWorkBattle.Ins.TurnStarted && MeteorManager.Instance.LocalPlayer != null)
    //    {
    //        //在战场更新中,更新其他角色信息,自己的只上传.
    //        //Debug.Log("SyncInterpolate:" + Time.frameCount);
    //        for (int i = 0; i < MeteorManager.Instance.UnitInfos.Count; i++)
    //        {
    //            MeteorUnit unit = MeteorManager.Instance.UnitInfos[i];
    //            if (unit != null && unit != MeteorManager.Instance.LocalPlayer)
    //            {
    //                //玩家同步所有属性
    //                if (unit.ShadowSynced)
    //                    continue;
    //                //float next = Mathf.Clamp01(unit.ShadowDelta + 0.5f);
    //                //Debug.Log("同步角色位置:" + Time.frameCount);
    //                unit.ShadowDelta += 5 * Time.deltaTime;
    //                unit.transform.position = Vector3.Lerp(unit.transform.position, unit.ShadowPosition, unit.ShadowDelta);
    //                unit.transform.rotation = Quaternion.Slerp(unit.transform.rotation, unit.ShadowRotation, unit.ShadowDelta);
    //                if (unit.ShadowDelta >= 1.0f)
    //                    unit.ShadowSynced = true;
    //            }
    //        }
    //    }
    //}

    //断开连接时.
    public void OnDisconnect()
    {
        if (RoomId != -1)
        {
            UdpClientProxy.Disconnect();
            //在联机战斗场景中.
            Main.Ins.GameBattleEx.Pause();
            Main.Ins.SoundManager.StopAll();
            Main.Ins.BuffMng.Clear();
            Main.Ins.MeteorManager.Clear();
            //if (FightWnd.Exist)
            //    FightWnd.Instance.Close();
            RoomId   = -1;
            RoomName = "";
            FrameReplay.Instance.OnDisconnected();
            FrameIndex = ServerFrameIndex = 0;
            U3D.InsertSystemMsg("与服务器断开链接.");
            //if (!MainWnd.Exist)
            //    U3D.GoBack();
        }
        RoomId     = -1;
        waitReborn = false;
        RoomName   = "";
    }
    public void SetResult(int result)
    {
        if (result == 1)
        {
            for (int i = 0; i < Main.Ins.MeteorManager.UnitInfos.Count; i++)
            {
                if (Main.Ins.MeteorManager.UnitInfos[i].StateMachine != null)
                {
                    Main.Ins.MeteorManager.UnitInfos[i].StateMachine.Stop();
                }
                Main.Ins.MeteorManager.UnitInfos[i].controller.Input.ResetVector();
                Main.Ins.MeteorManager.UnitInfos[i].OnGameResult(result);
            }
        }

        if (Main.Ins.CombatData.GGameMode == GameMode.MENGZHU)
        {
            U3D.InsertSystemMsg("回合结束");
        }
        else
        {
            string mat = "";
            Text   txt;
            switch (result)
            {
            case -1:
            case 0:
                mat = "BattleLose";
                txt = Control("ButterflyWin").GetComponent <Text>();
                U3D.InsertSystemMsg("蝴蝶阵营 获胜");
                txt.text = "1";
                break;

            case 1:
            case 2:
                mat = "BattleWin";
                txt = Control("MeteorWin").GetComponent <Text>();
                U3D.InsertSystemMsg("流星阵营 获胜");
                txt.text = "1";
                break;

            case 3:
                mat = "BattleNone";
                U3D.InsertSystemMsg("和局");
                break;
            }
            BattleResult.GetComponent <Image>().material = Resources.Load <Material>(mat);
            BattleResult.SetActive(true);
            BattleTitle.SetActive(true);
        }
        Control("Close").SetActive(true);
        Control("Close").GetComponent <Button>().onClick.AddListener(() =>
        {
            Main.Ins.GameStateMgr.SaveState();
            Main.Ins.GameBattleEx.Pause();
            Main.Ins.StopAllCoroutines();
            Main.Ins.SoundManager.StopAll();
            Main.Ins.BuffMng.Clear();
            Main.Ins.MeteorManager.Clear();
            OnBackPress();
            Main.Ins.ExitState(Main.Ins.FightState);
            if (GameOverlayDialogState.Exist())
            {
                GameOverlayDialogState.Instance.ClearSystemMsg();
            }
            //离开副本
            if (Main.Ins.CombatData.GLevelMode == LevelMode.MultiplyPlayer)
            {
                UdpClientProxy.LeaveLevel();
            }
            else
            {
                FrameReplay.Instance.OnDisconnected();
                Main.Ins.PlayEndMovie(result == 1);
            }
        });
        Control("SaveRecord").SetActive(true);
        Control("SaveRecord").GetComponent <Button>().onClick.AddListener(() => {
            DialogUtils.Ins.OpenWait("正在保存录像,请稍后");
            //单独开一个线程去保存录像信息.
            RecordMgr.Ins.WriteFile();
            Control("SaveRecord").SetActive(false);//隐藏掉该按钮
        });
    }
 //选择好了角色和武器,向服务器发出进入战场请求.
 public void EnterLevel()
 {
     //加载地图场景-开始和服务器同步历史帧信息.
     //LoadNetLevel();
     UdpClientProxy.EnterLevel(heroIdx, weaponIdx, camp);
 }