ShaderBundleCollector.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. namespace XGame.Editor.Build.AssetBundles
  4. {
  5. public class ShaderBundleCollector
  6. {
  7. private BundleContext _context;
  8. public ShaderBundleCollector(BundleContext context)
  9. {
  10. _context = context;
  11. }
  12. public void Collect()
  13. {
  14. if (_context.isMergeShader == false)
  15. {
  16. return;
  17. }
  18. var assetsRoots = _context.assetsRoots;
  19. for (var index = 0; index < assetsRoots.Length; index++)
  20. {
  21. //按照资源根目录("Assets/Presource/"优先)来分ShaderVariant和shader的ab包
  22. var guids = AssetDatabase.FindAssets("t:ShaderVariantCollection", new string[] { assetsRoots[index] });
  23. if (guids == null || guids.Length == 0)
  24. continue;
  25. var assetPaths = new List<string>();
  26. foreach (var guid in guids)
  27. {
  28. var variantPath = AssetDatabase.GUIDToAssetPath(guid);
  29. if (_context.bundleAssetPaths.Contains(variantPath))
  30. {
  31. continue;
  32. }
  33. assetPaths.Add(variantPath);
  34. }
  35. string[] variantPaths = assetPaths.ToArray();
  36. var dependencies = AssetDatabase.GetDependencies(variantPaths);
  37. System.Array.Sort(dependencies, (a, b) => string.Compare(a, b, System.StringComparison.OrdinalIgnoreCase));
  38. foreach (var dep in dependencies)
  39. {
  40. if (_context.bundleAssetPaths.Contains(dep) ||
  41. dep.EndsWith(_context.ShaderExt, System.StringComparison.OrdinalIgnoreCase) == false)
  42. {
  43. continue;
  44. }
  45. assetPaths.Add(dep);
  46. }
  47. _context.AddAddressableBundle(assetPaths.ToArray(), guids[0]);
  48. }
  49. }
  50. }
  51. }