我正在开发 UnityARWorldMap 以创建统一的持久性。因此,我将模型放置在现实世界中,保存它们,然后加载它们。当我单击 WorldMapManager.cs 中的 Load() 时,UnityARhitTestExample 中的更新函数停止更新。给出了 WorldMapManager 脚本以下。
public void Save()
{
//Saving the data into Application.persistentDataPath
}
public void Load()
{
//Loads the data from Application.persistentDataPath
Debug.LogFormat("Loading ARWorldMap {0}", path);
var worldMap = ARWorldMap.Load(path);
if (worldMap != null)
{
m_LoadedMap = worldMap;
Debug.LogFormat("Map loaded. Center: {0} Extent: {1}", worldMap.center, worldMap.extent);
UnityARSessionNativeInterface.ARSessionShouldAttemptRelocalization = true;
var config = m_ARCameraManager.sessionConfiguration;
config.worldMap = worldMap;
UnityARSessionRunOption runOption = UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors | UnityARSessionRunOption.ARSessionRunOptionResetTracking;
Debug.Log("Restarting session with worldMap");
session.RunWithConfigAndOptions(config, runOption);
}
}
在UnityARHitTestExample.cs的更新函数中,我给了一个Print语句。当我点击Load按钮时,它会停止打印。这是为什么呢?
public class UnityARHitTestExample : MonoBehaviour
{
public static UnityARHitTestExample HittestInstance;
public GameObject[] Allmodels;
public Transform m_HitTransform;
public float maxRayDistance = 30.0f;
public LayerMask collisionLayer = 1 << 10; //ARKitPlane layer
bool placed = false;
public List<DataHandlers> Modelinfo = new List<DataHandlers>();
List<GameObject> ContainerObject = new List<GameObject>();
GameObject currentModel;
string json;
int indexchoice;
GameObject temp;
List<int> indexarray = new List<int>() ;
float rotateValue = 300f;
//Original Camera Parent position
public Transform CameraParent;
private Vector3 Campos;
public Text Positions;
bool HitTestWithResultType (ARPoint point, ARHitTestResultType resultTypes)
{
List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface ().HitTest (point, resultTypes);
if (hitResults.Count > 0 ) {
foreach (var hitResult in hitResults) {
if (currentModel != null)
{
Debug.Log("Got hit!");
temp = Instantiate(currentModel);
ContainerObject.Add(temp);
placed = true;
temp.transform.position=UnityARMatrixOps.GetPosition(hitResult.worldTransform);
temp.transform.rotation=UnityARMatrixOps.GetRotation(hitResult.worldTransform);
Debug.Log(string.Format("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z));
return true;
}
}
}
return false;
}
private void Start()
{
HittestInstance = this;
}
private void LateUpdate()
{
Camera.main.transform.SetParent(CameraParent.transform);
}
private void Update()
{
Debug.Log("Changed ");
#if UNITY_EDITOR //we will only use this script on the editor side, though there is nothing that would prevent it from working on device
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//we'll try to hit one of the plane collider gameobjects that were generated by the plugin
//effectively similar to calling HitTest with ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent
if (Physics.Raycast(ray, out hit, maxRayDistance, collisionLayer))
{
//we're going to get the position from the contact point
m_HitTransform.position = hit.point;
Debug.Log(string.Format("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z));
//and the rotation from the transform of the plane collider
m_HitTransform.rotation = hit.transform.rotation;
}
}
#else
if (Input.touchCount == 1 && m_HitTransform != null && !EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId) )
{
var touch = Input.GetTouch(0);
//if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
if (touch.phase == TouchPhase.Began && isLocked==false )
{
var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
ARPoint point = new ARPoint {
x = screenPosition.x,
y = screenPosition.y
};
//User Initial choice was for existing plane & Horizontal plane
// prioritize reults types
ARHitTestResultType[] resultTypes = {
ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingGeometry,
//ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
// if you want to use infinite planes use this:
// ARHitTestResultType.ARHitTestResultTypeExistingPlane,
// ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane,
//ARHitTestResultType.ARHitTestResultTypeEstimatedVerticalPlane,
//ARHitTestResultType.ARHitTestResultTypeFeaturePoint
};
foreach (ARHitTestResultType resultType in resultTypes)
{
if (HitTestWithResultType (point, resultType))
{
return;
}
}
}
}
#endif
}