在启动应用程序时,我希望保存数据集的默认数量(一个唯一的键[timestamp as int],以及4个不同的传感器值)。从那时起,在新的数据集上,每10秒,将保存在数据库中。这些数据集应添加到存储中,例如,元组列表。最后,我使用存储的数据绘制图表。
需要存储的不同值如下:
intfloatfloatintboolean不幸的是,Unity似乎不支持带有4个值的元组。因此,此代码不起作用:
List<Tuple<int, float, float, int, boolean>> list = new List<Tuple<int, float, float, int, boolean>>();它总是弹出一条消息,即类型Tuple需要2个类型参数。Dictionary的pro是关键值(我的时间戳),但另一方面,我也只能存储两个值(包括键)。
元组的List将是完美的,因为如果用户选择“显示最后10个值”,我们将添加一个新的数据集,并删除最老的数据集。
还有别的办法吗?
发布于 2017-05-05 09:55:12
不要用4-5值的Tuple那样工作.很难跟踪每个Item的含义,并导致错误。创建一个自定义类:
public class SensorData
{
public int TimeStamp { get; set; }
public float Humidity { get; set; }
public int Temp { get; set; }
public int Light { get; set; }
public bool Button { get; set; }
}那么,如果您想要一个列表/字典:
List<SensorData> list = new List<SensorData>();
Dictionary<int, SensorData> mapping = new Dictionary<int, SensorData>();如果您拥有的数据最初位于列表中,则可以使用.ToDictionary创建字典:
list.ToDictionary(key => key.TimeStamp);
// Note that this will faild if you have sevetal items with the same timestamp
// If not unique then look at `.GroupBy` or `LookUp`发布于 2017-05-05 09:55:58
我使用带有自定义类的Dictionary来存储传感器结果和唯一的时间戳
public class SensorResult
{
public float Humidity { get; set; }
public float Temp { get; set; }
public int Light { get; set; }
public bool Button { get; set; }
}
Dictionary<int, SensorResult> items = new Dictionary<int, SensorResult>();发布于 2017-05-05 09:58:02
您可以使用类来保存键和值,并创建该类的列表,如下所示-
public class MyComponent : MonoBehaviour
{
// Declare your serializable data
[System.Serializable]
public class Data {
public int a;
public float b;
public float c;
public int d;
public bool e;
}
// Create list of your class type
public List<Data> list = new List<Data>();
...
}https://stackoverflow.com/questions/43801845
复制相似问题