为什么在此Unity脚本中设置此转换?

时间:2018-12-30 12:32:56

标签: unity3d augmented-reality vuforia

我一直在关注这个很棒的教程:

https://www.youtube.com/watch?v=uxrKE2VKvmc

并在某些时候为每个目标执行此操作,它会创建一个gameObject?

如果您在此处查看代码:

private void SetupTargets(List<TrackableBehaviour> allTargets)
    {
        Debug.Log("Listing all Targets names:");

        foreach (TrackableBehaviour target in allTargets)
        {
            Debug.Log("Target's name:" + target.TrackableName);

            target.gameObject.transform.parent = transform;
            target.gameObject.name = target.TrackableName;

            target.gameObject.AddComponent<PlaneManager>();

            Debug.Log(target.TrackableName + " created!");
        }
    }

为什么这行代码?

target.gameObject.transform.parent = transform;

如果我将其注释掉,它仍然可以正常工作。

完整的课程代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
using System.Linq;

public class TargetManager : MonoBehaviour
{

    public String mStartingMarkerDatabaseName = "";

    private List<TrackableBehaviour> mAllTargets = new List<TrackableBehaviour>();

    private void Awake()
    {
        VuforiaARController.Instance.RegisterVuforiaStartedCallback(onVuforiaStarted);
    }

    private void onDestroy()
    {
        VuforiaARController.Instance.UnregisterVuforiaStartedCallback(onVuforiaStarted);
    }

    private void onVuforiaStarted()
    {
        LoadDatabase(mStartingMarkerDatabaseName);
        mAllTargets = GetTargets();
        SetupTargets(mAllTargets);
    }

    private void LoadDatabase(string name)
    {
        ObjectTracker objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
        objectTracker.Stop();

        if (DataSet.Exists(name))
        {
            DataSet dataSet = objectTracker.CreateDataSet();
            dataSet.Load(name);
            objectTracker.ActivateDataSet(dataSet);
        }

        objectTracker.Start();
    }

    private List<TrackableBehaviour> GetTargets()
    {
        List<TrackableBehaviour> allTrackables = new List<TrackableBehaviour>();

        allTrackables = TrackerManager.Instance.GetStateManager().GetTrackableBehaviours().ToList();

        return allTrackables;
    }

    private void SetupTargets(List<TrackableBehaviour> allTargets)
    {
        Debug.Log("Listing all Targets names:");

        foreach (TrackableBehaviour target in allTargets)
        {
            Debug.Log("Target's name:" + target.TrackableName);

            target.gameObject.transform.parent = transform; 
            target.gameObject.name = target.TrackableName;

            target.gameObject.AddComponent<PlaneManager>();

            Debug.Log(target.TrackableName + " created!");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

target.gameObject.transform.parent = transform;

该代码意味着目标gameObject将是TargetManager脚本所附加的任何gameObject的子代。

正如derHugo在评论中指出的那样,它是多余的,可以写成

target.transform.parent = transform;