简体   繁体   中英

How to add Class properties to List?

In MainClass I have:

public List<TransformData> matrices1 = new List<TransformData>();
public Vector3 scaleVector = 1f;

In TransformData class I have:

public class TransformData 
{
    public Vector3 position;
    public Quaternion rotation;
    public Vector3 scale;
}

Back in MainClass, I want to add the info from TransformData into the variable matrices1. How do you do this right? I have-

matrices1.Add(TransformData.(TransformData.position, TransformData.rotation, scaleVector));

That gives me errors. I saw other StackOverflow questions about this but i cant get it right

Here you go sir:

TransformData td = new TransformData();
matrices1.add(td);

If you would like to add parameters right at the declearation, add a constructor to your TransformData class.

public class TransformData 
{
    public Vector3 position;
    public Quaternion rotation;
    public Vector3 scale;

    public TransformData(){}

    public TransformData(Vector3 pos, Quaternion  rot, Vector3  sc)
    {
      position = pos;
      rotation = rot;
      scale = sc;
    }
}

and then you can do it like u did in you code.

matrices1.add(new Transformdata(somepos, somerotation, somevector));

You need to add variable or reference to object that holds the data:

TransformData myObj = new TransformData();
myObj.position = //populate it here
myObj.rotation = //populate it here
myObj.scale = //populate it here

and then add it to the list:

matrices1.Add(myObj);

If you have that object somewhere else already created, then just add the variable that holds it.

What you've seen on the Stack Overflow that looks similar to line posted in question is probably that:

matrices1.Add(new TransformData(){ 
     position = //something,
     rotation = //something,
     scale = //something
});

Which is creating new object as well as adding it to a list.

You should give it a constructor like

// 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;
    }
}

and then do eg

// 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));
}

Note that Add can not be used outside of a method.


If you rather want to directly initialize your list with some elements added you could go like

public List<TransformData> matrices1 = new List<TransformData>()
{
    new TransformData(somePosition, someRotation, scaleVector);
};

if somePosition and someRotation are eg also values you get from the Inspector. You can't use transform.position etc outside of a method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM