崆、白鱼镇 2019-06-05 17:33 采纳率: 0%
浏览 1251

关于Unity GL画图被UI遮挡的问题

图片说明
下面是代码,目的是点击按钮之后再开始画线,可是画的线会被其他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);


    }

}

  • 写回答

1条回答

  • hh93424 2019-06-24 21:31
    关注

    看别人粒子效果被UI遮挡联想出来的解决方法,实践验证后可以解决问题,canvas修改Render Mode 为Screen Space - Camera,下面的Render Camera使用主相机,并且保证Sorting Layer 为 Default、 Order in Sorting 值为 0

    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝