公共函数,不放入任何模型上
using UnityEngine;
using System.Collections;
public class Globe : MonoBehaviour {
public static int AugmentationObjectState = 0;//增强物体的状态:0表示没有检测到识别图; 1表示在识别图之上;2表示脱离识别图,但仍存在。
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
放在需要旋转、缩放、拖拽的模型上
using UnityEngine;
using System.Collections;
public class GetModelScale : MonoBehaviour {
public Vector3 go_modelDefaultScale;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
go_modelDefaultScale = this.transform.localScale;
}
}
放在需要旋转、缩放、拖拽的模型上
using UnityEngine;
using System.Collections;
public class TouchToRotateAndScaleModel : MonoBehaviour {
public GameObject go_model;
private Vector3 go_modelCurScale;
private Vector3 go_modelDefaultScale;
private float curScale = 1.0f;
private float olddis = 0.0f;
private float newdis = 0.0f;
private float RoatedSpeed = 100.0F;
public float y;
public float z;
private Transform pickedObject = null;
// Use this for initialization
void Start()
{
y = 90;
}
// Update is called once per frame
void Update()
{
//旋转功能
if (Input.touchCount==1)
{
if(Input.touches[0].phase == TouchPhase.Moved)//手指移动
{
y += Input.GetAxis("Mouse X") * Time.deltaTime * RoatedSpeed;
z += Input.GetAxis("Mouse Y") * Time.deltaTime * RoatedSpeed;
if (z <= -90)
{
z = Mathf.Ceil(-90);
}
if (z >= 90)
{
z = Mathf.Ceil(90);
}
transform.eulerAngles = new Vector3(0, y, -z);
}
}
//缩放功能
if(Input.touchCount == 2)
{
if (go_modelDefaultScale != go_model.GetComponent<GetModelScale>().go_modelDefaultScale)
{
go_modelDefaultScale = go_modelCurScale = go_model.GetComponent<GetModelScale>().go_modelDefaultScale;
}
if(Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved)
{
var pos1=Input.GetTouch(0).position;
var pos2=Input.GetTouch(1).position;
newdis=Vector2.Distance(pos1,pos2);
curScale = go_modelCurScale.x / go_modelDefaultScale.x;
if(newdis < olddis)
{
go_model.transform.localScale -= go_modelDefaultScale * Time.deltaTime;
}
if(newdis>olddis)
{
go_model.transform.localScale += go_modelDefaultScale * Time.deltaTime;
}
go_modelCurScale = go_model.transform.localScale;
olddis=newdis;
}
}
//拖动功能
if(Input.touchCount == 3)
{
if(Globe.AugmentationObjectState == 2 )
{
foreach (Touch touch in Input.touches)
{
Debug.Log("Touching at: " + touch.position);
if (touch.phase == TouchPhase.Began)
{
Debug.Log("Touch phase began at: " + touch.position);
pickedObject = go_model.transform;
}
else if (touch.phase == TouchPhase.Moved)
{
Debug.Log("Touch phase Moved");
if (pickedObject != null)
{
Vector2 screenDelta = touch.deltaPosition;
float halfScreenWidth = 0.5f * Screen.width;
float halfScreenHeight = 0.5f * Screen.height;
float dx = screenDelta.x / halfScreenWidth;
float dy = screenDelta.y / halfScreenHeight;
Vector3 objectToCamera = pickedObject.transform.position - Camera.main.transform.position;
float distance = objectToCamera.magnitude;
float fovRad = Camera.main.fieldOfView * Mathf.Deg2Rad;
float motionScale = distance * Mathf.Tan(fovRad/2);
Vector3 translationInCameraRef = new Vector3(motionScale * dx, motionScale * dy, 0);
Vector3 translationInWorldRef =Camera.main.transform.TransformDirection(translationInCameraRef);
pickedObject.position += translationInWorldRef;
}
}
else if (touch.phase == TouchPhase.Ended)
{
Debug.Log("Touch phase Ended");
pickedObject = null;
}
}
}
}
}
}