EntityViewLoadAsync.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using XGame.Framework.Asset;
  3. using XGame.Framework.Asyncs;
  4. namespace XGame.Framework.Map
  5. {
  6. internal class EntityViewLoadAsync<TEntityView> : Async, IEntityViewLoadAsync<TEntityView> where TEntityView : class, IEntityView, new()
  7. {
  8. public TEntityView Result { get; private set; }
  9. public string AddressableName { get; private set; }
  10. private IAssetLoadAsync<EntityBehaviour> _assetLoadAsync;
  11. private Transform _parent;
  12. private IEntityViewHandle _handle;
  13. public EntityViewLoadAsync(IAssetLoadAsync<EntityBehaviour> assetLoadAsync, Transform parent, IEntityViewHandle handle)
  14. {
  15. AddressableName = assetLoadAsync.AddressableName;
  16. _assetLoadAsync = assetLoadAsync;
  17. _parent = parent;
  18. _handle = handle;
  19. assetLoadAsync.On(OnLoaded);
  20. _handle = handle;
  21. }
  22. private void OnLoaded(IAsync _)
  23. {
  24. var behaviour = _assetLoadAsync.Result;
  25. if (behaviour != null)
  26. {
  27. var entityView = _handle.CreateEntity<TEntityView>(behaviour);
  28. if (_parent != null)
  29. {
  30. behaviour.transform.SetParent(_parent, false);
  31. }
  32. entityView.Enable();
  33. Result = entityView;
  34. }
  35. Completed();
  36. //清理
  37. Clear();
  38. }
  39. protected override void OnRemoveAll()
  40. {
  41. _assetLoadAsync?.RemoveAll();
  42. Clear();
  43. }
  44. private void Clear()
  45. {
  46. _assetLoadAsync = null;
  47. _parent = null;
  48. _handle = null;
  49. }
  50. }
  51. }