在UnityhitTest示例中,我试图实例化一个对象并保存其位置和旋转度。场景局部化以后,在相同的位置和旋转度处将相同的对象带回来。问题在于实例化对象的位置和旋转度已经保存后来我正在调整对象的位置和旋转。我想更新位置和旋转而无需再次实例化对象。
[System.Serializable]
public class ShapeInfo
{
public float px;
public float py;
public float pz;
public float qx;
public float qy;
public float qz;
public float qw;
public int shapeType;
}
[System.Serializable]
public class ShapeList
{
public ShapeInfo[] shapes;
}
public class ShapeManager : MonoBehaviour {
public List<ShapeInfo> shapeInfoList = new List<ShapeInfo>();
public List<GameObject> shapeObjList = new List<GameObject>();
public GameObject arrow;
int shapeobjnum = 0;
int shapelistnum = 0;
bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes)
{
List<ARHitTestResult> hitResults = UnityARSessionNativeInterface.GetARSessionNativeInterface().HitTest(point, resultTypes);
if (hitResults.Count > 0)
{
foreach (var hitResult in hitResults)
{
Debug.Log("Got hit!");
Vector3 position = UnityARMatrixOps.GetPosition(hitResult.worldTransform);
Quaternion rotation = UnityARMatrixOps.GetRotation(hitResult.worldTransform);
//Transform to placenote frame of reference (planes are detected in ARKit frame of reference)
Matrix4x4 worldTransform = Matrix4x4.TRS(position, rotation, Vector3.one);
Matrix4x4? placenoteTransform = LibPlacenote.Instance.ProcessPose(worldTransform);
Vector3 hitPosition = PNUtility.MatrixOps.GetPosition(placenoteTransform.Value);
Quaternion hitRotation = PNUtility.MatrixOps.GetRotation(placenoteTransform.Value);
// add shape
AddShape(hitPosition, hitRotation);
return true;
}
}
return false;
}
void Update()
{
// Check if the screen is touched
//-----------------------------------
if (Input.touchCount ==1)
{
var touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began )
{
if (EventSystem.current.currentSelectedGameObject == null)
{
Debug.Log("Not touching a UI button. Moving on.");
// add new shape
var screenPosition = Camera.main.ScreenToViewportPoint(touch.position);
ARPoint point = new ARPoint
{
x = screenPosition.x,
y = screenPosition.y
};
// prioritize reults types
ARHitTestResultType[] resultTypes = {
//ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent,
//ARHitTestResultType.ARHitTestResultTypeExistingPlane,
//ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane,
//ARHitTestResultType.ARHitTestResultTypeEstimatedVerticalPlane,
ARHitTestResultType.ARHitTestResultTypeFeaturePoint
};
foreach (ARHitTestResultType resultType in resultTypes)
{
if (HitTestWithResultType(point, resultType))
{
Debug.Log("Found a hit test result");
return;
}
}
}
}
}
shapeobjnum = shapeObjList.Count;
shapelistnum = shapeInfoList.Count;
}
public void OnSimulatorDropShape()
{
Vector3 dropPosition = Camera.main.transform.position + Camera.main.transform.forward * 0.3f;
Quaternion dropRotation = Camera.main.transform.rotation;
AddShape(dropPosition, dropRotation);
}
public void AddShape(Vector3 shapePosition, Quaternion shapeRotation)
{
// System.Random rnd = new System.Random();
//PrimitiveType type = (PrimitiveType)rnd.Next(0, 3);
ShapeInfo shapeInfo = new ShapeInfo();
shapeInfo.px = shapePosition.x;
shapeInfo.py = shapePosition.y;
shapeInfo.pz = shapePosition.z;
shapeInfo.qx = shapeRotation.x;
shapeInfo.qy = shapeRotation.y;
shapeInfo.qz = shapeRotation.z;
shapeInfo.qw = shapeRotation.w;
//shapeInfo.shapeType = type.GetHashCode();
shapeInfoList.Add(shapeInfo);
GameObject shape = ShapeFromInfo(shapeInfo);
shapeObjList.Add(shape);
}
public GameObject ShapeFromInfo(ShapeInfo info)
{
//GameObject shape = GameObject.CreatePrimitive((PrimitiveType)info.shapeType);
GameObject shape = Instantiate(arrow,new Vector3(0,0,0),Quaternion.identity);
shape.transform.position = new Vector3(info.px, info.py, info.pz);
shape.transform.rotation = new Quaternion(info.qx, info.qy, info.qz, info.qw);
return shape;
}
下面的代码是我调整游戏对象的位置和旋转的地方
public void Rightbtn()
{
if (shapeObjList.Count > 0 && shapeInfoList.Count > 0)
{
shapeObjList[shapeObjList.Count - 1].transform.Rotate(0, 10, 0);
Vector3 pos = shapeObjList[shapeObjList.Count - 1].transform.position;
Quaternion rot = shapeObjList[shapeObjList.Count - 1].transform.rotation;
//How to use this position(pos) and rotation(rot) to update the gameobject [arrow-which is the shape]
}
}