• 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
3
Question by eurosat7 · Feb 25, 2010 at 01:55 PM · instantiatecloneskingui-skin

Cloning a GUISkin? Or: Some GUILayout.TextField with alignment right

I have my own skin and I think it will be worked over by a real graphical artist one day.

so: I have some TextFields with numbers in them and want them aligned to the right. But I also have some TextFields aligned to the left.

I first thought: Make 2 skins with the inspector and just change the Alignment. But I cannot do that if the skin will be overhauled.

So I tried to clone a skin on Start() and just change the align:

InputAlignRightStyle=mainmenuSkin;
InputAlignRightStyle.textField.alignment=TextAnchor.MiddleRight;

But both var are pointing to the same reference so both skins will be modified. Then I tried using Instantiate() but I think it only works for Classes/Objects but not for Instances.

This:

InputAlignRightStyle=Instantiate(mainmenuSkin);

... throws a cryprical error:

metaFlags & kStrongPPtrMask UnityEngine.Object:Internal_CloneSingle(Object) UnityEngine.Object:Internal_CloneSingle(Object) UnityEngine.Object:Instantiate(Object) GuiScript:Start() (at Assets\GUI\GuiScript.js:34)

[....\Runtime\Serialize\TransferUtility.cpp line 352] (Filename: Assets/GUI/GuiScript.js Line: 34)

Any ideas?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Bampf · Jun 21, 2010 at 11:10 AM

Several workarounds come to mind.

1 . Non-programmatic: GUISkins can have any number of custom styles, in addition to the standard ones (Button, Label, etc.) The skin contains an array of them, so in the inspector you can change the array count, and use them for whatever you want.

You'd have to tell your artist to maintain the custom style(s) as well. To help keep things maintainable you might consider having a sample scene that demos all the styles that you're using in the app, that way the artist won't neglect any, or work on ones you aren't using.

2 . Programmatic: if most controls are left aligned you could simply save off the value of the alignment setting you want to override, change it to right-aligned, the restore it once you've drawn your right-aligned controls.

3 . Programmatic: it wouldn't take you long to write your own method to copy all the values from one GUIStyle to another. Downside is if Unity Tech adds or removes guistyle elements in future updates you will have to update your method as well. That risk/effort seems small to me.

You could combine 3 & 1: at program/scene start up, copy your text style to a custom style of the skin, changing the alignment. Then all objects referencing that skin would have access to the right-aligned style, and the artist wouldn't have to maintain it.

Hope these help.

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
2

Answer by Mike 3 · Jun 21, 2010 at 11:14 AM

You can do something like this (note: you'd want to store the variable outside of start, i've just layed it out like this for legibility of type):

GUIStyle textFieldStyle = new GUIStyle("textfield");
textFIeldStyle.anchor = TextAnchor.MiddleRight;

Note: if you clone the GUIStyle this way, you need to pass it to the TextField as the last parameter

Comment
Add comment · Show 2 · 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 Bampf · Jun 21, 2010 at 05:47 PM 0
Share

What values does the new GUIStyle get? Is it a clone of the default Unity skin, the currently active skin, or what?

avatar image Mike 3 · Jun 23, 2010 at 12:22 AM 0
Share

That code clones the textfield style from the current skin

avatar image
2

Answer by Amadhia · Mar 16, 2012 at 05:33 PM

I've had a devil of a time with this too... (I was referencing a Style from a GUISkin, and the font-size changes I was making in the Style that were supposed to be temporary, were being imprinted in the GUISkin's permanent settings.)

I've not yet found a way to clone a whole GUISkin in a way where changes to the copy would not effect the original...

...But I've found this solution where I can create a clone of a specific Style within the referenced GUISkin (eg: box, textField, customName, etc.) where changes to that style do not impact the originally referenced GUISkin


I've run this solution several times and it looks to me like it tests-out fine: the original GUISkin values never change as my other scripts muck-about with all the different font-re-sizing I need:

(Declarations)

 var skinWithStyleIWant: GUISkin;
 private var clonedStyle: GUIStyle;

(In Start)

 clonedStyle = new GUIStyle(skinWithStyleIWant.GetStyle("outlined"));

.

...Where the style I want to clone is a custom style named, "outlined"

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
2

Answer by Kurt-Dekker · Jun 26, 2013 at 07:23 PM

Actually, Instantiate works for a GUISkin... you just need to remember to a) only instantiate it once (not every run through your OnGUI() method!), and b) to Destroy it when you're done, or just let the scene transition destroy it naturally.

This appears to make an entire copy of a GUISkin (all its GUIStyles) and then you can modify those, and it won't modify the original asset.

With that in mind, doing it this way is a bit "heavyweight," and generates quite a few new objects. I found this out the hard way by inadvertently Instantiate()-ing a copy each frame. :)

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
1

Answer by stijn · Dec 23, 2010 at 07:00 PM

I think what you wanna do is clone a GUIStyle:

GUIStyle clone = new GUIStyle (orig);
clone.anchor = TextAnchor.MiddleRight;
GUILayout.TextField("hello", clone);

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Cloned interactivecloth not acting like original interactivecloth. 0 Answers

Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers

How to Parent a Cloned Object to Another Cloned Object 1 Answer

Instantiate Clones Itself - Rather than Prefab 0 Answers

how can i take reference to the clone and not to the Gameobject? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges