No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoadSwapDLC.cs 2.8KB

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