Copy/Paste GuiStyle in the Inspector
Asked Answered
C

4

0

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

Conveyancing answered 31/8, 2023 at 11:9 Comment(0)
S
0

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

Socialminded answered 6/6, 2023 at 1:27 Comment(3)

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.

Steelworks

It seems to me that, @Socialminded 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.

Shipboard

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

Unlovely
S
0

you can use AngryAnt's copy inspector script. the script can copy and paste any component.

Steelworks answered 6/6, 2023 at 1:6 Comment(1)

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.

Shipboard
C
0

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";*
 *}*
*}*
*```*
Cleanse answered 28/1, 2010 at 3:7 Comment(0)
S
0

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 https://mcmap.net/q/38855/unity-copy-paste-guistyle-in-the-inspector
//
@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*_

Shipboard answered 6/6, 2023 at 1:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.