c# - 在 Unity 中创建图表

标签 c# unity3d graph

我正在开发一款智能手机游戏来衡量玩家的 react 时间。我想显示一个图表,显示他们每次点击的时间,以及一条显示平均值的线。

我想放置图表的图像:

enter image description here

这是否可以使用 Unity 实现,还是我应该使用其他软件来完成?

我的 react 时间变量存储在一个数组中。

最佳答案

所以,我对 necro-post 感到抱歉,但我正试图为一个项目完成这样的事情,并想出了这个(如下)来绘制一种示波器图。我以为我会发布最基本的知识。它在可移动的 GUI.Window 中绘制图形。目前没有“风吹草动”。它只是绘制了一堆随机值,但可以用作其他人的起点。

        using System.Collections.Generic;
        using UnityEngine;

        /// <summary>
        /// Draws a basic oscilloscope type graph in a GUI.Window()
        /// Michael Hutton May 2020
        /// This is just a basic 'as is' do as you wish...
        /// Let me know if you use it as I'd be interested if people find it useful.
        /// I'm going to keep experimenting wih the GL calls...eg GL.LINES etc 
        /// </summary>
        public class Graph : MonoBehaviour
        {

            Material mat;
            private Rect windowRect = new Rect(20, 20, 512, 256);

            // A list of random values to draw
            private List<float> values;

            // The list the drawing function uses...
            private List<float> drawValues = new List<float>();

            // List of Windows
            private bool showWindow0 = false;

            // Start is called before the first frame update
            void Start()
            {
                mat = new Material(Shader.Find("Hidden/Internal-Colored"));
                // Should check for material but I'll leave that to you..

                // Fill a list with ten random values
                values = new List<float>();
                for (int i = 0; i < 10; i++)
                {
                    values.Add(Random.value * 200);
                }
            }

            // Update is called once per frame
            void Update()
            {
                // Keep adding values
                values.Add(Random.value * 200);
            }

            private void OnGUI()
            {
                // Create a GUI.toggle to show graph window
                showWindow0 = GUI.Toggle(new Rect(10, 10, 100, 20), showWindow0, "Show Graph");

                if (showWindow0)
                {
                    // Set out drawValue list equal to the values list 
                    drawValues = values;
                    windowRect = GUI.Window(0, windowRect, DrawGraph, "");
                }

            }


            void DrawGraph(int windowID)
            {
                // Make Window Draggable
                GUI.DragWindow(new Rect(0, 0, 10000, 10000));

                // Draw the graph in the repaint cycle
                if (Event.current.type == EventType.Repaint)
                {
                    GL.PushMatrix();

                    GL.Clear(true, false, Color.black);
                    mat.SetPass(0);

                    // Draw a black back ground Quad 
                    GL.Begin(GL.QUADS);
                    GL.Color(Color.black);
                    GL.Vertex3(4, 4, 0);
                    GL.Vertex3(windowRect.width - 4, 4, 0);
                    GL.Vertex3(windowRect.width - 4, windowRect.height - 4, 0);
                    GL.Vertex3(4, windowRect.height - 4, 0);
                    GL.End();

                    // Draw the lines of the graph
                    GL.Begin(GL.LINES);
                    GL.Color(Color.green);

                    int valueIndex = drawValues.Count - 1;
                    for (int i = (int)windowRect.width - 4; i > 3; i--)
                    {
                        float y1 = 0;
                        float y2 = 0;
                        if (valueIndex > 0)
                        {
                            y2 = drawValues[valueIndex];
                            y1 = drawValues[valueIndex - 1];
                        }
                        GL.Vertex3(i, windowRect.height - 4 - y2, 0);
                        GL.Vertex3((i - 1), windowRect.height - 4 - y1, 0);
                        valueIndex -= 1;
                    }
                    GL.End();

                    GL.PopMatrix();
                }
            }
        }

我希望它有用(可能对 OP 没有用,但是...)

迈克尔

关于c# - 在 Unity 中创建图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37137110/

相关文章:

c# - 如何以编程方式将 wcf 绑定(bind)部分添加到 web.config

c# - 在复杂度最小的数组中找到第二大数

android - MPAndroidChart- 如何在图表表等网格内添加单元格?

java - 检查无向图中的奇环

python - 如何使用 cherrypy 动态生成图形

c# - 调查 XMLWriter 对象

c# - 当涉及到 MessageBox 时,String.Format() 的对齐方式表现不同

c# - Unity编辑器捕获 "hold mouse down and move"事件

c# - OpenCV圆圈检测C#实现

c# - 基于 2D 矢量的 Ai 运动