using UnityEngine;
using Unity.MLAgents.Sensors;
using Unity.MLAgents;
public class TestTextureSensor : ISensor
{
Texture2D m_Texture;
string m_Name;
int[] m_Shape;
SensorCompressionType m_CompressionType;
///
/// The compression type used by the sensor.
///
public SensorCompressionType CompressionType
{
get { return m_CompressionType; }
set { m_CompressionType = value; }
}
public TestTextureSensor(
Texture2D texture, string name, SensorCompressionType compressionType)
{
m_Texture = texture;
var width = texture.width;
var height = texture.height;
m_Name = name;
m_Shape = new[] { height, width, 3 };
m_CompressionType = compressionType;
}
///
public string GetName()
{
return m_Name;
}
///
public int[] GetObservationShape()
{
return m_Shape;
}
///
public byte[] GetCompressedObservation()
{
var compressed = m_Texture.EncodeToPNG();
return compressed;
}
///
public int Write(ObservationWriter writer)
{
var numWritten = writer.WriteTexture(m_Texture, false);
return numWritten;
}
///
public void Update() { }
///
public void Reset() { }
///
public SensorCompressionType GetCompressionType()
{
return m_CompressionType;
}
}