学习笔记4(寻找并设置某一物体上的文字)
寻找并设置某一物体上的文字
物体为people是一张精灵。
悬挂脚本:
―――――――――――――――――――――――――
(people)
public class fllowtext : MonoBehaviour {
publicstring MyEnterString="testtestest";
publicRect MyEnterTextRect;
publicRect mRect;
publicRect mRect2;
privateVector3 worldP1;
privateVector3 worldP2;//图片高度坐标。
publicfloat hx;
publicfloat hy;
publicfloat hyy;
publicfloat hxx;
privateVector2 position1;
privateVector2 position2;
voidStart () {
}
//Update is called once per frame
voidUpdate () {
}
voidOnGUI()
{
GUI.Label(newRect(10,20,100,200), MyEnterString);
//得到图片的大小
hy=gameObject.renderer.bounds.size.y;//高度。
hx=gameObject.renderer.bounds.size.x;//宽度。
worldP1= new Vector3 (transform.position.x , transform.position.y,transform.position.z);
worldP2= new Vector3 (transform.position.x+hx,transform.position.y-hy,transform.position.z);//对角位置
//两个位置转换为屏幕坐标。
position1= Camera.main.WorldToScreenPoint (worldP1);
position2= Camera.main.WorldToScreenPoint (worldP2);
//求出对角在屏幕上的宽度
hxx=position2.x-position1.x;
hyy=position2.y-position1.y;
mRect=newRect(position1.x+hxx+2,Screen.height-position1.y, 6, hyy);
GUIStylebb=new GUIStyle();
bb.normal.background= null; //这是设置背景填充的
bb.normal.textColor=newColor(1,0,0); //设置字体颜色的
bb.fontSize= (int)(-hyy);
GUI.Label(mRect,MyEnterString,bb);
}
publicvoid SetString(string x)
{
Debug.Log(x);
MyEnterString=x;
}
}
-----------------------------
悬挂在输入图上。查找上面的脚本,并传递文字。
public class iputxt : MonoBehaviour {
publicstring MyEnterString;
publicRect MyEnterTextRect;
publicRect mRect;
publicRect mRect2;
privateVector3 worldP1;
privateVector3 worldP2;//图片高度坐标。
publicfloat hx;
publicfloat hy;
publicfloat hyy;
publicfloat hxx;
privateVector2 position1;
privateVector2 position2;
voidStart () {
}
//Update is called once per frame
voidUpdate () {
}
voidOnGUI()
{
//得到图片的位置
hy=gameObject.renderer.bounds.size.y;
hx=gameObject.renderer.bounds.size.x;
worldP1= new Vector3 (transform.position.x ,transform.position.y,transform.position.z);
worldP2= new Vector3 (transform.position.x+hx, transform.position.y-hy,transform.position.z);
//位置转换为屏幕坐标。
position1= Camera.main.WorldToScreenPoint (worldP1);
position2= Camera.main.WorldToScreenPoint (worldP2);
hxx=position2.x-position1.x;
hyy=position2.y-position1.y;
mRect=newRect(position1.x+2,Screen.height-position1.y+2, hxx-6, -hyy-4);
GUIStylebb=new GUIStyle();
bb.normal.background= null; //这是设置背景填充的
//bb.normal.textColor=newColor(1,0,0); //设置字体颜色的
bb.fontSize= (int)(-hyy-6);
GUI.SetNextControlName("user");
//MyEnterString= GUI.TextField(mRect, MyEnterString);
MyEnterString= GUI.TextField(mRect, MyEnterString,bb);
///////////////////////////////////////////移除焦点,防止重复回车。
GUI.SetNextControlName("Text1");
//MyEnterString= GUI.TextField(mRect, MyEnterString);
GUI.Label(newRect(10,10,100,200), MyEnterString,bb);
///////////////////////////////////////////////////////
if(Event.current.keyCode == KeyCode.Return )//;; Event.current.type ==EventType.Used )
{
Debug.Log("设定焦点--");
if(GUI.GetNameOfFocusedControl() == "user" ) //获取这个user控件是否处于焦点状态
{
Debug.Log("------------------"+MyEnterString);
//当此控件有焦点的时候执行..
showinput();
//如果是回车就去执行些什么.. 这个Used是在Layout事件之后执行的...
//yieldreturn 1;
GUI.FocusControl("Text1");
}
}
//GUI.Label(mRect,"Hello World!",bb);
}
voidshowinput()
{
Debug.Log("准备传递焦点"+MyEnterString);
GameObjectgb1 = GameObject.Find("people");
gb1.GetComponent().SetString(MyEnterString);
gb1.GetComponent().MyEnterString=MyEnterString;
MyEnterString="";
//GUI.FocusControl("Text1");
}
}