ShaderCollectionAsync.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using UnityEngine;
  2. using XGame.Framework.Asset;
  3. using XGame.Framework.Asyncs;
  4. namespace XGame.Framework.Shaders
  5. {
  6. /// <summary>
  7. /// shader变体预热
  8. /// ShaderVariantCollection
  9. /// </summary>
  10. internal class ShaderCollectionAsync : Async
  11. {
  12. private IAssetLoadAsync<ShaderVariantCollection> _async;
  13. public ShaderCollectionAsync(string addressableName)
  14. {
  15. if (string.IsNullOrEmpty(addressableName))
  16. {
  17. Log.Error("ShaderCollectionAsync Name is null.");
  18. Completed();
  19. return;
  20. }
  21. Log.Info($"ShaderCollectionAsync start. Name:{addressableName}");
  22. _async = AssetManager.LoadAsync<ShaderVariantCollection>(addressableName);
  23. _async.On(OnLoaded);
  24. }
  25. private void OnLoaded(IAsync async)
  26. {
  27. _async = null;
  28. var result = async as IAssetLoadAsync<ShaderVariantCollection>;
  29. var collection = result?.Result;
  30. Log.Info($"ShaderCollectionAsync result:{collection != null} isWarmedUp:{collection?.isWarmedUp ?? false}");
  31. collection?.WarmUp();
  32. Completed();
  33. if (collection)
  34. {
  35. AssetManager.Recycle(collection);
  36. }
  37. Log.Info("ShaderCollectionAsync completed.");
  38. }
  39. protected override void OnRemoveAll()
  40. {
  41. _async?.RemoveOn(OnLoaded);
  42. _async = null;
  43. }
  44. }
  45. }