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; }
}