LoadSwapDLC.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.IO;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Experimental.U2D.Animation;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. namespace Unity.U2D.Animation.Sample
  9. {
  10. public class LoadSwapDLC : MonoBehaviour
  11. {
  12. const string k_AssetBundleName = "2DAnimationSampleAssetBundles";
  13. public SwapFullSkin[] swapFullSkin;
  14. public void LoadAssetBundle()
  15. {
  16. var assetBundlePath = Path.Combine(Application.streamingAssetsPath, k_AssetBundleName);
  17. var bundle = AssetBundle.LoadFromFile(Path.Combine(assetBundlePath, k_AssetBundleName));
  18. if (bundle == null)
  19. {
  20. Debug.LogWarning("AssetBundle not found");
  21. return;
  22. }
  23. var manifest = bundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
  24. if (manifest == null)
  25. {
  26. Debug.LogWarning("Unable to load manifest");
  27. return;
  28. }
  29. foreach (var assetBundleName in manifest.GetAllAssetBundles())
  30. {
  31. var subBundle = AssetBundle.LoadFromFile(Path.Combine(assetBundlePath, assetBundleName));
  32. var assets = subBundle.LoadAllAssets();
  33. foreach (var asset in assets)
  34. {
  35. if (asset is SpriteLibraryAsset)
  36. {
  37. var sla = (SpriteLibraryAsset)asset;
  38. foreach (var sfs in swapFullSkin)
  39. {
  40. var list = sfs.spriteLibraries.ToList();
  41. list.Add(sla);
  42. sfs.spriteLibraries = list.ToArray();
  43. }
  44. }
  45. }
  46. }
  47. foreach (var sfs in swapFullSkin)
  48. {
  49. sfs.UpdateSelectionChoice();
  50. }
  51. }
  52. #if UNITY_EDITOR
  53. [ContextMenu("Build Asset Bundles")]
  54. void BuildBundles()
  55. {
  56. BuildAssetBundles();
  57. }
  58. public static void BuildAssetBundles()
  59. {
  60. string assetBundleDirectory = Path.Combine(Application.streamingAssetsPath, "2DAnimationSampleAssetBundles");
  61. if (!Directory.Exists(assetBundleDirectory))
  62. {
  63. Directory.CreateDirectory(assetBundleDirectory);
  64. }
  65. BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);
  66. }
  67. #endif
  68. }
  69. }