1
\$\begingroup\$

How can I change the default settings for the standard components or is this even possible? E.g. setting the default font-size for a text component so that each new text component I add will have the same font-size?

\$\endgroup\$
  • \$\begingroup\$ I have no idea if this is possible, but you know you could make prefabs right? \$\endgroup\$ – DH. Oct 13 '16 at 11:48
  • \$\begingroup\$ Sure, but I got for example one default font for my whole project, and it's a way better workflow to add a textcomponent which hat set this font as default instead of dragging a prefab into my scene. \$\endgroup\$ – TobiasW Oct 13 '16 at 11:55
1
\$\begingroup\$

I prefer use [ExecuteInEditMode] instead of overriding defaults because you can edit it realtime in editor and you can grouping all texts , then set defaults to different groups of them.I know you want pervent repetitious work(changing defaults) by overriding defaults but you can't grouping them easily. for example:


ExecuteInEditMode methode:

group1(Menu fonts features) : font = 15;fontstyle = Normal,.....

group2(Score board fonts features) : font = 20;fontstyle = Bold ,.....

group3(Button fonts features) : font = 10;fontstyle = Italic,.....

override methode:

all text default fonts = 15;fontstyle = Normal,.....


but If you override them you have to change default again for some text,so you're limit in this way.this is mean you need many overriding classes foreach component.

this is basic using ExecuteInEditMode methode without grouping(for all text component in scene):

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

[ExecuteInEditMode]
public class ChangeFontSize : MonoBehaviour {
    public enum FontStyles{Normal,Bold,Italic,BoldAndItalic};
    public FontStyles ActiveState = FontStyles.Normal;
    public Color color;
    public Text[] AllText;
    public bool ChangeDefault;
    [Range(0,15)]
    public int size;

    void Update(){
        AllText = Object.FindObjectsOfType (typeof(Text)) as Text[];
        if(ChangeDefault == true){
            foreach (Text txt in AllText) {
            txt.fontSize = size;
                FStyle (txt);
            }
        }
    }
    //pass text components here
    void FStyle(Text mytext){
        switch(ActiveState) 
        { 
        // Check one case
        case FontStyles.Normal: 
            //Set Normal Font style
            mytext.fontStyle = FontStyle.Normal;
            break; 
        case FontStyles.Bold: 
            //Set Bold Font style
            mytext.fontStyle = FontStyle.Bold;
            break; 
        case FontStyles.Italic: 
            //Set Italic Font style
            mytext.fontStyle = FontStyle.Italic;
            break; 
        case FontStyles.BoldAndItalic: 
            //Set BoldAndItalic Font style
            mytext.fontStyle = FontStyle.BoldAndItalic;
            break; 
        }
    }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Will I still be able to change the FontStyle manually after adding this script or will it lock the FontStyle option? \$\endgroup\$ – TobiasW Oct 14 '16 at 5:53
0
\$\begingroup\$

Perhaps you can achieve this by extending the component and overriding the right properties? Just spit balling here.

http://answers.unity3d.com/questions/12019/how-can-i-extend-a-component.html

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.