Guest User

Untitled

a guest
Nov 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System;
  4.  
  5. // Create an editor window which can display a chosen GameObject.
  6. // Use OnInteractivePreviewGUI to display the GameObject and
  7. // allow it to be interactive.
  8.  
  9. public class PreviewSpriteMaker : EditorWindow {
  10. private static GameObject gameObject;
  11. private static Editor gameObjectEditor;
  12. Vector2Int resolution = new Vector2Int(128,128);
  13. readonly string[] options_s = new string[] {"16","32","64","128","256","512"};
  14. readonly int[] options_i = new int[] {16,32,64,128,256,512};
  15.  
  16. [MenuItem("Window/Preview Sprite Maker")]
  17. static void ShowWindow() {
  18. GetWindow<PreviewSpriteMaker>(true, "Preview Sprite Maker", true);
  19. }
  20.  
  21. void OnGUI() {
  22. EditorGUI.BeginChangeCheck();
  23. GUILayout.BeginHorizontal();
  24. resolution.x = EditorGUILayout.IntPopup(resolution.x,options_s,options_i);
  25. resolution.y = EditorGUILayout.IntPopup(resolution.y,options_s,options_i);
  26.  
  27. EditorGUI.BeginDisabledGroup(gameObjectEditor == null);
  28. if (GUILayout.Button("Save")) {
  29. Texture2D tex = gameObjectEditor.RenderStaticPreview("", null, resolution.x,resolution.y);
  30. Sprite sprite = Sprite.Create(tex, new Rect(0,0,resolution.x,resolution.y), Vector2.zero);
  31. string path = EditorUtility.SaveFilePanelInProject("Save Sprite","NewSprite", "asset", "");
  32. AssetDatabase.CreateAsset(tex, path);
  33. }
  34. EditorGUI.EndDisabledGroup();
  35. GUILayout.EndHorizontal();
  36. if (EditorGUI.EndChangeCheck()) {
  37. Rect pos = position;
  38. pos.width = Mathf.Max(pos.width, resolution.x);
  39. pos.height = Mathf.Max(pos.height, resolution.y);
  40. position = pos;
  41. }
  42.  
  43. GUIStyle bgColor = new GUIStyle();
  44. bgColor.normal.background = EditorGUIUtility.whiteTexture;
  45.  
  46. if (Selection.activeGameObject != null) {
  47. if (gameObjectEditor == null || Selection.activeGameObject != gameObject) {
  48. gameObjectEditor = Editor.CreateEditor(Selection.activeGameObject);
  49. gameObject = Selection.activeGameObject;
  50. }
  51. gameObjectEditor.OnPreviewGUI(GUILayoutUtility.GetRect(resolution.x, resolution.y, GUILayout.Width(resolution.x), GUILayout.Height(resolution.y)), bgColor);
  52. } else {
  53. gameObjectEditor = null;
  54. }
  55. }
  56. }
Add Comment
Please, Sign In to add comment