using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;
public class inputtext : MonoBehaviour {
public string MyEnterString="testtestest";
public Rect MyEnterTextRect;
public Rect mRect;
public Rect mRect2;
//private Camera cmaera;
// Use this for initialization
private Vector3 worldP1;
private Vector3 worldP2;//世界图片高度坐标。
private float hx;
private float hy;
private float hyy;//GUI宽高度
private float hxx;
private Vector2 position1;//gui坐标
private Vector2 position2;
int fonth;
public float scalex=1;
public int offsetx=0;
public int offsety;
int TimeInterval; //时间间隔。
int TimeI= System.DateTime.Now.Second;//当前时间。
public bool sumf= false;
public bool textinte = true ;
public bool lineout = false;
public Color xcolor=Color.black;
//public GameObject x1;
//public GameObject x2;
GUIStyle bb=new GUIStyle();
void Start () {
// xcolor=xcolor;
//GUI.Label(new Rect(10,20,100,200), MyEnterString);
//得到图片的大小
//hy=gameObject.transform.GetComponent<Renderer>.bounds.size.y; //高度。
hy=gameObject.transform.GetComponent<Renderer>().bounds.size.y;
hx=gameObject.transform.GetComponent<Renderer>().bounds.size.x;//宽度。
worldP1 = new Vector3 (transform.position.x-(hx/2), transform.position.y+(hy/2),transform.position.z);
worldP2 = new Vector3 (transform.position.x+(hx/2), transform.position.y-(hy/2),transform.position.z);//右上角位置
//x1.transform.position=worldP1;
//x2.transform.position=worldP2;
//两个位置转换为屏幕坐标。
position1 = Camera.main.WorldToScreenPoint (worldP1);
position2 = Camera.main.WorldToScreenPoint (worldP2);
//求出对角在屏幕上的宽度
hxx =position2.x-position1.x;
hyy= position1.y-position2.y;
// Debug.Log ("--"+hxx+"__"+hyy+"----------"+position1+"--"+position2);
//mRect=new Rect(position1.x+2,Screen.height-position1.y, 100, -hyy*2);
//gui坐标和屏幕坐标相反。所以再改。
//mRect=new Rect(position1.x+2+offsetx,Screen.height-position1.y+offsety, hxx+offsetx*2, hyy);
// mRect=new Rect(position1.x+offsetx,Screen.height-position1.y+offsety, hxx-offsetx, hyy);
//int testxx=(int)(Screen.height-position1.y+offsety);
// Debug.Log(testxx);
//mRect=new Rect(10,20,100,200);
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
mRect=new Rect(position1.x+2+offsetx,Screen.height-position1.y+offsety, hxx-offsetx, hyy);
int ra=(int)xcolor.r;
int rg=(int)xcolor.g;
int rb=(int)xcolor.b;
bb.normal.background = null; //这是设置背景填充的
// Debug.Log("color"+ra+rg+rb);
bb.normal.textColor=new Color(ra,rg,rb); //设置字体颜色的
// bb.normal.textColor = Color.yellow;
//bb.normal.textColor=new color((int)xcolor.r,(int)xcolor.g,(int)xcolor.b);
fonth=(int)Mathf.Ceil( hyy*scalex);
Debug.Log(fonth);
bb.fontSize = fonth;
//bb.fontSize = (int)Mathf.Ceil((hyy)*scalex);
if (lineout == false)
{
bb.wordWrap=true;
}
else
{
bb.wordWrap=false;
}
//GUI.TextArea(mRect, MyEnterString,20);
GUI.Label(mRect, MyEnterString,bb);
if (textinte)
{
GUI.SetNextControlName("user");
MyEnterString = GUI.TextField(mRect, MyEnterString,5,bb);
//if (sumf) MyEnterString= Regex.Replace(MyEnterString, "[^0-9]", "");
if (sumf) MyEnterString= Regex.Replace(MyEnterString, "[^a-z,A-Z]", "");
}
}
public void SetString(string x)
{
TimeInterval= Mathf.Abs( System.DateTime.Now.Second-TimeI) ;
TimeI= System.DateTime.Now.Second;
//Debug.Log ("子程序被调用/time:"+TimeInterval+"--"+TimeI);
if ((TimeInterval >0)&&(x!=""))
{
MyEnterString=x;
}
}
}