1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| using GameFramework.DataTable; using GameFramework.Event; using UnityEngine; using UnityGameFramework.Runtime; using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
namespace YouProject { public class ProcedureChangeScene : ProcedureBase { private const int MenuSceneId = 1; private bool m_IsSceneLoadComplete = false; private int m_BackgroundMusicId = 0; private int sceneId = 0; protected override void OnEnter(ProcedureOwner procedureOwner) { base.OnEnter(procedureOwner);
m_IsSceneLoadComplete = false; GameEntry.Event.Subscribe(LoadSceneSuccessEventArgs.EventId, OnLoadSceneSuccess); GameEntry.Event.Subscribe(LoadSceneFailureEventArgs.EventId, OnLoadSceneFailure); GameEntry.Event.Subscribe(LoadSceneUpdateEventArgs.EventId, OnLoadSceneUpdate);
GameEntry.Sound.StopAllLoadingSounds(); GameEntry.Sound.StopAllLoadedSounds();
GameEntry.Entity.HideAllLoadingEntities(); GameEntry.Entity.HideAllLoadedEntities();
string[] loadedSceneAssetNames = GameEntry.Scene.GetLoadedSceneAssetNames(); for (int i = 0; i < loadedSceneAssetNames.Length; i++) { Debug.Log("loadedSceneAssetNames: " + loadedSceneAssetNames[i]); GameEntry.Scene.UnloadScene(loadedSceneAssetNames[i]); }
GameEntry.Base.ResetNormalGameSpeed(); sceneId = procedureOwner.GetData<VarInt>("NextSceneId"); IDataTable<DRScene> dtScene = GameEntry.DataTable.GetDataTable<DRScene>(); DRScene drScene = dtScene.GetDataRow(sceneId); if (drScene == null) { Log.Warning("Can not load scene '{0}' from data table.", sceneId.ToString()); return; } GameEntry.Scene.LoadScene(AssetUtility.GetSceneAsset(drScene.AssetName), Constant.AssetPriority.SceneAsset, this); m_BackgroundMusicId = drScene.BackgroundMusicId; } protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown) { GameEntry.Event.Unsubscribe(LoadSceneSuccessEventArgs.EventId, OnLoadSceneSuccess); GameEntry.Event.Unsubscribe(LoadSceneFailureEventArgs.EventId, OnLoadSceneFailure); GameEntry.Event.Unsubscribe(LoadSceneUpdateEventArgs.EventId, OnLoadSceneUpdate); GameEntry.Event.Unsubscribe(LoadSceneDependencyAssetEventArgs.EventId, OnLoadSceneDependencyAsset);
base.OnLeave(procedureOwner, isShutdown); } protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds) { base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds); if (!m_IsSceneLoadComplete) return;
switch (sceneId) { case 1: ChangeState<ProcedureLogin>(procedureOwner); break; case 2: ChangeState<ProcedureMain>(procedureOwner); break; case 3: ChangeState<ProcedureBattle>(procedureOwner); break; default: break; } } private void OnLoadSceneSuccess(object sender, GameEventArgs e) { LoadSceneSuccessEventArgs ne = (LoadSceneSuccessEventArgs)e; if (ne.UserData != this) return;
Log.Info("Load scene '{0}' OK.", ne.SceneAssetName); if (m_BackgroundMusicId > 0) GameEntry.Sound.PlayMusic(m_BackgroundMusicId); m_IsChangeSceneComplete = true; } private void OnLoadSceneFailure(object sender, GameEventArgs e) { LoadSceneFailureEventArgs ne = (LoadSceneFailureEventArgs)e; if (ne.UserData != this) return; Log.Error("Load scene '{0}' failure, error message '{1}'.", ne.SceneAssetName, ne.ErrorMessage); } private void OnLoadSceneUpdate(object sender, GameEventArgs e) { LoadSceneUpdateEventArgs ne = (LoadSceneUpdateEventArgs)e; if (ne.UserData != this) return; Log.Info("Load scene '{0}' update, progress '{1}'.", ne.SceneAssetName, ne.Progress.ToString("P2")); } private void OnLoadSceneDependencyAsset(object sender, GameEventArgs e) { LoadSceneDependencyAssetEventArgs ne = (LoadSceneDependencyAssetEventArgs)e; if (ne.UserData != this) return; Log.Info("Load scene '{0}' dependency asset '{1}', count '{2}/{3}'.", ne.SceneAssetName, ne.DependencyAssetName, ne.LoadedCount.ToString(), ne.TotalCount.ToString()); } } }
|