在Unity中的运行时创建新列表

我尝试为场景中的每个游戏对象创建一个列表,该列表存储对象在一定时间内的位置和旋转。我希望这在运行时发生。因为如果我在运行时创建一个新对象,那么该对象也应该有一个新的单独列表。目的是使用这些值重置位置和随时间的旋转。喜欢倒带的时间。 问题是我不知道如何在运行时声明和初始化列表。我试图将所有值存储在一个列表中,但这并没有得到预期的结果。

我将不胜感激任何类型的小费或线索。谢谢。

一些代码可以与一个列表中的所有值共享我的尝试:

协程,用于将所有位置和旋转记录在一个列表中

IEnumerator Record()
    {
        for (; ; )
        {
            Debug.Log("Recording");

            /*Limit the maximum of values stored; that limits the seconds we can go back in time*/
            if (pointsInTime.Count > Mathf.Round(rewindableObjects.Count*(1f/(Time.fixedDeltaTime/Time.timeScale)) * maxrewindTime))
            {

                for (int i = 1; i < rewindableObjects.Count; i++)
                {
                    pointsInTime.RemoveLast();
                }
            }

            foreach (GameObject go in rewindableObjects)
            {
                pointsInTime.AddFirst(new PointInTime(go.name,go.transform.position,go.transform.rotation,go.getcomponent<Rigidbody>().velocity));

            }
            yield return new WaitForSecondsRealtime(0.02f/Time.timeScale);
        }

倒带功能可重置所有位置和旋转角度

/*rewind*/
    void rewind()
    {
        foreach(GameObject go in rewindableObjects)
        {
            if(pointsInTime.Count > 0)
            {
                PointInTime pointInTime = pointsInTime.First.Value;
                if (pointInTime.goName == go.name) //this is way to slow. Solves the problem with objects swapping positions,but looks like a diashow. Needed: Way to differantiate all values belonging to one object in a seperate list or sth. 
                {
                    go.transform.position = pointInTime.position;
                    go.transform.rotation = pointInTime.rotation;
                    go.getcomponent<Rigidbody>().velocity = pointInTime.velocity;   //not working because velocity is applyied,while the rigidbody is still kinematic!!!
                    pointsInTime.RemoveFirst();
                }

            }else Stoprewind();
        }
    }
xiaoleikesa 回答:在Unity中的运行时创建新列表

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3149055.html

大家都在问