为什么要在这个 Unity 脚本中设置这个转换?
Why is this transform set in this Unity script?
我一直在关注这个很棒的教程:
https://www.youtube.com/watch?v=uxrKE2VKvmc
并且在某些时候它会为每个目标执行此操作,它会创建一个游戏对象?
如果您查看此处的代码:
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;
如果我把它注释掉它仍然可以正常工作..
完整的 class 代码如下:
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!");
}
}
}
target.gameObject.transform.parent = transform;
该代码意味着目标游戏对象将是 TargetManager 脚本附加到的任何游戏对象的子对象。
正如 derHugo 在评论中指出的那样,它是多余的,可以写成
target.transform.parent = transform;
我一直在关注这个很棒的教程:
https://www.youtube.com/watch?v=uxrKE2VKvmc
并且在某些时候它会为每个目标执行此操作,它会创建一个游戏对象?
如果您查看此处的代码:
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;
如果我把它注释掉它仍然可以正常工作..
完整的 class 代码如下:
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!");
}
}
}
target.gameObject.transform.parent = transform;
该代码意味着目标游戏对象将是 TargetManager 脚本附加到的任何游戏对象的子对象。
正如 derHugo 在评论中指出的那样,它是多余的,可以写成
target.transform.parent = transform;