Вы должны дать ему конструктор, подобный
// In order to be able to actually save that list this class has to be
// Serializable!
// Another nice side effect is that from now you can also adjust the values
// from the Inspector of the MonoBehaviour using such an instance or list
[Serializable]
public class TransformData
{
public Vector3 position;
public Quaternion rotation;
public Vector3 scale;
// Serialization always needs a default constructor
// doesn't have to do anything but can
public TransformData()
{
// scale should be 1,1,1 by default
// The other two can keep their default values
scale = Vector3.one;
}
public TransformData(Vector3 pos, Quaternion rot, Vector3 scal)
{
position = pos;
rotation = rot;
scale = scal;
}
}
, а затем сделать, например,
// this is a vector not a float!
public Vector3 scaleVector = Vector3.one;
private void Start()
{
// I guess you wanted to add the data for some transform component
matrices1.Add(new TransformData(transform.position, transform.rotation, Vector3.one));
}
Обратите внимание, что Add
нельзя использовать вне метода.
Если вы предпочитаете напрямую инициализировать свой список с добавлением некоторых элементов, вы можете сделать следующее:
public List<TransformData> matrices1 = new List<TransformData>()
{
new TransformData(somePosition, someRotation, scaleVector);
};
, если somePosition
и someRotation
, например, также являются значениями, которые выполучить от инспектора.Вы не можете использовать transform.position
и т. Д. Вне метода.