Created
April 13, 2024 08:48
-
-
Save lilxyzw/a3d0712513182ca09d56ba93d64d88c4 to your computer and use it in GitHub Desktop.
ドキュメント用にエディタをキャプチャするやつ
This file contains hidden or 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
| /* | |
| MIT License | |
| Copyright (c) 2024 lilxyzw | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| */ | |
| #if UNITY_EDITOR | |
| using System.IO; | |
| using System.Threading.Tasks; | |
| using UnityEngine; | |
| using UnityEngine.UIElements; | |
| using UnityEditor; | |
| using UnityEditor.UIElements; | |
| namespace jp.lilxyzw.editorcapture | |
| { | |
| public class CaptureWindow : EditorWindow | |
| { | |
| // 多言語対応する場合はコールバックを設定するか、CaptureWindowを継承したclassを作成してoverride | |
| public string fileName = "lilEditorCapture/{date}_{lang}"; | |
| public Texture2D overlay; // 上から重ねるテクスチャ キャプチャする解像度と合わせること | |
| public System.Func<int> GetLanguageSizeCallback; | |
| public System.Func<int, string> GetLanguageNameCallback; | |
| public System.Action<int> UpdateLanguageCallback; | |
| public System.Action EndCaptureCallback; | |
| protected virtual int GetLanguageSize() => GetLanguageSizeCallback?.Invoke() ?? 0; // 言語バリエーション数 | |
| protected virtual string GetLanguageName(int count) => GetLanguageNameCallback?.Invoke(count) ?? count.ToString(); // ファイル名を気にしないなら不要 | |
| protected virtual void UpdateLanguage(int count) => UpdateLanguageCallback?.Invoke(count); // 言語切替処理 | |
| protected virtual void EndCapture() => EndCaptureCallback?.Invoke(); // キャプチャ完了後に言語を戻したい場合はこれを使用 | |
| public static CaptureWindow CreateWindow(Object target) | |
| { | |
| var window = CreateInstance<CaptureWindow>(); | |
| window.Initialize(target); | |
| return window; | |
| } | |
| public static CaptureWindow CreateWindow(Object target, Vector2 captureSize) | |
| { | |
| var window = CreateInstance<CaptureWindow>(); | |
| window.captureSize = captureSize; | |
| window.Initialize(target); | |
| return window; | |
| } | |
| protected void Initialize(Object target) | |
| { | |
| this.target = target; | |
| Show(); | |
| Focus(); | |
| CreateGUI(); | |
| Resize(); | |
| } | |
| public void SetScrollPosition(Vector2 position) => scrollView.scrollOffset = position; | |
| public async Task CaptureAllLanguages(Vector2 position) | |
| { | |
| SetScrollPosition(position); | |
| await CaptureAllLanguages(); | |
| } | |
| public async Task CaptureAllLanguages() | |
| { | |
| languageSize = GetLanguageSize(); | |
| count = 0; | |
| frameCount = -1; | |
| SetNext(); | |
| while(!isCaptured) await Task.Delay(100); | |
| } | |
| public void Capture(string lang = "") | |
| { | |
| Focus(); | |
| var pos = new Vector2(position.position.x, position.position.y + scrollView.worldBound.y); | |
| var w = (int)position.width; | |
| var h = (int)position.yMax + 21 - (int)pos.y; | |
| var pixels = UnityEditorInternal.InternalEditorUtility.ReadScreenPixel(pos, w, h); | |
| if(overlay) | |
| { | |
| var pixelsO = overlay.GetPixels(); | |
| for(int i = 0; i<pixels.Length; i++) | |
| { | |
| pixels[i] = Color.Lerp(pixels[i], pixelsO[i], pixelsO[i].a); | |
| pixels[i].a = 1; | |
| } | |
| } | |
| var texture = new Texture2D(w, h, TextureFormat.RGBA32, false); | |
| texture.SetPixels(pixels); | |
| string output = fileName.Replace("{date}", System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")).Replace("{lang}", lang); | |
| Directory.CreateDirectory(Path.GetDirectoryName(output)); | |
| File.WriteAllBytes($"{output}.png", texture.EncodeToPNG()); | |
| } | |
| [MenuItem("Tools/lilEditorCapture/Create Window")] | |
| private static void CreateWindow() | |
| { | |
| if(Selection.activeObject is GameObject obj) foreach(var c in obj.GetComponents<Component>()) CreateWindow(c); | |
| else CreateWindow(Selection.activeObject); | |
| } | |
| [SerializeField] private Vector2 captureSize = new Vector2(500,600); | |
| private Object target; | |
| private ScrollView scrollView; | |
| private float frameCount; | |
| private int languageSize = 0; | |
| private int count = 0; | |
| private bool isCaptured = false; | |
| private void CreateGUI() | |
| { | |
| rootVisualElement.Clear(); | |
| var scrollLabel = new Label("Scroll Position: (0, 0)"); | |
| scrollLabel.selection.isSelectable = true; | |
| rootVisualElement.Add(scrollLabel); | |
| var sizeField = new Vector2Field("Capture Size"){bindingPath = "captureSize"}; | |
| sizeField.Bind(new SerializedObject(this)); | |
| rootVisualElement.Add(sizeField); | |
| rootVisualElement.Add(new Button(() => Resize()){text = "Resize"}); | |
| rootVisualElement.Add(new Button(() => {Capture(); Close();}){text = "Capture"}); | |
| #pragma warning disable CS4014 | |
| rootVisualElement.Add(new Button(() => CaptureAllLanguages()){text = "Capture All Languages"}); | |
| #pragma warning restore CS4014 | |
| scrollView = new ScrollView(ScrollViewMode.Vertical); | |
| if(target is Material m) | |
| { | |
| var materialEditor = (MaterialEditor)Editor.CreateEditor(new[]{m}, typeof(MaterialEditor)); | |
| var imgui = new IMGUIContainer(() => | |
| { | |
| EditorGUIUtility.hierarchyMode = true; | |
| EditorGUILayout.BeginVertical(EditorStyles.inspectorDefaultMargins); | |
| materialEditor.customShaderGUI.OnGUI(materialEditor, MaterialEditor.GetMaterialProperties(new[]{m})); | |
| EditorGUILayout.EndVertical(); | |
| }); | |
| imgui.style.marginLeft = 16; | |
| imgui.style.overflow = Overflow.Visible; | |
| imgui.AddToClassList(InspectorElement.iMGUIContainerUssClassName); | |
| scrollView.Add(imgui); | |
| } | |
| else | |
| { | |
| scrollView.Add(new IMGUIContainer(() => EditorGUILayout.InspectorTitlebar(true, target))); | |
| scrollView.Add(new InspectorElement(target)); | |
| } | |
| scrollView.RegisterCallback<MouseCaptureOutEvent>((e) => scrollLabel.text = $"Scroll Position: ({(int)scrollView.scrollOffset.x}, {(int)scrollView.scrollOffset.y})"); | |
| scrollView.RegisterCallback<WheelEvent>((e) => scrollLabel.text = $"Scroll Position: ({(int)scrollView.scrollOffset.x}, {(int)scrollView.scrollOffset.y})"); | |
| rootVisualElement.Add(scrollView); | |
| } | |
| private void Resize() => position = new Rect(position.x, position.y, captureSize.x, captureSize.y - 21 + (scrollView.worldBound.y == 0 ? 119 : scrollView.worldBound.y)); | |
| private void CapturePerLang() | |
| { | |
| if(++frameCount < 1) return; | |
| frameCount = -1; | |
| EditorApplication.update -= CapturePerLang; | |
| Capture(GetLanguageName(count)); | |
| if(++count >= languageSize) | |
| { | |
| isCaptured = true; | |
| EndCapture(); | |
| Close(); | |
| return; | |
| } | |
| SetNext(); | |
| } | |
| private void SetNext() | |
| { | |
| UpdateLanguage(count); | |
| Repaint(); | |
| EditorApplication.update += CapturePerLang; | |
| } | |
| } | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment