创建动态GameObject并根据JSON数据一起转换其位置

时间:2019-04-18 20:09:31

标签: unity3d position

考虑此数据集:

[
   {
     PartId: 0,
     Positions: [
       { x: 0, y: 0, z: 0, timeCode: 0.0000 },
       { x: 0, y: 0, z: 1, timeCode: 0.0025 },
       { x: 0, y: 0, z: 2, timeCode: 0.0050 },
       { x: 0, y: 0, z: 3, timeCode: 0.0075 },
       { x: 0, y: 0, z: 4, timeCode: 0.0100 },
       { x: 0, y: 0, z: 5, timeCode: 0.0125 },
       { x: 0, y: 0, z: 6, timeCode: 0.0150 },
       { x: 0, y: 0, z: 7, timeCode: 0.0175 },
       { x: 0, y: 0, z: 8, timeCode: 0.0200 },
     ]
   },
   {
     PartId: 1,
     Positions: [
       { x: 0, y: 0, z: 0, timeCode: 0.0000 },
       { x: 0, y: 0, z: 2, timeCode: 0.0025 },
       { x: 0, y: 0, z: 4, timeCode: 0.0050 },
       { x: 0, y: 0, z: 6, timeCode: 0.0075 },
       { x: 0, y: 0, z: 8, timeCode: 0.0100 },
       { x: 0, y: 0, z: 10, timeCode: 0.0125 },
       { x: 0, y: 0, z: 12, timeCode: 0.0150 },
       { x: 0, y: 0, z: 14, timeCode: 0.0175 },
       { x: 0, y: 0, z: 16, timeCode: 0.0200 },
     ]
   }
 ]

我能够加载和解析JSON数据,并且可以为每个PartId创建一个新的GameObject。我试图找出同时根据Position集合转换每个“零件”的最佳方法。

我在场景中有一个带有附属类的空GameObject。在Start方法中,我获取JSON数据,然后在循环中创建GameObject类,设置其初始位置,然后启动在同一类中定义的协程。

主类:

void Start() {
    // do json stuff...
    // ........
    // then
    // for each part...

    foreach(PartGroup pg in Parts) {
        // create a new GameObject from the Part class
        Part part = gameObject.AddComponent(typeof(Part)) as Part;
        // send this part data to the new GameObject
        part.PartGroup = pg;
        // set the initial position for the part
        part.Init();

        // start a IEnumerator Coroutine in the part class
        StartCoroutine(part.PlayFrom());
    }
}

零件类:

public void Init() {
    Joint = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    Joint.transform.localScale = new Vector3(jointSize, jointSize, jointSize);
    Joint.transform.position = new Vector3(PartGroup.Positions[0].X, PartGroup.Positions[0].Z, PartGroup.Positions[0].Y);
}
public IEnumerator PlayFrom(float time = 0f) {
    while (PlayBack(time)) {
        yield return null;

        time += Time.fixedDeltaTime;
    }
}
bool PlayBack(float time) {
    float sample = time / (Time.fixedDeltaTime / speed);
    int previousIndex = (int)(sample);
    int last = PartGroup.Positions.Count - 1;

    if (previousIndex < last) {
        int nextIndex = previousIndex + 1;
        float interpolation = sample - previousIndex;

        Joint.transform.position = Vector3.Lerp(
            new Vector3(PartGroup.Positions[previousIndex].X, PartGroup.Positions[previousIndex].Z, PartGroup.Positions[previousIndex].Y),
            new Vector3(PartGroup.Positions[nextIndex].X, PartGroup.Positions[nextIndex].Z, PartGroup.Positions[nextIndex].Y),
            interpolation);

        return true;
    }
    Joint.transform.position = new Vector3(PartGroup.Positions[last].X, PartGroup.Positions[last].Z, PartGroup.Positions[last].Y);

    return false;
}

这是我目前的设置方式。它的确起作用,但是它不是平稳的运动,有时似乎滞后或跳帧。这是完成此任务的最佳方法,还是有更好的方法(如FixedUpdate)?我已经在项目设置中设置了固定时间属性,以匹配数据timeCode。

对于此类最佳实践的任何帮助,将不胜感激!

1 个答案:

答案 0 :(得分:2)

您必须在{p>中使用Time.deltaTime

time += Time.deltaTime;

float sample = time / (Time.deltaTime / speed);

Coroutines与所有Update调用一起执行,因此使用fixedDeltaTime会破坏帧独立性。

或者可能使用WaitForFixedUpdateFixedUpdate一样执行它

while (PlayBack(time)) {
    yield return new WaitForFixedUpdate();

    time += Time.fixedDeltaTime;
}

foreach(PartGroup pg in Parts) 
{
    // create a new GameObject from the Part class
    Part part = gameObject.AddComponent(typeof(Part)) as Part;
    // send this part data to the new GameObject
    part.PartGroup = pg;
    // set the initial position for the part
    part.Init();

    // start a IEnumerator Coroutine in the part class
    StartCoroutine(part.PlayFrom());
}

似乎您要为列表中的每个元素都添加一个组件,并且都添加到一个GameObject中。.我不知道您是否打算这样做。 AddComponent不会使用该组件创建新的GameObject,而是将该组件附加到脚本所附加的同一gameObject上。

您可能会考虑使用new GameObject

Part part = new GameObject("new GameObject").AddComponent<Part>();
// make it a child of this gameObject?
part.SetParent(gameObject, false);

也是计算

float sample = time / (Time.fixedDeltaTime / speed);
int previousIndex = (int)(sample);

...

float interpolation = sample - previousIndex;

似乎有点奇怪..您确定它总是返回01之间的值吗?