What’s the best/fastest way to draw graph of a function or a set of data inside unity? Like, representing a players speed over time.
I was thinking about using a Mesh for this. But what’s my alternatives and how would you do it?
You could use a mesh, or the line renderer to draw your chart line in 3D space, or drawing routines (Texture2D, see SetPixel) to draw directly into a texture used on a plane or as part of your GUI display. Each will have advantages and options so you’ll have to see what suits your situation best (you haven’t explained quite exactly how/where you want that data to be shown).
Ah, I didn’t think about Texture2D with SetPixel. Got to try that out as well.
What I want to do is to have a real time chart line which represents the players speed over time.
Having it as a part of the GUI would be preferred.
Edit:
SetPixel was really cool to play around with. This will be perfect for what I’m making.
Here’s a little fun script to draw a chart based on your mouse y position. Just attach it to a Cube or something and watch it draw.
using UnityEngine;
using System.Collections;
public class ChartTexture : MonoBehaviour
{
private Texture2D texture = new Texture2D(128, 128);
// Use this for initialization
void Start ()
{
renderer.material.mainTexture = texture;
}
// Update is called once per frame
void Update ()
{
//Copy current texture and move one step to the left
for (int y = 0; y < texture.height; ++y)
{
for (int x = 0; x < texture.width; ++x)
{
if (x != texture.width - 1)
texture.SetPixel(x, y, texture.GetPixel(x + 1, y));
else
texture.SetPixel(x, y, Color.black);
}
}
texture.SetPixel(texture.width-1, (int)Input.mousePosition.y, Color.red);
texture.Apply();
}
}