I am trying to create a Text that describes an object on Runtime, the GUIText object is created but it is not visible. any ideas ?
here is my code :
void enemydescriptionInit( GameObject text, string desc){
text.transform.parent = enemyCar1.car.transform;
enemyCar1.car.transform.position = enemyCar1.startPosiotion;
text.gameObject.GetComponent<GUIText> ().font = new Font ("Arial");
text.gameObject.GetComponent<GUIText>().text = desc;
text.gameObject.GetComponent<GUIText>().fontSize = 40;
text.gameObject.GetComponent<GUIText>().fontStyle = FontStyle.BoldAndItalic;
text.gameObject.GetComponent<GUIText>().color = Color.blue;
text.gameObject.name = "Desc";
}
void Start () {
player = GameObject.Find ("Player");
text = new GameObject();
text.AddComponent<GUIText>();
}
void Update(){
enemydescriptionInit (text, "HELLO");
}
If you're using Unity 5 try using UnityEngine.UI
And instead of GUITextjust use Text
Hope it helps
Try on OnGUI() method
this example code
void OnGUI(){
GUIStyle style = new GUIStyle();
style.fontSize = 30;
style.alignment = TextAnchor.UpperRight;
GUI.Label (new Rect(Screen.width/2 , 10 , 100 ,40) , "Time left " + secondsLeft.ToString() , style);
}
Related
I want to develop an experimental program in unity. In the program, I use scripts to create a series of objects and assign values to them. I use Tobii's SDK to develop an eye movement experimental program. In order to get the line of sight feedback of these objects (actually a ray collision detection principle), I created a script and added this script for each object. But when I run, I find that only the last of the series of objects I create will give correct feedback, and the other objects will not give feedback。
My feedback script is as follow:
void Update () {
if (_gazeAwareComponent.HasGazeFocus) {
img.color = _outColor;
newText = textName.text;
MainInteraction.isEnter = true;
} else {
img.color = initinalColor;
newText = null;
MainInteraction.isEnter = false;
}
}
I use a static variable to control the state of feedback.
And in my main script, my fuction is:
void createTargetAndText (int number) {
int[] a = GetRandomSequence(10, currentTrailTime);
for (int i = 0; i < number; i++) {
GameObject image = new GameObject();
image.transform.localScale = Vector3.one;
image.name = a[i].ToString();
image.AddComponent<Image>();
image.AddComponent<SphereCollider>();
image.GetComponent<SphereCollider>().radius = targetSize / 2;
image.AddComponent<Feedback>();
image.GetComponent<Image>().sprite = sourceImage;
image.transform.SetParent(currentGameObject.transform.GetChild(3).transform);
image.layer = LayerMask.NameToLayer("UI");
GameObject textGame = new GameObject();
textGame.name = a[i].ToString();
textGame.AddComponent<Text>();
textGame.GetComponent<Text>().font = textFont;
textGame.GetComponent<Text>().text = textGame.name;
textGame.GetComponent<Text>().fontSize = 48;
textGame.GetComponent<Text>().alignment = TextAnchor.MiddleCenter;
textGame.GetComponent<Text>().color = Color.black;
textGame.transform.SetParent(image.transform);
textGame.layer = LayerMask.NameToLayer("UI");
}
}
The only problem is MainInteration.isEnter doesn't work, when I try to change the color to debug, it works.
if (_gazeAwareComponent.HasGazeFocus) {
img.color = _outColor;
newText = textName.text;
MainInteraction.isEnter = true;
if (this.name == "2") {
MainInteraction.isEnter = true;
img.color = Color.cyan;
}
}
I guess the problem may come from:
the static variables in Unity.
Can someone help me answer it? Thank you
I'm trying to create text dynamically so I would like to create text in code. I tried doing it like this:
private void DrawIndexNumber()
{
Text txt = new GameObject().AddComponent<Text>();
txt.name = "Ind";
txt.transform.position = _posOfOrigin;
txt.material = materialNum;
txt.font = Font.CreateDynamicFontFromOSFont("arial", 16);
txt.text = "TEST";
}
The above code creates the text object but does not display it.
All UI elements must be inside a Canvas GameObject to be visible. So what you can do is add the Canvas GameObject in edit mode. Or alternatively in runtime:
private void DrawIndexNumber()
{
Canvas canvas = new GameObject().AddComponent<Canvas>();
Text txt = new GameObject().AddComponent<Text>();
txt.name = "Ind";
txt.transform.position = _posOfOrigin;
txt.transform.parent = canvas.transform;
txt.material = materialNum;
txt.font = Font.CreateDynamicFontFromOSFont("arial", 16);
txt.text = "TEST";
}
I customize the background color and its tint color with this code:
var blackBGColor = new UIColor(new nfloat(40) / 255f,
new nfloat(40) / 255f,
new nfloat(40) / 255f, 1f);
this.MoreNavigationController.TopViewController.View.BackgroundColor = blackBGColor;
var moreTableView = (UITableView)this.MoreNavigationController.TopViewController.View;
moreTableView.TintColor = UIColor.White;
foreach (var cell in moreTableView.VisibleCells)
{
cell.BackgroundColor = blackBGColor;
cell.TextLabel.TextColor = UIColor.White;
var selectedView = new UIView
{
BackgroundColor = UIColor.DarkGray
};
cell.SelectedBackgroundView = selectedView;
}
this.MoreNavigationController.NavigationBar.BarStyle = UIBarStyle.Black;
this.MoreNavigationController.NavigationBar.Translucent = false;
this.MoreNavigationController.NavigationBar.TintColor = UIColor.White;
this.MoreNavigationController.NavigationBar.BarTintColor = new UIColor(24f / 255f, 24f / 255f, 24f / 255f, 1f);
But I couldn't change the badge color inside UIMoreNavigationController.
I've tried this:
this.MoreNavigationController.TopViewController.TabBarItem.BadgeColor = UIColor.White
but it's not working.
Tried this one too inside WillShowViewController:
this.ViewControllers[4].TabBarItem.BadgeColor = UIColor.White
but still not working.
Is there any way to change the badge color?
UPDATE:
After investigating the hierarchy of MoreNavigationController, apparently the badge value for Priority and DueBy tab is assign to a UILabel inside _UITableViewCellBadgeNeue. The hierarchy is:
this.MoreNavigationController.ViewControllers[0]: this is a UIMoreListController
Get the View and cast it to UITableView because that View is a _UIMoreListTableView
Then iterate inside that tableview VisibleCells, check the IEnumerator and in the forth object there is _UITableViewCellBadgeNeue
The SubViews[0] inside _UITableViewCellBadgeNeue is a UILabel and the label's text is the badge value.
Based on that, I change the label TextColor and BackgroundColor in WillShowViewController. It works but I need to go back and forth from Priority/DueBy tab to More tab. It never works on the first time.
[Export("navigationController:willShowViewController:animated:")]
public void WillShowViewController(UINavigationController navigationController, UIViewController viewController, bool animated)
{
if (this.MoreNavigationController.ViewControllers.Contains(viewController))
{
this.ViewControllers[4].TabBarItem.BadgeValue = this.ViewModel.SelectedPrioritiesFilter.Count > 0 ? this.ViewModel.SelectedPrioritiesFilter.Count.ToString() : null;
this.ViewControllers[5].TabBarItem.BadgeValue = this.ViewModel.SelectedDueByFilter != null ? "1" : null;
//this is the code to change the color
var vc = this.MoreNavigationController.ViewControllers[0];
var moreTableView = (UITableView)vc.View;
foreach (var cell in moreTableView.VisibleCells)
{
var enumerator = cell.GetEnumerator();
var i = 0;
while (enumerator.MoveNext())
{
//_UITableViewCellBadgeNeue is in the forth object
if(i == 3)
{
//_UITableViewCellBadgeNeue is a UIView
if (enumerator.Current is UIView)
{
var current = (UIView)enumerator.Current;
if (current != null)
{
if (current.Subviews.Length > 0)
{
var label = (UILabel)current.Subviews[0];
label.TextColor = UIColor.White;
label.BackgroundColor = UIColor.Clear;
}
}
}
}
i++;
}
}
}
}
I reproduced your issue and figured the reason out.
It is because the TableView has not finished rendering(drawing) when you retrieve the view hierarchy in WillShowViewController.
My test
View Hierarchy in WillShowViewController
UITableViewCellContentView
UIButton
View Hierarchy in DidShowViewController
UITableViewCellContentView
UIButton
_UITableViewCellBadgeNeue
_UITableViewCellSeparatorView
UITableViewCellContentView
UIButton
_UITableViewCellSeparatorView
So you just need replace the WillShowViewController with DidShowViewController.
PS: Small Suggestion
To avoid the change of View Hierarchy by Apple, you can use the condition like if (view.Class.Name.ToString() == "_UITableViewCellBadgeNeue") instead of judging which level the _UITableViewCellBadgeNeue is.
Here is what happened:
In Scene one:
void OnGUI() {
string _text = "Options";
GUIContent content = new GUIContent ();
content.image = (Texture2D)imageForTexture;
content.text = _text;
GUI.skin.button.normal.background = (Texture2D)content.image;
GUI.skin.button.hover.background = (Texture2D)content.image;
GUI.skin.button.active.background = (Texture2D)content.image;
GUIStyle myButtonStyle = new GUIStyle(GUI.skin.button);
myButtonStyle.fontSize = 20;
myButtonStyle.normal.textColor = Color.green;
GUI.Button(new Rect(Screen.width/2-700, Screen.height/2 -200 ,300,100),content,myButtonStyle);
}
In Scene two:
void OnGUI()
{
GUI.Button(new Rect(Screen.width/2-700, Screen.height/2 -200 ,300,100),"Instruction");
}
Here what the buttons look like:
Your experience is always supportive. Thanks in advance!
How do I program it so that I can change the font type to: Coalition or Arial...
Here is my current code...
using UnityEngine;
using System.Collections;
public class InvSlotHandler : MonoBehaviour {
private int excess = 1;
UILabel lbl;
void Start() {
GameObject label = new GameObject();
lbl = label.AddComponent<UILabel>();
label.transform.name = "#QTY";
lbl.fontStyle = FontStyle.Normal;
lbl.fontSize = 15;
lbl.alignment = NGUIText.Alignment.Right;
NGUITools.AddChild (gameObject.transform.gameObject, label.gameObject);
}
void FixedUpdate() {
lbl.text = gameObject.transform.childCount - excess + "";
}
}
Here is an example of how to change the font of a UILabel that uses a dynamic font in NGUI.
The label shows some text in the original font for 2 seconds, then switches to the other font (the one you assign to otherFont in the inspector)
using UnityEngine;
using System.Collections;
public class ChangeFont : MonoBehaviour {
public UILabel label;
public Font otherFont;
IEnumerator Start() {
label.text = "This is a bit of text"; //show text
yield return new WaitForSeconds(2f); //wait 2 seconds
label.trueTypeFont = otherFont; //change font
}
}
If your label was set to use a bitmap font, you'd assign a UIFont to label.bitmapFont instead.