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

时间:2019-06-19 06:17:42

标签: c# unity3d

在MainClass中,我有:

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

在TransformData类中,我有:

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

回到MainClass,我想将TransformData中的信息添加到变量matrices1中。您如何正确执行此操作?我有-

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

这给了我错误。我看到了其他与此有关的StackOverflow问题,但我无法正确处理

3 个答案:

答案 0 :(得分:2)

您在这里,先生:

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

如果您想在清除时添加参数,请将构造函数添加到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;
    }
}

然后您可以像您在代码中所做的那样进行操作。

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

答案 1 :(得分:1)

您需要向保存数据的对象添加变量或引用:

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

,然后将其添加到列表中:

matrices1.Add(myObj);

如果您已经在其他地方创建了该对象,则只需添加保存它的变量。

您在堆栈溢出中看到的与所讨论的行类似的内容可能是:

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

正在创建新对象并将其添加到列表中。

答案 2 :(得分:1)

您应该给它一个类似的构造函数

string.h

然后例如

 ` $("#calendar-modal").html("");
                    var scheduleClick=info.event.start.toISOString();
                    var calendarEl = document.getElementById('calendar-modal');
                    var calendar = new FullCalendar.Calendar(calendarEl, {
                            plugins: [ 'timeGrid' ],
                            header: {
                                left: false,
                                center: 'title',
                                right: false
                            },
                            defaultView:'timeGrid',
                            defaultDate:scheduleClick,
                            displayEventTime:false,
                            allDaySlot: false,
                            slotEventOverlap:false,
                            minTime:"07:00",
                            maxTime:"24:00",
                            eventSources:[
                               {
                               url:'something.php',
                               method:'POST',
                               color: '#87a900',  
                               textColor: 'black'
                               }
                               ],
                    });  
                    calendar.refetchEvents(); // I've tried using refetch events
                    calendar.render();`

请注意,// 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));
}

如果 Addpublic List<TransformData> matrices1 = new List<TransformData>() { new TransformData(somePosition, someRotation, scaleVector); }; 例如还可以从检查器获得值。您不能在方法外部使用somePosition等。