• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Jerome · Jan 07, 2010 at 02:43 PM · guieditor-scripting

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Waz · Jun 28, 2011 at 01:21 AM 0
Share

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.

avatar image
1

Answer by andeeee · Jan 18, 2010 at 02:11 PM

There isn't any way currently to copy/paste variable values in the inspector.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ashkan_gc · Jan 18, 2010 at 02:31 PM 0
Share

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.

avatar image Waz · Jun 28, 2011 at 01:10 AM 0
Share

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.

avatar image Nition · Oct 24, 2013 at 03:49 AM 0
Share

Waz is correct. Possibly Ashkan_gc should refrain from talking about things he isn't sure about.

avatar image
1

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.

alt text

 // 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]);
             }
         }
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 &lt; 0 )
         return;

     // move each item back one, then resize off the end
     for( int i=removeIdx+1; i&lt;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";
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Advanced Scripting Features 2 Answers

Resizing an array of gameObjects 2 Answers

How to make a monolgue? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges