Drawing.DrawLine C# (CSharp) Метод

DrawLine() публичный статический Метод

public static DrawLine ( Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias = false ) : void
pointA Vector2
pointB Vector2
color Color
width float
antiAlias bool
Результат void
    public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias=false)
    {
        // Normally the static initializer does this, but to handle texture reinitialization
        // after editor play mode stops we need this check in the Editor.
        #if UNITY_EDITOR
        if (!lineTex)
        {
            Initialize();
        }
        #endif

        // Note that theta = atan2(dy, dx) is the angle we want to rotate by, but instead
        // of calculating the angle we just use the sine (dy/len) and cosine (dx/len).
        float dx = pointB.x - pointA.x;
        float dy = pointB.y - pointA.y;
        float len = Mathf.Sqrt(dx * dx + dy * dy);

        // Early out on tiny lines to avoid divide by zero.
        // Plus what's the point of drawing a line 1/1000th of a pixel long??
        if (len < 0.001f)
        {
            return;
        }

        // Pick texture and material (and tweak width) based on anti-alias setting.
        Texture2D tex;
        Material mat;
        if (antiAlias)
        {
            // Multiplying by three is fine for anti-aliasing width-1 lines, but make a wide "fringe"
            // for thicker lines, which may or may not be desirable.
            width = width * 3.0f;
            tex = aaLineTex;
            mat = blendMaterial;
        }
        else
        {
            tex = lineTex;
            mat = blitMaterial;
        }

        float wdx = width * dy / len;
        float wdy = width * dx / len;

        Matrix4x4 matrix = Matrix4x4.identity;
        matrix.m00 = dx;
        matrix.m01 = -wdx;
        matrix.m03 = pointA.x + 0.5f * wdx;
        matrix.m10 = dy;
        matrix.m11 = wdy;
        matrix.m13 = pointA.y - 0.5f * wdy;

        // Use GL matrix and Graphics.DrawTexture rather than GUI.matrix and GUI.DrawTexture,
        // for better performance. (Setting GUI.matrix is slow, and GUI.DrawTexture is just a
        // wrapper on Graphics.DrawTexture.)
        GL.PushMatrix();
        GL.MultMatrix(matrix);
        Graphics.DrawTexture(lineRect, tex, lineRect, 0, 0, 0, 0, color, mat);
        GL.PopMatrix();
    }

Usage Example

Пример #1
0
    private void OnGUI()
    {
        if (!graphMode)
        {
            var style = GUI.skin.GetStyle("label");
            style.fontSize = 24;

            if (m_joycons == null || m_joycons.Count <= 0)
            {
                GUILayout.Label("Joy-Con が接続されていません");
                return;
            }

            if (!m_joycons.Any(c => c.isLeft))
            {
                GUILayout.Label("Joy-Con (L) が接続されていません");
                return;
            }

            //if (!m_joycons.Any(c => !c.isLeft))
            //{
            //    GUILayout.Label("Joy-Con (R) が接続されていません");
            //    return;
            //}

            GUILayout.BeginHorizontal(GUILayout.Width(960));

            foreach (var joycon in m_joycons)
            {
                var isLeft = joycon.isLeft;
                var name   = isLeft ? "Joy-Con (L)" : "Joy-Con (R)";
                var key    = isLeft ? "Z キー" : "X キー";
                //var button = isLeft ? m_pressedButtonL : m_pressedButtonR;
                var button      = m_pressedButtonL;
                var stick       = joycon.GetStick();
                var gyro        = joycon.GetGyro();
                var accel       = joycon.GetAccel();
                var orientation = joycon.GetVector();


                GUILayout.BeginVertical(GUILayout.Width(480));
                GUILayout.Label(name);
                GUILayout.Label(key + ":振動");
                GUILayout.Label("押されているボタン:" + button);
                GUILayout.Label(string.Format("スティック:({0}, {1})", stick[0], stick[1]));
                GUILayout.Label("ジャイロ:" + gyro);
                GUILayout.Label("加速度:" + accel);
                GUILayout.Label("傾き:" + orientation);
                GUILayout.EndVertical();
            }

            GUILayout.EndHorizontal();
        }
        else
        {
            //加速度グラフ描画

            var area = GUILayoutUtility.GetRect(Screen.width, Screen.height);

            // Grid
            const int div = 10;
            for (int i = 0; i <= div; ++i)
            {
                var lineColor = (i == 0 || i == div) ? Color.white : Color.gray;
                var lineWidth = (i == 0 || i == div) ? 2f : 1f;
                var x         = (area.width / div) * i;
                var y         = (area.height / div) * i;
                Drawing.DrawLine(
                    new Vector2(area.x + x, area.y),
                    new Vector2(area.x + x, area.yMax), lineColor, lineWidth, true);
                Drawing.DrawLine(
                    new Vector2(area.x, area.y + y),
                    new Vector2(area.xMax, area.y + y), lineColor, lineWidth, true);
            }

            // Data
            if (accelLog.Count > 0)
            {
                var     max         = accelLog.Max();
                var     dx          = area.width / accelLog.Count;
                var     dy          = area.height / max;
                Vector2 previousPos = new Vector2(area.x, area.yMax);
                for (var i = 0; i < accelLog.Count; ++i)
                {
                    var x          = area.x + dx * i;
                    var y          = area.yMax - dy * accelLog[i];
                    var currentPos = new Vector2(x, y);
                    Drawing.DrawLine(previousPos, currentPos, Color.red, 3f, true);
                    previousPos = currentPos;
                }
            }
        }
    }
All Usage Examples Of Drawing::DrawLine