I trying to write an editor script to copy/paste whole content of a GUI style because I need refactoring GUI part of my project.
But I don't know how to start and firstly make a context menu on a GUIStyle part of a GUISkin for exemple... if it is possible ?
Since individuals variables cannot be copied in the inspector (unless they happen to be strings or ints), I wrote a custom editor for GUISkin. To use it, save the following as Assets/Editor/GUISkinExtensions.js. You should then get extra lines at the bottom of the inspector for a GUISkin that allow you to copy styles. I find this very useful when you want to make a variation of an existing styles - if it’s a custom style you can just CTRL-D to duplicate, but if it’s a builtin like label, you need this script.
![alt text][1]
// See http://answers.unity3d.com/questions/9844/copypaste-guistyle-in-the-inspector.html
//
@CustomEditor(GUISkin)
class GUISkinExtensions extends Editor
{
static final var builtinGUISkinStyles : String[] = [
"box", "button", "toggle",
"label", "textField", "textArea",
"window",
"horizontalSlider", "horizontalSliderThumb",
"verticalSlider", "verticalSliderThumb",
"horizontalScrollbar", "horizontalScrollbarThumb",
"horizontalScrollbarLeftButton", "horizontalScrollbarRightButton",
"verticalScrollbar", "verticalScrollbarThumb",
"verticalScrollbarUpButton", "verticalScrollbarDownButton",
"scrollView"
];
var from = 0;
var to = 0;
function OnInspectorGUI()
{
var skin = target as GUISkin;
DrawDefaultInspector();
EditorGUILayout.Space();
var names = builtinGUISkinStyles;
var customNames = new String[skin.customStyles.Length];
for (var i=0; i<customNames.Length; ++i) {
customNames _= skin.customStyles*.name;*_
* }* * names += customNames;*
* from = EditorGUILayout.Popup(“From:”,from,names);* * to = EditorGUILayout.Popup(“To:”,to,names);* * if (GUILayout.Button(“Copy”)) {* * var fs = skin.GetStyle(names[from]);* * var ts = skin.GetStyle(names[to]);* * var newStyle = new GUIStyle(fs);* * newStyle.name = ts.name;* * var custom = to - builtinGUISkinStyles.Length;* * if (custom >= 0) {* * skin.customStyles[custom] = newStyle;* * } else {* * skin.GetType().InvokeMember(names[to], System.Reflection.BindingFlags.SetProperty, null, skin, [newStyle]);* * }* * }* * }* }
_*[1]: http://i.imgur.com/rxZqO.png*_
I encountered a similar need, since our skin had a ton of custom styles. Since GUISkins have the property of saving modifications made to them while playing the game, I made a this quick and dirty script that lets you duplicate, delete and sort your skin's custom styles. It is also helpful when you just want to see what is in a skin.
Nothing fancy, but I've found it very useful.
using UnityEngine;
using System;
using System.Collections;
//
// Some functionality to clean up skins with lots of custom styles
// Since skins are saved even while playing, we can just play and edit
// Usage
// - new scene
// - add an empty game object
// - add this script to that object
// - set the skin to the one you want to edit
// - play
//
public class SkinHelper : MonoBehaviour
{
public GUISkin skin;
private Vector2 scrollView;
private Rect area;
private string customText = "Button";
void Awake()
{
area = new Rect(0,0,Screen.width, Screen.height);
}
void OnGUI()
{
if( skin == null )
{
GUILayout.Label("No skin set for SkinHelper");
return;
}
GUILayout.BeginArea( area );
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Label("Skin: " + skin.name);
GUILayout.Label("Custom Styles: " + skin.customStyles.Length);
if( GUILayout.Button("Sort by name") )
{
Array.Sort( skin.customStyles, StyleComparer );
}
GUILayout.EndHorizontal();
// column headers
GUILayout.BeginHorizontal("Box");
GUILayout.Label("Custom Styles", GUILayout.Width(area.width*0.2f));
Rect r = GUILayoutUtility.GetRect( new GUIContent("Duplicate"), "Button" );
Rect r2 = GUILayoutUtility.GetRect( new GUIContent("Delete"), "Button" );
GUI.Label(new Rect(r.x,r.y,r.width+r2.width,r.height), "Operations");
GUILayout.Label("Examples");
GUILayout.FlexibleSpace();
GUILayout.Label("Custom Text:");
customText = GUILayout.TextArea( customText );
GUILayout.EndHorizontal();
scrollView = GUILayout.BeginScrollView( scrollView );
GUILayout.BeginVertical();
foreach( GUIStyle cust in skin.customStyles )
{
GUILayout.BeginHorizontal();
GUILayout.Label(cust.name, GUILayout.Width(area.width*0.2f));
if( GUILayout.Button("Duplicate") )
{
DuplicateStyle( cust );
}
if( GUILayout.Button("Delete") )
{
RemoveStyle( cust );
}
// some examples
GUILayout.Label("Label", cust );
GUILayout.FlexibleSpace();
GUILayout.Box("Box", cust );
GUILayout.FlexibleSpace();
GUILayout.Button(customText, cust );
GUILayout.FlexibleSpace();
GUI.enabled = false;
GUILayout.Box("Disabled Box", cust );
GUILayout.FlexibleSpace();
GUILayout.Button("Disabled Button", cust );
GUILayout.FlexibleSpace();
GUI.enabled = true;
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
GUILayout.EndScrollView();
GUILayout.EndVertical();
GUILayout.EndArea();
}
int StyleComparer( GUIStyle x, GUIStyle y )
{
return String.Compare( x.name, y.name );
}
void RemoveStyle( GUIStyle style )
{
int removeIdx = Array.IndexOf( skin.customStyles, style );
if( removeIdx < 0 )
return;
// move each item back one, then resize off the end
for( int i=removeIdx+1; i<skin.customStyles.Length; i++ )
{
skin.customStyles[i-1] = skin.customStyles*;*
* }*
* Array.Resize( ref skin.customStyles, skin.customStyles.Length-1 );*
*}*
*void DuplicateStyle( GUIStyle style )*
*{*
* Array.Resize( ref skin.customStyles, skin.customStyles.Length+1 );*
* skin.customStyles[skin.customStyles.Length-1] = new GUIStyle(style);*
* skin.customStyles[skin.customStyles.Length-1].name += " Copy";*
*}*
*}*
*```*