下面是代码,目的是点击按钮之后再开始画线,可是画的线会被其他UI给中的挡住,我这里该如何修改?请各位看一下,麻烦了
using UnityEngine;
using UnityEngine.UI;
public class TestLine : MonoBehaviour
{
public static Material lineMaterial;
public Material tmpMaterial;
Button tmp;
bool isPaint = false;
private void Awake()
{
tmp = GetComponent<Button>();
tmp.onClick.AddListener(TmpOnClick);
}
private void TmpOnClick()
{
isPaint = !isPaint;
}
static void CreateLineMaterial()
{
if (!lineMaterial)
{
// Unity has a built-in shader that is useful for drawing
// simple colored things.
Shader shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// Turn on alpha blending
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// Turn backface culling off
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// Turn off depth writes
lineMaterial.SetInt("_ZWrite", 10);
}
}
public void OnRenderObject()
{
if (isPaint)
{
CreateLineMaterial();
//第几个pass渲染
lineMaterial.SetPass(0);
//矩阵压栈
GL.PushMatrix();
//将顶点从local坐标系转换成世界坐标系
GL.MultMatrix(transform.localToWorldMatrix);
//将透视投影改成正交投影
GL.LoadOrtho();
//开始画线
GL.Begin(GL.LINES);
DrawLines();
//结束画线
GL.End();
//矩阵出栈
GL.PopMatrix();
}
}
public void DrawLines()
{
GL.Color(Color.red);
GL.Vertex(Camera.main.ScreenToViewportPoint(transform.position));
GL.Vertex(Camera.main.ScreenToViewportPoint(transform.GetChild(0).position));
//GL.Vertex(Camera.main.ScreenToViewportPoint(transform.position));
Vector3 tmp= Camera.main.ScreenToViewportPoint(transform.position);
Vector3 result = Camera.main.ScreenToViewportPoint(transform.GetChild(0).position);
Vector3 tmp1 = new Vector3(tmp.x , tmp.y+0.01f, 0);
Vector3 result1= new Vector3(result.x , result.y+0.01f, 0);
GL.Vertex(tmp1);
GL.Vertex(result1);
//Vector3 tmp2= new Vector3(tmp.x -0.1f, tmp.y, 0);
//Vector3 result2= new Vector3(result.x - 0.1f, result.y, 0);
//GL.Vertex(tmp2);
//GL.Vertex(result2);
}
}