using UnityEngine; using System.Collections; using System; [Serializable] public class Plot2DChannel{ public bool sample = true; public bool draw = true; public Color color = Color.green; public float cellTime = 0.2f; public float cellValue = 0.2f; public OscilloscopeSampler sampler; public bool oscilloscopeMode; private float[] sampleValueBuffer; public float[] SampleValueBuffer { get { return sampleValueBuffer; } } private float[] sampleTimeBuffer; public float[] SampleTimeBuffer { get { return sampleTimeBuffer; } } private int sampleBufferPosition; public int SampleBufferPosition { get { return sampleBufferPosition; } set { sampleBufferPosition = value; } } public void ResizeBuffers(int samplesCount) { sampleTimeBuffer = new float[samplesCount]; sampleValueBuffer = new float[samplesCount]; } public void SetSample(int id, float x, float y) { sampleTimeBuffer[id] = x; sampleValueBuffer[id] = y; } public void Reset(int cellCount, int sampleRate){ float totalTime = cellCount * cellTime; int maxSampleCount = Mathf.CeilToInt (totalTime * sampleRate); sampleTimeBuffer = new float[maxSampleCount]; sampleValueBuffer = new float[maxSampleCount]; } public void Sample(){ float val = sampler == null ? 0 : sampler(); PushSample(Time.time, val); } public void PushSample(float x, float y) { sampleBufferPosition = (sampleBufferPosition + 1) % sampleValueBuffer.Length; sampleTimeBuffer[sampleBufferPosition] = x; sampleValueBuffer[sampleBufferPosition] = y; } } public delegate float OscilloscopeSampler(); [ExecuteInEditMode] public class Plot2D : MonoBehaviour { public Vector2 pos = new Vector2(10,10); public Vector2 size; public Plot2DChannel[] channels; private Vector3[] screenCorners; private const float cellSize = 40.0f; private const int hCellCountHalf = 6; private const int vCellCountHalf = 4; public int sampleRate = 50; private float lastSampleTime; public bool showInEditor = true; public int xMatrixOffset = 0; public int yMatrixOffset = 0; private GUIStyle richLabelStyle; void Start () { size = new Vector2 (hCellCountHalf * 2 * cellSize, vCellCountHalf * 2 * cellSize); screenCorners = new []{ new Vector3(pos.x, pos.y, 0.5f), new Vector3(pos.x + size.x, pos.y, 0.5f), new Vector3(pos.x + size.x, pos.y + size.y, 0.5f), new Vector3(pos.x, pos.y + size.y, 0.5f) }; } }