using UnityEngine; using System.Collections; public class DrawMesh : MonoBehaviour { GameObject gameobj; //绘制mesh的物体,如果是多个可以动态的生成 public Camera cameraObj; //NGUI中的摄像机 public GameObject lookObject;//被指向的物体 // Use this for initialization float x = 0;//屏幕的宽 float y = 0;//屏蔽的高 Color color; void Start () { color = Color.white; x = Screen.currentResolution.width; //屏幕分辨率 y = Screen.currentResolution.height; //这个地方我是动态获得的一个空的物体对象。用来创建三角形指示面片的。 gameobj = (GameObject)Instantiate(Resources.Load("GameObject")); } // Update is called once per frame void Update () { GetPositon(); } //绘制三角面片 void DrawTrianglesEX(GameObject triangles, Vector3 port1, Vector3 port2, Vector3 port3, Color color) { color.a = 0.8f;//面片的透明度 if (triangles.GetComponent<MeshFilter>() == null) { triangles.AddComponent<MeshFilter>(); //添加MeshFilter triangles.AddComponent<MeshRenderer>();//添加MeshRenderer //获得着色器 triangles.renderer.material.shader = Shader.Find("Transparent/Diffuse"); } //Unity3D:www.unitymanual.com //实时改变颜色 triangles.renderer.material.color = color; Mesh mesh = triangles.GetComponent<MeshFilter>().mesh; mesh.Clear(); //这个是主要的点的转换,在视窗位置转换成时间坐标位置。 Vector3 v1 = triangles.transform.InverseTransformPoint(port1); Vector3 v2 = triangles.transform.InverseTransformPoint(Camera.main.ViewportToWorldPoint(port2)); Vector3 v3 = triangles.transform.InverseTransformPoint(Camera.main.ViewportToWorldPoint(port3)); mesh.vertices = new Vector3[] { v1, v2, v3 }; mesh.triangles = new int[] { 0, 1, 2 }; } //获得三个点的位置,并且转化为屏幕坐标 void GetPositon() { Vector3 po = cameraObj.WorldToScreenPoint(transform.position); //在这个地方是生算的,这边完全可以算出面板的宽与高。注意的是2D摄相机与主摄像机的关系。建议不要去生算。 Vector3 po1 = new Vector3(po.x / x+0.075f, po.y / y, 1f); Vector3 po2 = po1 - new Vector3(0, 0.1f, 0); Vector3 v = lookObject.renderer.bounds.size; Vector3 equPosScreen = lookObject.transform.position+ new Vector3(0, v.y, 0); Vector3 po3 = equPosScreen; DrawTrianglesEX(gameobj, po3, po2, po1, color); } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。