EntityViewLoadAsync.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.GetResult();
  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. Clear();
  42. }
  43. private void Clear()
  44. {
  45. _assetLoadAsync = null;
  46. _parent = null;
  47. _handle = null;
  48. }
  49. }
  50. }