soundvision/UnityProject/Assets/Editor/LevelMeter.cs

51 lines
1.4 KiB
C#
Raw Normal View History

2019-09-28 19:21:58 +00:00
using UnityEngine;
using UnityEditor;
namespace cylvester
{
public class LevelMeter
{
private const int TextureWidth = 1;
private const int TextureHeight = 100;
private readonly Texture2D meterImageTexture_;
private readonly int index_;
private readonly string label_;
private readonly IPdArray pdArray_;
2019-09-28 19:21:58 +00:00
public LevelMeter(int index, IPdArray pdArray)
2019-09-28 19:21:58 +00:00
{
index_ = index;
label_ = (index_ + 1).ToString();
pdArray_ = pdArray;
meterImageTexture_ = new Texture2D(TextureWidth, TextureHeight);
2019-09-28 19:21:58 +00:00
}
public void Render()
{
UpdateTexture();
var rect = EditorGUILayout.BeginVertical();
var style = new GUIStyle(GUI.skin.label)
{
alignment = TextAnchor.UpperCenter,
fixedHeight = 120
};
GUILayout.Label(label_, style);
rect.y += 20;
rect.height -= 20;
EditorGUI.DrawPreviewTexture(rect, meterImageTexture_);
EditorGUILayout.EndVertical();
}
private void UpdateTexture()
{
var level = pdArray_.Data[index_];
for (var i = 0; i < 100; ++i)
2019-09-28 19:21:58 +00:00
{
meterImageTexture_.SetPixel(0, i, i < level ? Color.green : Color.black);
2019-09-28 19:21:58 +00:00
}
meterImageTexture_.Apply();
}
}
}