1090628 放大和縮小與旋轉
by 黃國哲 2020-06-28 11:37:17, 回應(0), 人氣(900)
以下指令的功能描述:
****使用時把以下的功能複製貼到update裡面****
1.縮小時不能比原來的圖小
2.放大到最多一倍
using UnityEngine;
using System.Collections;
using System.IO;
public class scriptai : MonoBehaviour
{
private Touch oldTouch1; //上次觸控點1(手指1)
private Touch oldTouch2; //上次觸控點2(手指2)
void Start()
{}
void Update () {
//沒有觸控
if ( Input.touchCount <= 0 ){
return;
}
//多點觸控, 放大縮小
Touch newTouch1 = Input.GetTouch (0);
Touch newTouch2 = Input.GetTouch (1);
//第2點剛開始接觸螢幕, 只記錄,不做處理
if( newTouch2.phase == TouchPhase.Began ){
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
return;
}
//計算老的兩點距離和新的兩點間距離,變大要放大模型,變小要縮放模型
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//兩個距離之差,為正表示放大手勢, 為負表示縮小手勢
float offset = newDistance - oldDistance;
//放大因子, 一個畫素按 0.01倍來算(100可調整)
float scaleFactor = offset / 100f;
Vector3 localScale = transform.localScale;
Vector3 scale = new Vector3(localScale.x + scaleFactor,
localScale.y + scaleFactor,
localScale.z + scaleFactor);
//最小縮放到 1 倍,更改數字可以改變縮小比例,例如改成0.3為0.3倍
if (scale.x > 1f && scale.y > 1f && scale.z > 1f) {
transform.localScale = scale;
}
//記住最新的觸控點,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
}
///////////////////////////////////
Type2
void Update()
{
Ray ray = new Ray();
ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Stationary)
{
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(touchDeltaPosition.x * Time.deltaTime *0.03f, touchDeltaPosition.y * Time.deltaTime * 0.03f, 0);
}
if (Input.touchCount > 1)
{
//当第二根手指按下的时候
if (Input.GetTouch(1).phase == TouchPhase.Began)
{
isTwoTouch = true;
//获取第一根手指的位置
firstTouch = Input.touches[0].position;
//获取第二根手指的位置
secondTouch = Input.touches[1].position;
lastDistance = Vector2.Distance(firstTouch, secondTouch);
}
//如果有两根手指按下
if (isTwoTouch)
{
//每一帧都得到两个手指的坐标以及距离
firstTouch = Input.touches[0].position;
secondTouch = Input.touches[1].position;
twoTouchDistance = Vector2.Distance(firstTouch, secondTouch);
//当前图片的缩放
Vector3 curImageScale = new Vector3(image.localScale.x, image.localScale.y, 1);
//两根手指上一帧和这帧之间的距离差
//因为100个像素代表单位1,把距离差除以100看缩放几倍
float changeScaleDistance = (twoTouchDistance - lastDistance) / 100;
//因为缩放 Scale 是一个Vector3,所以这个代表缩放的Vector3的值就是缩放的倍数
Vector3 changeScale = new Vector3(changeScaleDistance, changeScaleDistance, 0);
//图片的缩放等于当前的缩放加上 修改的缩放
image.localScale = curImageScale + changeScale;
//控制缩放级别
image.localScale = new Vector3(Mathf.Clamp(image.localScale.x, 0.5f, 10f), Mathf.Clamp(image.localScale.y, 0.5f, 10f), 1);
//这一帧结束后,当前的距离就会变成上一帧的距离了
lastDistance = twoTouchDistance;
}
//当第二根手指结束时(抬起)
if (Input.GetTouch(1).phase == TouchPhase.Ended)
{
isTwoTouch = false;
firstTouch = Vector3.zero;
secondTouch = Vector3.zero;
}
}
————————————————
版权声明:本文为CSDN博主「bigmangaofei」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/gaofei12300/java/article/details/79754131
//////////////////////////////////////
Type 3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchMove : MonoBehaviour
{
public float distance = 60f;
//记录上一次手机触摸位置判断用户是在左放大还是缩小手势
private Vector2 oldPosition1;
private Vector2 oldPosition2;
private Vector2 FingerPos;
private Vector2 lastPos;
private float speed = 0.01f;
public LayerMask layerMask;
private string tag = "ground";//覆盖在场景上的plane ,设置layer层为ground,tag 为ground
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//没有触摸
if (Input.touchCount <= 0)
{
return;
}
if (Input.touchCount == 1)
{//单指操作
if (Input.touches[0].phase == TouchPhase.Began)
{
lastPos = Input.touches[0].position;
}
else if (Input.touches[0].phase == TouchPhase.Moved && Input.touches[0].phase != TouchPhase.Stationary)
{
if (lastPos != Input.touches[0].position)//解决手指用力向下按也会触发TouchPhase.Moved
{
if(isRayHit(Input.touches[0].position))//手指触摸到场景区域,可移动,超出范围就不移动
{
transform.Translate(new Vector3(Input.touches[0].deltaPosition.x * speed, 0, Input.touches[0].deltaPosition.y * speed));
}
}
lastPos = Input.touches[0].position;
}
}
else if (Input.touchCount > 1 && Input.touches[0].phase != TouchPhase.Stationary && Input.touches[1].phase != TouchPhase.Stationary)//多指操作
{
//前两只手指触摸类型都为移动触摸
if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
{
//计算出当前两点触摸点的位置
Vector2 tempPosition1 = Input.GetTouch(0).position;
Vector2 tempPosition2 = Input.GetTouch(1).position;
//函数返回真为放大,返回假为缩小
if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
{
//这里的数据自己任意修改,根据项目需求而定
distance -= 20f * Time.deltaTime;
if (distance <= 10)
{
distance = 10;
}
Camera.main.fieldOfView = distance;
}
else
{
distance += 20f * Time.deltaTime;
if (distance >= 100f)
{
distance = 100;
}
Camera.main.fieldOfView = distance;
}
//备份上一次触摸点的位置,用于对比
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
}
}
}
//函数返回真为放大,返回假为缩小
bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
{
//函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
if (leng1 < leng2)
{
//放大手势
return true;
}
else
{
//缩小手势
return false;
}
}
/// <summary>
/// 监测射线是否碰撞到有效地面
/// </summary>
/// <returns><c>true</c>, if ray hit was ised, <c>false</c> otherwise.</returns>
/// <param name="mousePosition">Mouse position.</param>
bool isRayHit(Vector3 mousePosition)
{
bool isTrue = false;
Ray ray = Camera.main.ScreenPointToRay(mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 500, layerMask))
{
print("碰到层:" + isTrue);
if (hitInfo.collider.transform.tag == tag)
{
isTrue = true;
}
}
else
{
}
print("碰到点:" + isTrue);
return isTrue;
}
}
————————————————
版权声明:本文为CSDN博主「tianyongheng」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/tianyongheng/java/article/details/78586959
以下為原文
//////////////////////////////////////
using UnityEngine;
using System.Collections;
using System.IO;
public class ScaleAndRotate : MonoBehaviour
{
private Touch oldTouch1; //上次觸控點1(手指1)
private Touch oldTouch2; //上次觸控點2(手指2)
void Start()
{
}
void Update () {
//沒有觸控
if ( Input.touchCount <= 0 ){
return;
}
//單點觸控, 水平上下旋轉
if( 1 == Input.touchCount ){
Touch touch = Input.GetTouch (0);
Vector2 deltaPos = touch.deltaPosition;
transform.Rotate(Vector3.down * deltaPos.x , Space.World);
transform.Rotate(Vector3.right * deltaPos.y , Space.World);
}
//多點觸控, 放大縮小
Touch newTouch1 = Input.GetTouch (0);
Touch newTouch2 = Input.GetTouch (1);
//第2點剛開始接觸螢幕, 只記錄,不做處理
if( newTouch2.phase == TouchPhase.Began ){
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
return;
}
//計算老的兩點距離和新的兩點間距離,變大要放大模型,變小要縮放模型
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//兩個距離之差,為正表示放大手勢, 為負表示縮小手勢
float offset = newDistance - oldDistance;
//放大因子, 一個畫素按 0.01倍來算(100可調整)
float scaleFactor = offset / 100f;
Vector3 localScale = transform.localScale;
Vector3 scale = new Vector3(localScale.x + scaleFactor,
localScale.y + scaleFactor,
localScale.z + scaleFactor);
//最小縮放到 0.3 倍
if (scale.x > 0.3f && scale.y > 0.3f && scale.z > 0.3f) {
transform.localScale = scale;
}
//記住最新的觸控點,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
}
回應