- Home /
Copy/Paste GuiStyle in the Inspector
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 ?
Thanks Jerome
Answer by Ashkan_gc · Jan 18, 2010 at 02:31 PM
you can use AngryAnt's copy inspector script. the script can copy and paste any component.
The question is about variables, not entire components. i.e. copying a GUIStyle variable from one GUISkin element to another in the same skin, or between arbitrary GUIStyle variables.
Answer by andeeee · Jan 18, 2010 at 02:11 PM
There isn't any way currently to copy/paste variable values in the inspector.
don't talk about things that you are not sure about. you don't have to answer all questions. you can write "i don't know any way but i think ...". answer those questions that you are sure about them and you have clear answers or good ideas about them.
It seems to me that, @andeeee is talking about variables in the inspector (which is what the question is asking about), whereas the other answers are talking about whole components, which is not the question.
Waz is correct. Possibly Ashkan_gc should refrain from talking about things he isn't sure about.
Answer by Waz · Jun 28, 2011 at 01:19 AM
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.
// 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[i] = skin.customStyles[i].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]);
}
}
}
}
Answer by Molix · Jan 28, 2010 at 03:07 AM
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[i];
}
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";
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613200850im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Advanced Scripting Features 2 Answers
Resizing an array of gameObjects 2 Answers