AssetModeNavigation.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityToolbarExtender;
  4. using XGame.Framework.Asset;
  5. namespace XGame.Editor.Asset
  6. {
  7. [InitializeOnLoad]
  8. class AssetModeNavigation : INavigation
  9. {
  10. static readonly string[,] AssetModeContent =
  11. { { "AssetBundle", "当前模式:AssetBundle\n运行时不可切换" }, { "AssetDatabase", "当前模式:AssetDatabase\n运行时不可切换" } };
  12. bool _assetMode;
  13. public int Order => 200;
  14. public void OnGUI()
  15. {
  16. GUILayout.Space(50);
  17. _assetMode = EditorPrefs.GetBool(Define.SIMULATE_ASSETBUNDLE_EDITOR_KEY, true);
  18. EditorGUI.BeginDisabledGroup(Application.isPlaying);
  19. if (GUILayout.Button(new GUIContent(AssetModeContent[_assetMode ? 1 : 0, 0], AssetModeContent[_assetMode ? 1 : 0, 1]), ToolbarStyles.assetModeButtonStyle))
  20. {
  21. _assetMode = !_assetMode;
  22. Debug.Log($"切换资源模式成功,当前模式:{AssetModeContent[_assetMode ? 1 : 0, 0]}");
  23. EditorSettings.spritePackerMode = _assetMode ? UnityEditor.SpritePackerMode.AlwaysOnAtlas : UnityEditor.SpritePackerMode.BuildTimeOnlyAtlas;
  24. EditorPrefs.SetBool(Define.SIMULATE_ASSETBUNDLE_EDITOR_KEY, _assetMode);
  25. }
  26. EditorGUI.EndDisabledGroup();
  27. }
  28. static AssetModeNavigation()
  29. {
  30. ToolbarExtender.AddToLeft(new AssetModeNavigation());
  31. }
  32. static class ToolbarStyles
  33. {
  34. public static readonly GUIStyle assetModeButtonStyle;
  35. static ToolbarStyles()
  36. {
  37. assetModeButtonStyle = new GUIStyle("Command")
  38. {
  39. fixedWidth = 100,
  40. fontSize = 13,
  41. alignment = TextAnchor.MiddleCenter,
  42. imagePosition = ImagePosition.ImageAbove,
  43. fontStyle = FontStyle.Bold
  44. };
  45. }
  46. }
  47. }
  48. }