Browse Source

feat:地图模块资源加载

chenbin 6 months ago
parent
commit
5cf9c015bc

+ 1 - 1
Assets/Scripts/Business/Editor/Business.Editor.asmdef

@@ -1,6 +1,6 @@
 {
     "name": "Business.Editor",
-    "rootNamespace": "",
+    "rootNamespace": "FL.Editor",
     "references": [
         "GUID:a931f884323eea34397b583771fc6907",
         "GUID:a8ec4955e032e254bbaa034aa00e0232",

+ 1 - 1
Assets/Scripts/Business/Framework/Business.Framework.asmdef

@@ -1,6 +1,6 @@
 {
     "name": "Business.Framework",
-    "rootNamespace": "",
+    "rootNamespace": "FL",
     "references": [
         "GUID:a8ec4955e032e254bbaa034aa00e0232",
         "GUID:8c8f9d96103e94a7da84b012fd7e9f13"

+ 5 - 1
Assets/Scripts/Business/Framework/FGUI/UIModuleFGUI.cs

@@ -202,11 +202,15 @@ namespace XGame.Framework.FGUI
                 DestroyUIView(uiview);
             }
             _openedMap.Clear();
-
+            #region 清理FGUI緩存
+            StageEngine.beingQuit = true;
+            UIPackage.RemoveAllPackages();
+            Stage.inst.Dispose();
             NTexture.CustomDestroyMethod -= RecycleTexture;
             NAudioClip.CustomDestroyMethod -= RecycleAudio;
             UIObjectFactory.Clear();
             ClearGloaderAssets();
+            #endregion
             (_uitree as IDisposable)?.Dispose();
             (_assetModule as IDisposable)?.Dispose();
             _assetModule = null;

+ 1 - 1
Assets/Scripts/Business/Runtime/Business.Runtime.asmdef

@@ -1,6 +1,6 @@
 {
     "name": "Business.Runtime",
-    "rootNamespace": "",
+    "rootNamespace": "FL",
     "references": [
         "GUID:a931f884323eea34397b583771fc6907",
         "GUID:a8ec4955e032e254bbaa034aa00e0232",

+ 1 - 1
Assets/Scripts/Framework/Runtime/Framework.Runtime.asmdef

@@ -1,6 +1,6 @@
 {
     "name": "Framework.Runtime",
-    "rootNamespace": "",
+    "rootNamespace": "XGame.Framework",
     "references": [
         "GUID:a931f884323eea34397b583771fc6907"
     ],

+ 4 - 1
Assets/Scripts/Framework/Runtime/Map/Api/IEntityView.cs

@@ -14,6 +14,9 @@
         /// 隐藏
         /// </summary>
         void Disable();
-
+    }
+    public interface IEntityProxy
+    {
+        EntityBehaviour Behaviour { get; set; }
     }
 }

+ 14 - 1
Assets/Scripts/Framework/Runtime/Map/Api/IMapAssetModule.cs

@@ -1,3 +1,5 @@
+using System;
+using UnityEngine;
 using XGame.Framework.Asset;
 
 namespace XGame.Framework.Map
@@ -12,13 +14,24 @@ namespace XGame.Framework.Map
         /// <param name="addressableName">可寻址资源名字</param>
         /// <returns>返回异步回调</returns>
         IAssetLoadAsync<TResult> LoadAsync<TResult>(string addressableName) where TResult : UnityEngine.Object;
+        void LoadEntity<TEntityView>(string addressableName, Transform parent, Action<TEntityView> callback) where TEntityView : class, IEntityView, new();
+        TEntityView CreateEntity<TEntityView>(EntityBehaviour behaviour) where TEntityView : class, IEntityView, new();
         /// <summary>
         /// 回收资源
         /// </summary>
         /// <param name="obj"></param>
         void Recycle(UnityEngine.Object obj);
+        /// <summary>
+        /// 回收Entity实例
+        /// 和LoadEntity配套
+        /// </summary>
+        /// <param name="entityView"></param>
+        void Recycle(IEntityView entityView);
+        /// <summary>
+        /// 结束所有加载的异步
+        /// </summary>
+        void StopAllAsyncs();
 
-        
     }
 }
 

+ 1 - 1
Assets/Scripts/Framework/Runtime/Map/Entity/EntityController.cs

@@ -6,7 +6,7 @@
         public MapContext Context => _context;
         MapContext IMapContextSetter.Context { set => _context = value; }
         private IEntityViewModel _viewModel;
-        public TViewModel VM => _viewModel as TViewModel;
+        protected TViewModel VM => _viewModel as TViewModel;
         IEntityViewModel IEntityViewModelSetter.ViewModel { set => _viewModel = value; }
 
         public abstract void OnEnable(object intent);

+ 4 - 0
Assets/Scripts/Framework/Runtime/Map/Entity/EntityControllerGroup.cs

@@ -24,6 +24,10 @@ namespace XGame.Framework.Map
         public void AddController(IEntityController controller)
         {
             _controllers.Add(controller);
+            if (controller is IMapContextSetter contextSetter)
+            {
+                contextSetter.Context = _context;
+            }
             if (controller is IEntityViewModelSetter vmSetter)
             {
                 vmSetter.ViewModel = _viewModel;

+ 46 - 27
Assets/Scripts/Framework/Runtime/Map/Entity/EntityView.cs

@@ -5,69 +5,88 @@ namespace XGame.Framework.Map
 {
     /// <summary>
     /// Entity实例
-    /// 只有一个Controller
+    /// 没有Controller
     /// </summary>
-    /// <typeparam name="TCtrl"></typeparam>
-    public abstract class EntityView<TCtrl> : IEntityView, IDisposable where TCtrl : class, IEntityController
+    /// <typeparam name="TViewModel"></typeparam>
+    public abstract class EntityView<TViewModel> : IEntityView, IMapContextSetter, IEntityProxy, IDisposable where TViewModel : class, IEntityViewModel
     {
-        public MapContext Context { get; private set; }
+        private MapContext _context;
+        protected MapContext Context => _context;
 
         private EntityBehaviour _behaviour;
         private IEntityViewModel _viewModel;
-        /// <summary>
-        /// Entity的controller只有一个
-        /// </summary>
-        private IEntityController _controller;
-        public TCtrl Controller => _controller as TCtrl;
+        protected TViewModel VM => _viewModel as TViewModel;
 
-        public void Init(MapContext context, EntityBehaviour behaviour)
+        private void Init()
         {
-            Context = context;
-            _behaviour = behaviour;
-            _viewModel = CtreatViewModel(behaviour.Collector);
-            _controller = CreateController();
-            if (_controller is IEntityViewModelSetter vmSetter)
-            {
-                vmSetter.ViewModel = _viewModel;
-            }
+            _viewModel = Activator.CreateInstance<TViewModel>();
+            (_viewModel as EntityViewModel)?.Init(_behaviour);
+            //_controller = Activator.CreateInstance<TCtrl>();
+            //if (_controller is IMapContextSetter contextSetter)
+            //{
+            //    contextSetter.Context = context;
+            //}
+            //if (_controller is IEntityViewModelSetter vmSetter)
+            //{
+            //    vmSetter.ViewModel = _viewModel;
+            //}
             OnInited();
         }
 
         #region 接口实现
+
+        MapContext IMapContextSetter.Context { set => _context = value; }
+        EntityBehaviour IEntityProxy.Behaviour 
+        { 
+            get => _behaviour;
+            set 
+            {
+                _behaviour = value; 
+                Init();
+            }
+        }
         public bool Active { get; private set; }
+        
 
         public void Enable(object intent)
         {
             if (Active) return;
             Active = true;
             _behaviour.gameObject.SetActive(true);
-            _controller.OnEnable(intent);
+            OnEnable(intent);
         }
 
+
         public void Disable()
         {
             if (!Active) return;
             Active = false;
             _behaviour.gameObject.SetActive(false);
-            _controller.OnDisable();
+            OnDisable();
         }
 
+
         void IDisposable.Dispose()
         {
             OnDispose();
-            _behaviour = null;
-            (_controller as IDisposable)?.Dispose();
-            _controller = null;
-            Context = null;
+            (_viewModel as IDisposable)?.Dispose();
             _viewModel = null;
+            _behaviour = null;
+            _context = null;
         }
         #endregion
 
         protected abstract IEntityViewModel CtreatViewModel(IObjectCollector collector);
-        protected abstract IEntityController CreateController();
 
-        protected abstract void OnInited();
-        protected abstract void OnDispose();
+        protected abstract void OnEnable(object intent);
+
+        protected abstract void OnDisable();
+        protected virtual void OnInited()
+        {
 
+        }
+        protected virtual void OnDispose()
+        {
+        }
     }
 }

+ 27 - 0
Assets/Scripts/Framework/Runtime/Map/Entity/EntityViewModel.cs

@@ -0,0 +1,27 @@
+using System;
+using UnityEngine;
+using XGame.Framework.ObjectCollection;
+
+namespace XGame.Framework.Map
+{
+    public abstract class EntityViewModel : IEntityViewModel, IDisposable
+    {
+        private EntityBehaviour _behaviour;
+        protected Transform Tr => _behaviour.transform;
+        public void Init(EntityBehaviour behaviour)
+        {
+            _behaviour = behaviour;
+            OnInit(behaviour.Collector);
+        }
+
+        void IDisposable.Dispose()
+        {
+            OnDispose();
+            _behaviour = null;
+        }
+        protected abstract void OnInit(IObjectCollector collector);
+        protected virtual void OnDispose()
+        {
+        }
+    }
+}

+ 11 - 0
Assets/Scripts/Framework/Runtime/Map/Entity/EntityViewModel.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: Bnwc4CyoBi4XPSkYr8t6zl5HR8xNgXRGcdfOWWMSkKFugMi1HxuiDUs=
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 76 - 0
Assets/Scripts/Framework/Runtime/Map/Impl/MapAssetModule.cs

@@ -0,0 +1,76 @@
+using System;
+using UnityEngine;
+using XGame.Framework.Asset;
+
+namespace XGame.Framework.Map
+{
+    public class MapAssetModule : IMapAssetModule, IDisposable
+    {
+        private IAssetModule _assetModule;
+        private MapContext _context;
+        public MapAssetModule(MapContext mapContext)
+        {
+            _assetModule = new AssetModule();
+            _context = mapContext;
+        }
+        IAssetLoadAsync<TResult> IMapAssetModule.LoadAsync<TResult>(string addressableName)
+        {
+            return _assetModule.LoadAsync<TResult>(addressableName);
+        }
+        void IMapAssetModule.LoadEntity<TEntityView>(string addressableName, Transform parent, Action<TEntityView> callback)
+        {
+            var loadAsync = _assetModule.LoadAsync<EntityBehaviour>(addressableName);
+            loadAsync.On(_ =>
+            {
+                var behaviour = loadAsync.GetResult();
+                if (behaviour == null)
+                {
+                    callback?.Invoke(null);
+                    return;
+                }
+                var entityView = CreateEntity<TEntityView>(behaviour);
+                if (parent != null)
+                {
+                    behaviour.transform.SetParent(parent, false);
+                }
+                callback?.Invoke(entityView);
+            });
+        }
+        public TEntityView CreateEntity<TEntityView>(EntityBehaviour behaviour) where TEntityView : class, IEntityView, new()
+        {
+            var entityView = Activator.CreateInstance<TEntityView>();
+            if (entityView is IMapContextSetter contextSetter)
+            {
+                contextSetter.Context = _context;
+            }
+            if (entityView is IEntityProxy proxy)
+            {
+                proxy.Behaviour = behaviour;
+            }
+            return entityView;
+        }
+        void IMapAssetModule.Recycle(UnityEngine.Object obj)
+        {
+            _assetModule.Recycle(obj);
+        }
+        void IMapAssetModule.Recycle(IEntityView entityView)
+        {
+            if (entityView is IEntityProxy proxy)
+            {
+                var behaviour = proxy.Behaviour;
+                _assetModule.Recycle(behaviour);
+            }
+            (entityView as IDisposable)?.Dispose();
+        }
+        void IMapAssetModule.StopAllAsyncs()
+        {
+            _assetModule.StopAllAsyncs();
+        }
+
+        void IDisposable.Dispose()
+        {
+            (_assetModule as IDisposable)?.Dispose();
+            _assetModule = null;
+        }
+    }
+}

+ 11 - 0
Assets/Scripts/Framework/Runtime/Map/Impl/MapAssetModule.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: CHlMsnmlUCp5qZWb/1ak2OD9aHPPbEQkjk55+4XxLhQYGkksv9rCSgw=
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 8 - 0
Assets/Scripts/Framework/Runtime/Map/MapContext.cs

@@ -11,6 +11,12 @@ namespace XGame.Framework.Map
 
         public IMapModule Map { get; private set; }
 
+        /// <summary>
+        /// 资源加载模块,需要时才创建
+        /// </summary>
+        private IMapAssetModule _assetModule;
+        public IMapAssetModule Asset => _assetModule ?? (_assetModule = new MapAssetModule(this));
+
         public MapContext(IQualityModule quality, ITimeModule time, IMapModule map)
         {
             Quality = quality;
@@ -28,6 +34,8 @@ namespace XGame.Framework.Map
             Quality = null;
             Time = null;
             Map = null;
+            (_assetModule as IDisposable)?.Dispose();
+            _assetModule = null;
         }
     }