我想需要的时候再开启Vuforia识别图片的功能,识别后再关上,以免影响Hololens的性能。成功实现了,主界面配置如下:
PositionTest按钮是克隆生成cube的,VuforiaControl按钮是控制Vuforia开关的。
imagetarget的设置界面如下,目标发现时执行creatcube函数,里面如果直接克隆生成cube的话会有异常(坐标没更新),所以设置个发现的标记flag就行了(具体参加主代码):
挂在PositionTest按钮上的主代码GetCyclinderPosition.cs如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
public class GetCyclinderPosition : MonoBehaviour
{
// private static GetCyclinderPosition instance;
public VuforiaBehaviour vuforia;
public GameObject VuImageTarget;
bool vuforiacontrolflag = false;
bool detectedflag = false;
/* private void Awake()
{
instance=this;
}*/
// Start is called before the first frame update
void Start()
{
vuforia = GameObject.Find("MixedRealityPlayspace/ARCamera").GetComponent<VuforiaBehaviour>();
// vuforia.enabled = false;
}
public void getcylinderposition()//随便测试cylinder的位置而已,现在不用了,保留吧
{
Vector3 cylinder1position = GameObject.FindGameObjectWithTag("Cylinder1").transform.position;
Debug.Log($"cylinder1 is {cylinder1position}");
}
public void getcubeposition()
{
Vector3 cubeposition = GameObject.FindGameObjectWithTag("cube1").transform.position;
Debug.Log($"cube is {cubeposition}");
// Vector3 modelposition = GameObject.FindGameObjectWithTag("ModelTarget").transform.position;
// Debug.Log($"modeltarget is {modelposition}");
}
public void VuforiaControl()
{
vuforiacontrolflag = !vuforiacontrolflag;
if (vuforiacontrolflag)
{
vuforia.enabled = true;
VuImageTarget.SetActive(true);
Debug.Log("vuforia.enabled = true;");
}
else
{
vuforia.enabled = false;
VuImageTarget.SetActive(false);
Debug.Log("vuforia.enabled = false;");
}
}
public void Creatcube()// Excuted when marker is found
{
detectedflag = true;
}
public void Creatcube2()
{
// Vector3 cubeposition = GameObject.FindGameObjectWithTag("cube1").transform.position;
// Quaternion cuberotation = GameObject.FindGameObjectWithTag("cube1").transform.rotation;
Vector3 markerposition = GameObject.Find("ImageTarget2").transform.position;//ImageTarget2
Quaternion markerrotation = GameObject.Find("ImageTarget2").transform.rotation;
Debug.Log($"markerposition is ({markerposition.x},{markerposition.y},{markerposition.z}),markerrotation is ({markerrotation.eulerAngles.x},{markerrotation.eulerAngles.y},{markerrotation.eulerAngles.z})");
// Vector3 markerpositionloc = GameObject.Find("ImageTarget2").transform.localPosition;//一级目录的local跟global是一样的,子物体不同,子物体的local值是永远不变的
// Quaternion markerrotationloc = GameObject.Find("ImageTarget2").transform.localRotation;
// Debug.Log($"markerpositionloc is ({markerpositionloc.x},{markerpositionloc.y},{markerpositionloc.z}),markerrotationloc is ({markerrotationloc.eulerAngles.x},{markerrotationloc.eulerAngles.y},{markerrotationloc.eulerAngles.z})");
GameObject targetobject = GameObject.Find("ImageTarget2/Cubetest");
Vector3 cubeposition = new Vector3(markerposition.x+ targetobject.transform.localPosition.x, markerposition.y+ targetobject.transform.localPosition.y, markerposition.z+ targetobject.transform.localPosition.z);
Quaternion cuberotation = targetobject.transform.rotation;
Debug.Log($"cubeposition is ({cubeposition.x},{cubeposition.y},{cubeposition.z}),cuberotation is ({cuberotation.eulerAngles.x},{cuberotation.eulerAngles.y},{cuberotation.eulerAngles.z})");
Debug.Log($"cube local position is ({targetobject.transform.localPosition.x},{targetobject.transform.localPosition.y},{targetobject.transform.localPosition.z}),cube local rotation is ({targetobject.transform.localRotation.eulerAngles.x},{targetobject.transform.localRotation.eulerAngles.y},{targetobject.transform.localRotation.eulerAngles.z})");
GameObject newcube = GameObject.Instantiate(targetobject, cubeposition, cuberotation);
// newcube.name = "newcube";
Debug.Log("cube is created!");
detectedflag = false;
if (vuforiacontrolflag)
{
vuforia.enabled = false;
VuImageTarget.SetActive(false);
vuforiacontrolflag = false;
}
// Vector3 modelposition = GameObject.FindGameObjectWithTag("ModelTarget").transform.position;
// Debug.Log($"modeltarget is {modelposition}");
/*
Vector3 markerposition = GameObject.FindGameObjectWithTag("Plane").transform.position;
Quaternion markerrotation = GameObject.FindGameObjectWithTag("Plane").transform.rotation;
Debug.Log($"markerposition is {markerposition},markerrotation is {markerrotation}");
Vector3 markerpositionloc = GameObject.FindGameObjectWithTag("Plane").transform.localPosition;
Quaternion markerrotationloc = GameObject.FindGameObjectWithTag("Plane").transform.localRotation;
Debug.Log($"markerpositionloc is {markerpositionloc},markerrotationloc is {markerrotationloc}");
GameObject targetobject = GameObject.FindGameObjectWithTag("cube2");
Vector3 cubeposition = targetobject.transform.position;
Quaternion cuberotation = targetobject.transform.rotation;
Debug.Log($"cubeposition is {cubeposition},cuberotation is {cuberotation}");
Debug.Log($"cube local position is {targetobject.transform.localPosition},cube local rotation is {targetobject.transform.localRotation}");
GameObject newcube;
newcube = GameObject.Instantiate(targetobject, cubeposition, cuberotation);
float xscale, yscale, zscale;
xscale = GameObject.FindGameObjectWithTag("Plane").transform.localScale.x*targetobject.transform.localScale.x;
yscale= GameObject.FindGameObjectWithTag("Plane").transform.localScale.y * targetobject.transform.localScale.y;
zscale= GameObject.FindGameObjectWithTag("Plane").transform.localScale.z * targetobject.transform.localScale.z;
newcube.transform.localScale =new Vector3(xscale, yscale, zscale);
// newcube = GameObject.Instantiate(targetobject);
// newcube.transform.parent = GameObject.FindGameObjectWithTag("Plane").transform;
newcube.name = "newcube2";
Debug.Log("cube2 is created!");
*/
}
}