NGUI dynamically changing the label font

Source: Internet
Author: User
The content of this article may not be very high, perhaps almost a little, because many games will not be so boring in the course of the game to change the font, the basic is static pictures or text on it. This article is written to familiarize yourself with how getter and setter are defined in C #. First we create a label button and then create two C # classes, one floatingtext bound to the label, one floatingtextdriver bound to the button, This allows us to assign values to the corresponding parameters in the Inspector panel, when we click on the button, the label text appears, and the position has been followed by the target movement.


The target in the label is empty, and we dynamically added the target object in the Floatingtextdriver onclick event.

The following is a concrete implementation of the Floatingtextdriver and Floatingtext two classes:

Using Unityengine;
Using System.Collections;
 
public class Floatingtextdriver:monobehaviour {public
 
    gameobject target;
    public floatingtext ft;
    public Uifont font;
    IEnumerator OnClick ()
    {
        ft. target = target;
        Ft. Text = "Libufan";
        Ft. Active = true;
 
        Yield return new waitforseconds (2.0f);
 
        Ft. Font = font;
        Ft. FontSize = new Vector3 (1);}
 
}

Using Unityengine;
 
Using System.Collections;
 
    public class Floatingtext:monobehaviour {private UILabel _lbl;
    Public Gameobject _target;
    Public Camera Worldcamera;
    Public Camera Guicamera;
 
    Private Vector3 Pubpos;
 
	private bool _active;
		void Awake () {_lbl = getcomponent<uilabel> ();
		if (_LBL = = null) {Debug.logerror ("Could not fint the uilabel!");
    }} void Start () {Guicamera = Nguitools.findcameraforlayer (Gameobject.layer);
		} public Color TextColor {get {return _lbl.color;}
	set {_lbl.color = value;}
        } public string Text {get {return _lbl.text;}
    set {_lbl.text = value;}
        } public bool Active {get {return _active;}
    set {_active = value;}
        } public gameobject Target {get {return _target;}
            set {_target = value;
        Worldcamera = Nguitools.findcameraforlayer (_target.layer);
 
 }
    }   Public Uifont Font {get {return _lbl.font;}
    set {_lbl.font = value;}
        } public Vector3 FontSize {get {return _lbl.transform.localscale;}
    set {_lbl.transform.localscale = value;}
        } void Lateupdate () {if (!_active) {return;
        } Pubpos = Worldcamera.worldtoviewportpoint (_target.transform.position);
        Pubpos = Guicamera.viewporttoworldpoint (Pubpos);
        pubpos.z = 0;
    Transform.position = Pubpos; }
 
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.