【发布时间】:2020-02-12 12:28:44
【问题描述】:
我一直在尝试计算 AR 会话中收集的所有 ar 点云的数量。
我尝试了以下代码,但 arPointCloud 一直抛出错误消息:
Object reference not set to an instance of an object
如果有人可以帮助我,我会很高兴。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
public class PointNumberCount : MonoBehaviour
{
ARSessionOrigin arSessionOrigin;
ARPointCloud arPointCloud;
int totalNumber;
List<Vector3> featurePoints = new List<Vector3>();
void Start()
{
arSessionOrigin = GetComponent<ARSessionOrigin>();
arPointCloud = arSessionOrigin.trackablesParent.GetComponentInChildren<ARPointCloud>();
}
void Update()
{
arPointCloud = arSessionOrigin.trackablesParent.GetComponentInChildren<ARPointCloud>();
featurePoints = new List<Vector3>(arPointCloud.positions);
totalNumber = featurePoints.Count;
}
}
}
【问题讨论】:
-
arSessionOrigin分配了吗?您确定此对象附加了ARSessionOrigin吗?进一步的trackablesParent分配是否正确?最后你确定ARPointCloud附加在一个活跃且启用的孩子身上吗?您可以尝试像arSessionOrigin.trackablesParent.GetComponentInChildren<ARPointCloud>(true);一样传递true以在搜索中包含非活动子对象。虽然你不应该在Update这样做.. 非常昂贵 -
其实我也不知道。我正在研究 ARF 基本示例。所以我的程序现在做的是简单的浊点可视化和图像跟踪。我没有改变 ARF 基本示例的结构。我是 Unity 和 ARF 的新手。
-
我太笨了!
arSessionOrigin未分配!你说的对!谢谢@derHugo
标签: c# unity3d augmented-reality