MapAssetModule.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using UnityEngine;
  3. using XGame.Framework.Asset;
  4. namespace XGame.Framework.Map
  5. {
  6. public class MapAssetModule : IMapAssetModule, IDisposable
  7. {
  8. private IAssetModule _assetModule;
  9. private MapContext _context;
  10. private MapAssetPool _pool;
  11. private MapAssetPool Pool => _pool ?? (_pool = new MapAssetPool());
  12. public MapAssetModule(MapContext mapContext)
  13. {
  14. _assetModule = new AssetModule();
  15. _context = mapContext;
  16. }
  17. IAssetLoadAsync<TResult> IMapAssetModule.LoadAsync<TResult>(string addressableName)
  18. {
  19. return _assetModule.LoadAsync<TResult>(addressableName);
  20. }
  21. IEntityViewLoadAsync<TEntityView> IMapAssetModule.LoadEntity<TEntityView>(string addressableName, Transform parent)
  22. {
  23. var loadAsync = _assetModule.LoadAsync<EntityBehaviour>(addressableName);
  24. var viewAsync = new EntityViewLoadAsync<TEntityView>(loadAsync, parent, this);
  25. return viewAsync;
  26. }
  27. public TEntityView CreateEntity<TEntityView>(EntityBehaviour behaviour) where TEntityView : class, IEntityView, new()
  28. {
  29. var entityView = Pool.Acquire<TEntityView>();
  30. if (entityView is IMapContextSetter contextSetter)
  31. {
  32. contextSetter.Context = _context;
  33. }
  34. if (entityView is IEntityProxy proxy)
  35. {
  36. proxy.Behaviour = behaviour;
  37. }
  38. return entityView;
  39. }
  40. void IMapAssetModule.Recycle(UnityEngine.Object obj)
  41. {
  42. _assetModule.Recycle(obj);
  43. }
  44. void IMapAssetModule.Recycle(IEntityView entityView, bool isDestroy)
  45. {
  46. if (entityView?.Active ?? false)
  47. { // ÏÈÒþ²Ø
  48. entityView.Disable();
  49. }
  50. if (entityView is IEntityProxy proxy)
  51. {
  52. var behaviour = proxy.Behaviour;
  53. _assetModule.Recycle(behaviour.gameObject, isDestroy);
  54. }
  55. if (isDestroy)
  56. {
  57. (entityView as IDisposable)?.Dispose();
  58. }
  59. else
  60. {
  61. Pool.Recycle(entityView);
  62. }
  63. }
  64. void IMapAssetModule.StopAllAsyncs()
  65. {
  66. _assetModule.StopAllAsyncs();
  67. }
  68. void IDisposable.Dispose()
  69. {
  70. _pool?.Clear();
  71. _pool = null;
  72. (_assetModule as IDisposable)?.Dispose();
  73. _assetModule = null;
  74. }
  75. }
  76. }