简体   繁体   English

如何将类属性添加到列表?

[英]How to add Class properties to List?

In MainClass I have: 在MainClass中,我有:

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

In TransformData class I have: 在TransformData类中,我有:

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. 回到MainClass,我想将TransformData中的信息添加到变量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 我看到了其他有关此的StackOverflow问题,但我无法正确解决

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. 如果您想在清除时添加参数,则向您的TransformData类添加一个构造函数。

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: 您在Stack Overflow上看到的与所讨论的行类似的内容可能是:

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. 请注意,不能在方法外部使用Add


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. 如果 somePositionsomeRotation也是例如您从Inspector获得的值。 You can't use transform.position etc outside of a method. 您不能在方法外部使用transform.position等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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