public ObjMyPlayer CreateMainPlayer(InitMyPlayerData initData) { if (null != MyPlayer) { Logger.Error("mMainPlayer != null"); Object.Destroy(MyPlayer.gameObject); MyPlayer = null; } var objRes = ResourceManager.PrepareResourceSync <GameObject>(Resource.PrefabPath.MyPlayer); if (null == objRes) { Logger.Debug("ERROR: Resources.Load[{0}]", Resource.PrefabPath.MyPlayer); return(null); } var p = new Vector3(initData.X, initData.Y, initData.Z); if (initData.DirX == 0 && initData.DirZ == 0) { initData.DirX = 1.0f; } var q = Quaternion.LookRotation(new Vector3(initData.DirX, 0, initData.DirZ)); var obj = Object.Instantiate(objRes, p, q) as GameObject; var avatar = obj.GetComponent <ActorAvatar>(); #if UNITY_EDITOR obj.name = string.Format("[{0}]({1})_{2}", initData.Name, initData.DataId, initData.ObjId); #else obj.name = initData.ObjId.ToString(); #endif var character = obj.GetComponent <ObjCharacter>(); character.Init(initData); obj.SetActive(true); mObjDict.Add(initData.ObjId, character); Logger.Debug("SUCCESS: Create Character[{0}]", character.GetObjId()); MyPlayer = obj.GetComponent <ObjMyPlayer>(); var e1 = new Character_Create_Event(initData.ObjId); EventDispatcher.Instance.DispatchEvent(e1); MyPlayer.TitleList = PlayerDataManager.Instance.TitleList; return(MyPlayer); }
public IEnumerator EnterGameCoroutine() { //加载场景Prefab if (!string.IsNullOrEmpty(ScenePrefab)) { var ret = ResourceManager.PrepareResourceWithHolder <GameObject>(ScenePrefab, true, false); yield return(ret.Wait()); try { var sceneRoot = Instantiate(ret.Resource) as GameObject; if (null != sceneRoot) { sceneRoot.transform.parent = transform; // 优化场景特效 OptList <ParticleSystem> .List.Clear(); sceneRoot.transform.GetComponentsInChildren(true, OptList <ParticleSystem> .List); foreach (var particle in OptList <ParticleSystem> .List) { if (!particle.CompareTag("NoPauseEffect")) { if (particle.gameObject.GetComponent <ParticleOptimizer>() == null) { particle.gameObject.AddComponent <ParticleOptimizer>(); } } } Scene = sceneRoot.GetComponent <Scene>(); if (null != Scene) { Scene.InitPortal(); } else { Logger.Error("cant find Scene in ScenePerfab!!!"); } } SoundManager.Instance.SetAreaSoundMute(SoundManager.Instance.EnableBGM); // var sceneCacheKey = string.Format("{0}.unity3d", ScenePrefab); // ResourceManager.Instance.RemoveFromCache(sceneCacheKey); } catch (Exception e) { Logger.Error("step 0------------------{0}\n{1}", e.Message, e.StackTrace); } } if (ObjManager.Instance == null) { Logger.Log2Bugly("EnterGameCoroutine ObjManager.Instance = null "); yield break; } //清除ObjManager ObjManager.Instance.Reset(); if (PlayerDataManager.Instance == null || PlayerDataManager.Instance.mInitBaseAttr == null) { Logger.Log2Bugly("EnterGameCoroutine PlayerDataManager.Instance = null "); yield break; } var data = PlayerDataManager.Instance.mInitBaseAttr; var attr = new InitMyPlayerData(); //初始化造主角的数据 try { attr.ObjId = data.CharacterId; attr.DataId = data.RoleId; attr.Name = data.Name; attr.Camp = data.Camp; attr.IsDead = data.IsDead == 1; attr.HpMax = data.HpMax; attr.HpNow = data.HpNow; attr.MpMax = data.MpMax; attr.MpNow = data.MpMow; attr.X = data.X; attr.Y = GetTerrainHeight(data.X, data.Y); attr.Z = data.Y; attr.MoveSpeed = data.MoveSpeed; attr.AreaState = (eAreaState)data.AreaState; attr.EquipModel = data.EquipsModel; attr.ModelId = data.ModelId; { var __list1 = data.Buff; var __listCount1 = __list1.Count; for (var __i1 = 0; __i1 < __listCount1; ++__i1) { var buff = __list1[__i1]; { attr.Buff.Add(buff.BuffId, buff.BuffTypeId); } } } } catch (Exception e) { Logger.Error("step 1------------------{0}\n{1}", e.Message, e.StackTrace); } //造主角 ObjMyPlayer player = null; try { player = ObjManager.Instance.CreateMainPlayer(attr); if (player == null) { Logger.Log2Bugly("EnterGameCoroutine player = null "); yield break; } player.AdjustHeightPosition(); } catch (Exception e) { Logger.Error("step 2------------------{0}\n{1}", e.Message, e.StackTrace); } //设置buff try { EventDispatcher.Instance.DispatchEvent(new UIEvent_ClearBuffList()); var count = data.Buff.Count; for (var i = 0; i < count; i++) { var buffResult = data.Buff[i]; EventDispatcher.Instance.DispatchEvent(new UIEvent_SyncBuffCell(buffResult)); } } catch (Exception e) { Logger.Error("step 3------------------{0}\n{1}", e.Message, e.StackTrace); } //预加载技能资源 try { ObjManager.Instance.PrepareMainPlayerSkillResources(); } catch (Exception e) { Logger.Error("step 4------------------{0}\n{1}", e.Message, e.StackTrace); } //给主摄像机设置跟随,设置声音 try { if (MainCamera == null) { Logger.Log2Bugly("EnterGameCoroutine MainCamera = null "); yield break; } MainCamera.GetComponent <CameraController>().FollowObj = player.gameObject; { //audio listener var audioListerner = MainCamera.gameObject.GetComponent <AudioListener>(); if (null != audioListerner) { DestroyObject(audioListerner); } var playerAudio = player.gameObject.GetComponent <AudioListener>(); if (null == playerAudio) { player.gameObject.AddComponent <AudioListener>(); } } } catch (Exception e) { Logger.Error("step 5------------------{0}\n{1}", e.Message, e.StackTrace); } //初始化UI Coroutine co = null; try { co = StartCoroutine(InitUI()); if (null != LoadingLogic.Instance) { LoadingLogic.Instance.SetLoadingProgress(0.95f); } else { Logger.Error("LoadingLogic.Instance==null"); } } catch (Exception e) { Logger.Error("step 6------------------{0}\n{1}", e.Message, e.StackTrace); } if (null != co) { yield return(co); } //控制模块 try { InputManager.Instance.OnMoveDestination = mControl.MoveTo; InputManager.Instance.SelectTarget = mControl.SelectTarget; if (UIManager.Instance.MainUIFrame != null) { var main = UIManager.Instance.MainUIFrame.GetComponent <MainScreenFrame>(); var joystick = main.GetComponentInChildren <JoyStickLogic>(); if (joystick != null) { joystick.OnMoveDirection = mControl.MoveDirection; } SkillBar = main.SkillBar.GetComponent <BattleSkillRootFrame>(); if (SkillBar != null) { SkillBar.OnClickEvent = mControl.OnClickEvent; } } } catch (Exception e) { Logger.Error("step 7------------------{0}\n{1}", e.Message, e.StackTrace); } //UI try { EventDispatcher.Instance.DispatchEvent(new Enter_Scene_Event(Scene.SceneTypeId)); EventDispatcher.Instance.DispatchEvent(new RefresSceneMap(Scene.SceneTypeId)); UIManager.Instance.OpenDefaultFrame(); player.CreateNameBoard(); if (PlayerDataManager.Instance != null) { //根据场景不一样,自动战斗的优先级也不一样 PlayerDataManager.Instance.RefrehEquipPriority(); } } catch (Exception e) { Logger.Error("step 8------------------{0}\n{1}", e.Message, e.StackTrace); } //向服务器请求场景参数 yield return(StartCoroutine(AskSceneExtData())); //向服务器发送切换场景结束的包 if (SceneManager.Instance != null) { yield return(StartCoroutine(SceneManager.Instance.ChangeSceneOverCoroutine())); } else { Logger.Log2Bugly("EnterGameCoroutine SceneManager.Instance = null "); } //客户端切换场景结束事件 try { SceneEffect.OnEnterScecne(); LoadingLogic.Instance.SetLoadingProgress(1.0f); SceneManager.Instance.OnLoadSceneOver(); EventDispatcher.Instance.DispatchEvent(new LoadSceneOverEvent()); } catch (Exception e) { Logger.Error("step 9------------------{0}\n{1}", e.Message, e.StackTrace); } yield return(new WaitForSeconds(0.1f)); //播放CG try { Action brightnessStartWork = () => { LoginWindow.State = LoginWindow.LoginState.InGaming; var bc = Game.Instance.GetComponent <BrightnessController>(); if (bc) { bc.ResetTimer(); } }; if (1 == PlayFirstEnterGameCG) { #if UNITY_EDITOR var skip = true; #else bool skip = true; #endif if (0 == PlayerDataManager.Instance.GetRoleId() || 1 == PlayerDataManager.Instance.GetRoleId() || 2 == PlayerDataManager.Instance.GetRoleId()) { if (int.Parse(Table.GetClientConfig(1205).Value) == 1) { PlayCG.Instance.PlayCGFile("Video/HeroBorn.txt", brightnessStartWork, skip); PlatformHelper.UMEvent("PlayCG", "play", "Video/HeroBorn.txt"); } } EventDispatcher.Instance.DispatchEvent(new FirstEnterGameEvent(true)); PlayFirstEnterGameCG = 0; } else { brightnessStartWork(); } } catch (Exception e) { Logger.Error("step 10------------------{0}\n{1}", e.Message, e.StackTrace); } mControl.OnLoadSceneOver(); LoadOver = true; //优化loading读条速度,所以meshtree放在读条之后再加载 if (null != Scene) { StartCoroutine(DelayLoadTerrainMeshTree(ScenePrefab)); } if (!HasAdjustSetting) { HasAdjustSetting = true; StartCoroutine(ChangeSetting()); } LuaEventManager.Instance.PushEvent("OnEnterGameOver", Scene.SceneTypeId); }
public ObjMyPlayer CreateMainPlayer(InitMyPlayerData initData) { if (null == MyPlayer) { var objRes = ResourceManager.PrepareResourceSync <GameObject>(Resource.PrefabPath.MyPlayer); if (null == objRes) { Logger.Debug("ERROR: Resources.Load[{0}]", Resource.PrefabPath.MyPlayer); return(null); } var p = new Vector3(initData.X, initData.Y, initData.Z); if (initData.DirX == 0 && initData.DirZ == 0) { initData.DirX = 1.0f; } var q = Quaternion.LookRotation(new Vector3(initData.DirX, 0, initData.DirZ)); var obj = Object.Instantiate(objRes, p, q) as GameObject; #if UNITY_EDITOR obj.name = string.Format("[{0}]({1})_{2}", initData.Name, initData.DataId, initData.ObjId); #else obj.name = initData.ObjId.ToString(); #endif MyPlayer = obj.GetComponent <ObjMyPlayer>(); MyPlayer.InitImpl(initData, true); obj.SetActive(true); mObjDict.Add(initData.ObjId, MyPlayer); Logger.Debug("SUCCESS: Create Character[{0}]", MyPlayer.GetObjId()); Object.DontDestroyOnLoad(obj); } else { var p = new Vector3(initData.X, initData.Y, initData.Z); if (initData.DirX == 0 && initData.DirZ == 0) { initData.DirX = 1.0f; } var q = Quaternion.LookRotation(new Vector3(initData.DirX, 0, initData.DirZ)); MyPlayer.transform.position = p; MyPlayer.transform.rotation = q; try { MyPlayer.gameObject.SetActive(true); } catch (Exception e) { Logger.Error("CreateMainPlayer MyPlayer.gameObject.SetActive(true); throw exception:" + e); } MyPlayer.InitImpl(initData, false); mObjDict.Add(initData.ObjId, MyPlayer); Logger.Debug("SUCCESS: Create Character[{0}]", MyPlayer.GetObjId()); } var e1 = new Character_Create_Event(initData.ObjId); EventDispatcher.Instance.DispatchEvent(e1); MyPlayer.TitleList = PlayerDataManager.Instance.TitleList; return(MyPlayer); }
public IEnumerator EnterGameCoroutine() { Stopwatch sw = new Stopwatch(); Stopwatch swTotal = new Stopwatch(); swTotal.Start(); string log = ""; //加载场景Prefab if (!string.IsNullOrEmpty(ScenePrefab)) { sw.Start(); var ret = ResourceManager.PrepareResourceWithHolder <GameObject>(ScenePrefab, true, true, true, true, true); yield return(ret.Wait()); EventDispatcher.Instance.DispatchEvent(new LoadingPercentEvent(0.6f)); try { var sceneRoot = Instantiate(ret.Resource) as GameObject; sw.Stop(); log += "\nScenePrefab---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; if (null != sceneRoot) { sceneRoot.transform.parent = transform; sw.Reset(); sw.Start(); // 优化场景特效 OptList <ParticleSystem> .List.Clear(); sceneRoot.transform.GetComponentsInChildren(true, OptList <ParticleSystem> .List); foreach (var particle in OptList <ParticleSystem> .List) { if (!particle.CompareTag("NoPauseEffect")) { if (particle.gameObject.GetComponent <ParticleOptimizer>() == null) { particle.gameObject.AddComponent <ParticleOptimizer>(); } } } sw.Stop(); log += "\nParticleOptimizer---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; sw.Reset(); sw.Start(); Scene = sceneRoot.GetComponent <Scene>(); if (null != Scene) { Scene.InitPortal(); } else { Logger.Error("cant find Scene in ScenePerfab!!!"); } sw.Stop(); log += "\nInitPortal---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; } sw.Reset(); sw.Start(); if (Scene.StaticChildren) { StaticBatchingUtility.Combine(Scene.StaticChildren); } else { StaticBatchingUtility.Combine(sceneRoot); } sw.Stop(); log += "\nStaticBatchingUtility---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; sw.Reset(); sw.Start(); SoundManager.Instance.SetAreaSoundEnable(SoundManager.Instance.EnableBGM); // var sceneCacheKey = string.Format("{0}.unity3d", ScenePrefab); // ResourceManager.Instance.RemoveFromCache(sceneCacheKey); sw.Stop(); log += "\nSetAreaSoundEnable---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; } catch (Exception e) { Logger.Error("step 0------------------{0}\n{1}", e.Message, e.StackTrace); } } if (ObjManager.Instance == null) { Logger.Log2Bugly("EnterGameCoroutine ObjManager.Instance = null "); yield break; } //清除ObjManager ObjManager.Instance.Reset(); if (PlayerDataManager.Instance == null || PlayerDataManager.Instance.mInitBaseAttr == null) { Logger.Log2Bugly("EnterGameCoroutine PlayerDataManager.Instance = null "); yield break; } sw.Reset(); sw.Start(); var data = PlayerDataManager.Instance.mInitBaseAttr; var attr = new InitMyPlayerData(); //初始化造主角的数据 try { attr.ObjId = data.CharacterId; attr.DataId = data.RoleId; attr.Name = data.Name; attr.Camp = data.Camp; attr.IsDead = data.IsDead == 1; attr.HpMax = data.HpMax; attr.HpNow = data.HpNow; attr.MpMax = data.MpMax; attr.MpNow = data.MpMow; attr.X = data.X; attr.Y = GetTerrainHeight(data.X, data.Y); attr.Z = data.Y; attr.MoveSpeed = data.MoveSpeed; attr.AreaState = (eAreaState)data.AreaState; attr.EquipModel = data.EquipsModel; attr.ModelId = data.ModelId; attr.MountId = data.MountId; { var __list1 = data.Buff; var __listCount1 = __list1.Count; for (var __i1 = 0; __i1 < __listCount1; ++__i1) { var buff = __list1[__i1]; { attr.Buff.Add(buff.BuffId, buff.BuffTypeId); } } } } catch (Exception e) { Logger.Error("step 1------------------{0}\n{1}", e.Message, e.StackTrace); } //造主角 ObjMyPlayer player = null; try { player = ObjManager.Instance.CreateMainPlayer(attr); if (player == null) { Logger.Log2Bugly("EnterGameCoroutine player = null "); yield break; } if (PlayFirstEnterGameCG == 1 && GameSetting.Instance.ReviewState == 1) { var reivewRecord = GameSetting.Instance.GetReviewRecord(); if (reivewRecord != null) { var transPos = new Vector3(); transPos.x = reivewRecord.posX; transPos.y = GetTerrainHeight(reivewRecord.posX, reivewRecord.posY); transPos.z = reivewRecord.posY; player.transform.position = transPos; GameUtils.FlyTo(3, reivewRecord.posX, reivewRecord.posY); } } player.AdjustHeightPosition(); } catch (Exception e) { Logger.Error("step 2------------------{0}\n{1}", e.Message, e.StackTrace); } sw.Stop(); log += "\nObjMyPlayer---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; sw.Reset(); sw.Start(); //设置buff try { EventDispatcher.Instance.DispatchEvent(new UIEvent_ClearBuffList()); var count = data.Buff.Count; for (var i = 0; i < count; i++) { var buffResult = data.Buff[i]; EventDispatcher.Instance.DispatchEvent(new UIEvent_SyncBuffCell(buffResult)); } } catch (Exception e) { Logger.Error("step 3------------------{0}\n{1}", e.Message, e.StackTrace); } sw.Stop(); log += "\nUIEvent_ClearBuffList---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; sw.Reset(); sw.Start(); //预加载技能资源 try { ObjManager.Instance.PrepareMainPlayerSkillResources(); } catch (Exception e) { Logger.Error("step 4------------------{0}\n{1}", e.Message, e.StackTrace); } sw.Stop(); log += "\nPrepareMainPlayerSkillResources---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; sw.Reset(); sw.Start(); //给主摄像机设置跟随,设置声音 try { if (MainCamera == null) { Logger.Log2Bugly("EnterGameCoroutine MainCamera = null "); yield break; } MainCamera.GetComponent <CameraController>().FollowObj = player.gameObject; { //audio listener var audioListerner = MainCamera.gameObject.GetComponent <AudioListener>(); if (null != audioListerner) { DestroyObject(audioListerner); } var playerAudio = player.gameObject.GetComponent <AudioListener>(); if (null == playerAudio) { player.gameObject.AddComponent <AudioListener>(); } } } catch (Exception e) { Logger.Error("step 5------------------{0}\n{1}", e.Message, e.StackTrace); } sw.Stop(); log += "\nCameraController---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; sw.Reset(); sw.Start(); //初始化UI Coroutine co = null; try { co = StartCoroutine(InitUI()); } catch (Exception e) { Logger.Error("step 6------------------{0}\n{1}", e.Message, e.StackTrace); } if (null != co) { yield return(co); } sw.Stop(); log += "\nInitUI---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; EventDispatcher.Instance.DispatchEvent(new LoadingPercentEvent(0.7f)); sw.Reset(); sw.Start(); //UI // try { EventDispatcher.Instance.DispatchEvent(new Enter_Scene_Event(Scene.SceneTypeId)); EventDispatcher.Instance.DispatchEvent(new RefresSceneMap(Scene.SceneTypeId)); player.CreateNameBoard(); if (PlayerDataManager.Instance != null) { //根据场景不一样,自动战斗的优先级也不一样 PlayerDataManager.Instance.RefrehEquipPriority(); } } // catch (Exception e) // { // Logger.Error("step 7------------------{0}\n{1}", e.Message, e.StackTrace); // } sw.Stop(); log += "\nUI---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; sw.Reset(); sw.Start(); //向服务器发送切换场景结束的包 if (SceneManager.Instance != null) { //yield return StartCoroutine(SceneManager.Instance.ChangeSceneOverCoroutine()); StartCoroutine(SceneManager.Instance.ChangeSceneOverCoroutine()); } else { Logger.Log2Bugly("EnterGameCoroutine SceneManager.Instance = null "); } sw.Stop(); log += "\nChangeSceneOverCoroutine---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; sw.Reset(); sw.Start(); //客户端切换场景结束事件 try { SceneEffect.OnEnterScecne(); var formersceneid = Scene.SceneTypeId; SceneManager.Instance.OnLoadSceneOver(formersceneid); EventDispatcher.Instance.DispatchEvent(new LoadSceneOverEvent()); } catch (Exception e) { Logger.Error("step 9------------------{0}\n{1}", e.Message, e.StackTrace); } sw.Stop(); log += "\nOnLoadSceneOver---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; //如果还没有Cache或者UIManger的Cache里没配这个UI,那就在这Cache if (!HasCache) { sw.Reset(); sw.Start(); var args = new UIInitArguments { Args = new List <int> { -1 } }; yield return(StartCoroutine(UIManager.Instance.ShowUICoroutine(UIConfig.EraBookUI, args))); yield return(new WaitForEndOfFrame()); EventDispatcher.Instance.DispatchEvent(new Close_UI_Event(UIConfig.EraBookUI)); //解决关闭灭世地图会打开书的bug for (int i = 0; i < UIManager.Instance.mRecordStack.Count; i++) { if (UIManager.Instance.mRecordStack[i].Config == UIConfig.EraBookUI) { UIManager.Instance.mRecordStack.Remove(UIManager.Instance.mRecordStack[i]); break; } } sw.Stop(); log += "\nEraBookUI---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; HasCache = true; } yield return(new WaitForEndOfFrame()); EventDispatcher.Instance.DispatchEvent(new LoadingPercentEvent(0.9f)); sw.Reset(); sw.Start(); //播放CG try { Action brightnessStartWork = () => { LoginLogic.State = LoginLogic.LoginState.InGaming; var bc = Game.Instance.GetComponent <BrightnessController>(); if (bc) { bc.ResetTimer(); } }; if (1 == PlayFirstEnterGameCG) { #if UNITY_EDITOR var skip = true; #else bool skip = true; #endif if (0 == PlayerDataManager.Instance.GetRoleId() || 1 == PlayerDataManager.Instance.GetRoleId() || 2 == PlayerDataManager.Instance.GetRoleId()) { if (int.Parse(Table.GetClientConfig(1205).Value) == 1) { PlayCG.Instance.PlayCGFile("Video/HeroBorn.txt", brightnessStartWork, skip); PlatformHelper.UMEvent("PlayCG", "play", "Video/HeroBorn.txt"); } } EventDispatcher.Instance.DispatchEvent(new FirstEnterGameEvent(true)); PlayFirstEnterGameCG = 0; } else { brightnessStartWork(); } } catch (Exception e) { Logger.Error("step 10------------------{0}\n{1}", e.Message, e.StackTrace); } try { if (null != GameLogic.Instance) { GameLogic.Instance.AttachControl(); } } catch (Exception e) { Logger.Error("step 8------------------{0}\n{1}", e.Message, e.StackTrace); } mControl.OnLoadSceneOver(); LoadOver = true; //优化loading读条速度,所以meshtree放在读条之后再加载 if (null != Scene) { StartCoroutine(DelayLoadTerrainMeshTree()); } StartCoroutine(DelayShowLoginRewardUI()); if (!HasAdjustSetting) { HasAdjustSetting = true; // StartCoroutine(ChangeSetting()); 暂时屏蔽掉了自动降低配置功能 } //LuaEventManager.Instance.PushEvent("OnEnterGameOver", Scene.SceneTypeId); sw.Stop(); log += "\nOVER---------" + sw.ElapsedMilliseconds + "------------" + swTotal.ElapsedMilliseconds; UnityEngine.Debug.Log(log); }