Created
May 30, 2022 18:21
-
-
Save ErnSur/e7442f19110e85ae1763112f7eda36b8 to your computer and use it in GitHub Desktop.
Export Unity Editor USS files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.UIElements; | |
namespace QuickEye.Editor | |
{ | |
internal class UssExporter : EditorWindow | |
{ | |
private static AssetBundle EditorAssetBundle; | |
[MenuItem("Window/UI Toolkit/Uss Exporter")] | |
public static void OpenWindow() | |
{ | |
var wnd = GetWindow<UssExporter>(); | |
wnd.titleContent = new GUIContent("Uss Exporter"); | |
} | |
private string[] paths; | |
private void OnEnable() | |
{ | |
if (EditorAssetBundle == null) | |
EditorAssetBundle = GetEditorAssetBundle(); | |
paths = GetUssPaths(); | |
} | |
private void CreateGUI() | |
{ | |
var listView = new ListView(); | |
listView.style.flexGrow = 1; | |
listView.fixedItemHeight = 16; | |
listView.makeItem = () => new Label(); | |
listView.bindItem = (element, i) => | |
{ | |
var e = (Label)element; | |
e.text = paths[i]; | |
}; | |
listView.itemsSource = paths; | |
listView.onItemsChosen += objects => | |
{ | |
foreach (string path in objects) | |
{ | |
Export(path); | |
} | |
}; | |
rootVisualElement.Add(listView); | |
} | |
public static void Export(string sheetPath) | |
{ | |
var sheet = EditorGUIUtility.Load(sheetPath) as StyleSheet; | |
var projectPath = $"Assets/Uss/{Path.GetFileName(sheetPath)}"; | |
if (Path.GetExtension(projectPath) == ".asset") | |
projectPath = Path.ChangeExtension(projectPath, null); | |
Directory.CreateDirectory(Path.GetDirectoryName(projectPath)); | |
Debug.Log($"created at: {projectPath}"); | |
WriteStyleSheet(sheet, projectPath); | |
AssetDatabase.ImportAsset(projectPath); | |
} | |
public static string[] GetUssPaths() | |
{ | |
return (from path in EditorAssetBundle.GetAllAssetNames() | |
let asset = EditorAssetBundle.LoadAsset<StyleSheet>(path) | |
where asset != null | |
select path).ToArray(); | |
} | |
public static void WriteStyleSheet(StyleSheet sheet, string path) | |
{ | |
var ass = typeof(EditorWindow).Assembly; | |
var type = ass.GetType("UnityEditor.StyleSheets.StyleSheetToUss"); | |
var method = type.GetMethod( | |
"WriteStyleSheet", | |
BindingFlags.Public | BindingFlags.Static); | |
method.Invoke(null, new object[] { sheet, path, null }); | |
} | |
private static AssetBundle GetEditorAssetBundle() | |
{ | |
var editorGUIUtility = typeof(EditorGUIUtility); | |
var getEditorAssetBundle = editorGUIUtility.GetMethod( | |
"GetEditorAssetBundle", | |
BindingFlags.NonPublic | BindingFlags.Static); | |
return (AssetBundle)getEditorAssetBundle.Invoke(null, null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment