分享

编辑器窗口画线修改

 tiancaiwrk 2019-05-13

使用某个编辑器窗口进行画线的逻辑使用的是

GUI.DrawTexture(new Rect(pointA.x, pointA.y, 1, 1), lineTex);

的方式, 可是在起点不被编辑器窗口渲染的情况下整条线段都不被渲染, 很有问题, 

修改为GUI.Box的情况就可以改善

 public static System.Collections.Generic.Dictionary<Color, Texture2D> lineTextures = new System.Collections.Generic.Dictionary<Color, Texture2D>();

   public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width)

        {

            pointA = new Vector2(Mathf.RoundToInt(pointA.x), Mathf.RoundToInt(pointA.y));

            pointB = new Vector2(Mathf.RoundToInt(pointB.x), Mathf.RoundToInt(pointB.y));

            var dis = Vector2.Distance(pointA, pointB);

            if(dis < 0.1f)

            {

                return;

            }

            width = Mathf.Max(1.0f, width);

            // Save the current GUI matrix, since we're going to make changes to it.

            Matrix4x4 matrix = GUI.matrix;

            var tex = lineTextures.TryGetNullableValue(color);

            // Generate a single pixel texture if it doesn't exist

            if(false == tex)

            {

                tex = new Texture2D(1, 1);

                tex.SetPixel(0, 0, color);

                tex.Apply();

                lineTextures[color] = tex;

            }

            // Store current GUI color, so we can switch it back later,

            // and set the GUI color to the color parameter

            Color savedColor = GUI.color;

            GUI.color = color;

            // Determine the angle of the line.

            float angle = Vector3.Angle(pointB - pointA, Vector2.right);

            // Vector3.Angle always returns a positive number.

            // If pointB is above pointA, then angle needs to be negative.

            if(pointA.y > pointB.y)

            { angle = -angle; }

            // Use ScaleAroundPivot to adjust the size of the line.

            // We could do this when we draw the texture, but by scaling it here we can use

            //  non-integer values for the width and length (such as sub 1 pixel widths).

            // Note that the pivot point is at +.5 from pointA.y, this is so that the width of the line

            //  is centered on the origin at pointA.

            //GUIUtility.ScaleAroundPivot(new Vector2(dis, width), new Vector2(pointA.x, pointA.y));

            // Set the rotation for the line.

            //  The angle was calculated with pointA as the origin.

            GUIUtility.RotateAroundPivot(angle, pointA);

            // Finally, draw the actual line.

            // We're really only drawing a 1x1 texture from pointA.

            // The matrix operations done with ScaleAroundPivot and RotateAroundPivot will make this

            //  render with the proper width, length, and angle.

            //GUI.DrawTexture(new Rect(pointA.x, pointA.y, 1, 1), lineTex);

            var lastBG = GUI.skin.box.normal.background;

            GUI.skin.box.normal.background = tex;

            GUI.Box(new Rect(pointA.x, pointA.y, dis, width), tex);

            GUI.skin.box.normal.background = lastBG;

            // We're done.  Restore the GUI matrix and GUI color to whatever they were before.

            GUI.matrix = matrix;

            GUI.color = savedColor;

        }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多