soundvision/UnityProject/Assets/Editor/LevelMeter.cs

54 lines
1.5 KiB
C#
Raw Normal View History

2019-09-28 19:21:58 +00:00
using UnityEngine;
using UnityEditor;
namespace cylvester
{
2019-09-29 16:04:26 +00:00
interface ILevelMeter
{
2019-09-29 19:53:47 +00:00
void Render(IPdArray pdArray);
2019-09-29 16:04:26 +00:00
}
public class LevelMeter : ILevelMeter
2019-09-28 19:21:58 +00:00
{
private const int TextureWidth = 1;
private const int TextureHeight = 100;
private readonly Texture2D meterImageTexture_;
private readonly int index_;
private readonly string label_;
2019-09-28 19:21:58 +00:00
2019-09-29 19:53:47 +00:00
public LevelMeter(int index)
2019-09-28 19:21:58 +00:00
{
index_ = index;
label_ = (index_ + 1).ToString();
meterImageTexture_ = new Texture2D(TextureWidth, TextureHeight);
2019-09-28 19:21:58 +00:00
}
2019-09-29 19:53:47 +00:00
public void Render(IPdArray pdArray)
2019-09-28 19:21:58 +00:00
{
2019-09-29 19:53:47 +00:00
UpdateTexture(pdArray);
2019-09-28 19:21:58 +00:00
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();
}
2019-09-29 19:53:47 +00:00
private void UpdateTexture(IPdArray pdArray)
2019-09-28 19:21:58 +00:00
{
2019-09-29 19:53:47 +00:00
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();
}
}
}