是否可以将Text对象统一固定到特定的GameObject?我想要做的是,创建一个运行时游戏对象并将文本固定到该游戏对象上。我不知道如何创建这个,因为我是Unity游戏开发的初学者。
// Code for Game Objects..
#pragma strict
// Variable for Get Components
var getComponents : GameObject;
// For Moving the Box
var speed : float = 10f;
var movementVector = Vector3.zero;
function Start() {
// Run Time Cube Generator
for ( i = 0 ; i <= 5 ; i++ )
{
if(flag) {
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = "RedCube" + Tagcounter;
cube.AddComponent(GUIText);
// Render Cubes
cube.transform.position = Vector2(posx,posy);
cube.transform.localScale = Vector2(1,1);
cube.renderer.material.color = Color.red;
// cube.AddComponent(Rigidbody);
// cube.AddComponent(GUIText);
cube.transform.Translate(0f,1f * Time.deltaTime,0f);
posy+=2;
cube.gameObject.guiText.text = counter.ToString();
cube.gameObject.guiText.material.color = Color.white;
// cube.guiText.text = Vector3.forward(posx,posy,0);
counter++;
// Text Working
Debug.Log(cube.gameObject.guiText.text);
}
Tagcounter++;
}
}
function Update()
{
var Redcube = GameObject.Find("RedCube0");
var Redcube1 = GameObject.Find("RedCube1");
var Redcube2 = GameObject.Find("RedCube2");
var Redcube3 = GameObject.Find("RedCube3");
var Redcube4 = GameObject.Find("RedCube4");
var Redcube5 = GameObject.Find("RedCube5");
// For Upward Movement
if(Input.GetKey(KeyCode.UpArrow)) {
// For Moving Upward
Redcube.transform.Translate(0f,1f * Time.deltaTime,0f);
Redcube1.transform.Translate(0f,1f * Time.deltaTime,0f);
Redcube2.transform.Translate(0f,1f * Time.deltaTime,0f);
Redcube3.transform.Translate(0f,1f * Time.deltaTime,0f);
Redcube4.transform.Translate(0f,1f * Time.deltaTime,0f);
Redcube5.transform.Translate(0f,1f * Time.deltaTime,0f);
}
// For Downward Movement
if( Input.GetKey(KeyCode.DownArrow)) {
// For Moving Downwards
Redcube.transform.Translate(0f,-1f * Time.deltaTime,0f);
Redcube1.transform.Translate(0f,-1f * Time.deltaTime,0f);
Redcube2.transform.Translate(0f,-1f * Time.deltaTime,0f);
Redcube3.transform.Translate(0f,-1f * Time.deltaTime,0f);
Redcube4.transform.Translate(0f,-1f * Time.deltaTime,0f);
Redcube5.transform.Translate(0f,-1f * Time.deltaTime,0f);
}
// For Left Movement
if( Input.GetKey(KeyCode.S)) {
// IF one moves Left
cube.transform.Translate(-1f * Time.deltaTime,0f,0f);
// Others Move Up First
Redcube.transform.Translate(0f,1f * Time.deltaTime,0f);
Redcube1.transform.Translate(0f,1f * Time.deltaTime,0f);
Redcube2.transform.Translate(0f,1f * Time.deltaTime,0f);
Redcube3.transform.Translate(0f,1f * Time.deltaTime,0f);
Redcube4.transform.Translate(0f,1f * Time.deltaTime,0f);
Redcube5.transform.Translate(0f,1f * Time.deltaTime,0f);
}
// For Right Movement
if( Input.GetKey(KeyCode.D)) {
cube.transform.Translate(1f * Time.deltaTime,0f,0f);
// Others Move Up First
Redcube.transform.Translate(1f * Time.deltaTime,0f,0f);
Redcube1.transform.Translate(1f * Time.deltaTime,0f,0f);
Redcube2.transform.Translate(1f * Time.deltaTime,0f,0f);
Redcube3.transform.Translate(1f * Time.deltaTime,0f,0f);
Redcube4.transform.Translate(1f * Time.deltaTime,0f,0f);
Redcube5.transform.Translate(1f * Time.deltaTime,0f,0f);
}
}
function OnGUI() {
// Whole Screen
GUI.Box (Rect (0,0,100,50), "Top-left");
GUI.Box (Rect (Screen.width - 100,0,100,50), "Top-right");
GUI.Box (Rect (0,Screen.height - 50,100,50), "Bottom-left");
GUI.Box (Rect (Screen.width - 100,Screen.height - 50,100,50), "Bottom-right");
// Label
}
答案 0 :(得分:0)
如果您希望将文本对象直接附加到您的游戏对象,则可以使用
"object is not found"
我不确定MeshRenderer是否标记为textMesh的必需组件。如果不是,您还必须手动添加MeshRenderer。 稍微复杂一点,但更好的做法是创建仅包含文本的子对象并在父对象中公开它。像那样:
var text = gameObjectName.AddComponent<TextMesh>();
text.text = "Some text";
希望它有所帮助。