Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEditor;
- using UnityEngine;
- using System;
- // Create an editor window which can display a chosen GameObject.
- // Use OnInteractivePreviewGUI to display the GameObject and
- // allow it to be interactive.
- public class PreviewSpriteMaker : EditorWindow {
- private static GameObject gameObject;
- private static Editor gameObjectEditor;
- Vector2Int resolution = new Vector2Int(128,128);
- readonly string[] options_s = new string[] {"16","32","64","128","256","512"};
- readonly int[] options_i = new int[] {16,32,64,128,256,512};
- [MenuItem("Window/Preview Sprite Maker")]
- static void ShowWindow() {
- GetWindow<PreviewSpriteMaker>(true, "Preview Sprite Maker", true);
- }
- void OnGUI() {
- EditorGUI.BeginChangeCheck();
- GUILayout.BeginHorizontal();
- resolution.x = EditorGUILayout.IntPopup(resolution.x,options_s,options_i);
- resolution.y = EditorGUILayout.IntPopup(resolution.y,options_s,options_i);
- EditorGUI.BeginDisabledGroup(gameObjectEditor == null);
- if (GUILayout.Button("Save")) {
- Texture2D tex = gameObjectEditor.RenderStaticPreview("", null, resolution.x,resolution.y);
- Sprite sprite = Sprite.Create(tex, new Rect(0,0,resolution.x,resolution.y), Vector2.zero);
- string path = EditorUtility.SaveFilePanelInProject("Save Sprite","NewSprite", "asset", "");
- AssetDatabase.CreateAsset(tex, path);
- }
- EditorGUI.EndDisabledGroup();
- GUILayout.EndHorizontal();
- if (EditorGUI.EndChangeCheck()) {
- Rect pos = position;
- pos.width = Mathf.Max(pos.width, resolution.x);
- pos.height = Mathf.Max(pos.height, resolution.y);
- position = pos;
- }
- GUIStyle bgColor = new GUIStyle();
- bgColor.normal.background = EditorGUIUtility.whiteTexture;
- if (Selection.activeGameObject != null) {
- if (gameObjectEditor == null || Selection.activeGameObject != gameObject) {
- gameObjectEditor = Editor.CreateEditor(Selection.activeGameObject);
- gameObject = Selection.activeGameObject;
- }
- gameObjectEditor.OnPreviewGUI(GUILayoutUtility.GetRect(resolution.x, resolution.y, GUILayout.Width(resolution.x), GUILayout.Height(resolution.y)), bgColor);
- } else {
- gameObjectEditor = null;
- }
- }
- }
Add Comment
Please, Sign In to add comment