Commit 4c7eff3e authored by shujianhe's avatar shujianhe

1

parent dad803f7
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
delegate void CheckInput();
public class mainGameScaleMove : MonoBehaviour
{
private CheckInput checkInput = null;
private Vector2 tempVector = new Vector2(0, 0);
private Vector2 defvalueVector = new Vector2(-90000, -90000);
private Vector2 nowScale = new Vector2();
private Vector2 nowPos = new Vector2();
private Vector2 nowSize = new Vector2();
private bool skipDoubleScale = false;
public float OldTouchCurrDis = -1000;
public float minScale = 0.25f;
public float maxScale = 8f;
public Vector2 NowPos
{
get { return nowPos; }
}
public Vector2 NowScale
{
get
{
return nowScale;
}
}
// Start is called before the first frame update
void Start()
{
setCheckInput();
Vector3 tempVector3;
Quaternion quaternion;
for (int i = 0; i < transform.childCount; i++)
{
var itemChild = transform.GetChild(i);
itemChild.GetLocalPositionAndRotation(out tempVector3, out quaternion);
itemChild.SetLocalPositionAndRotation(tempVector3, quaternion);
if(i == 0)
{
nowPos = new Vector2(tempVector3.x,tempVector3.y);
nowScale = itemChild.localScale;
Rect rect = GetComponent<RectTransform>().rect;
nowSize = new Vector2(rect.width, rect.height);
}
}
}
private void setCheckInput()
{
if (checkInput == null)
{
switch (Application.platform)
{
case RuntimePlatform.WindowsEditor:
case RuntimePlatform.WindowsPlayer:
case RuntimePlatform.WebGLPlayer:
case RuntimePlatform.OSXEditor:
case RuntimePlatform.OSXPlayer:
checkInput = checkInputByComputer;
break;
case RuntimePlatform.Android:
case RuntimePlatform.IPhonePlayer:
checkInput = checkInputByPhone;
break;
default:
checkInput = checkInputByPhone;
break;
}
}
}
void checkInputByPhone()
{
if (Input.touchCount == 2)
{
clearOneTouchBegin();
Touch touch1 = Input.GetTouch(0);
Touch touch2 = Input.GetTouch(1);
if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved)
{
if (skipDoubleScale) return;
onDoubleTouchHandler(Vector2.Distance(touch1.position, touch2.position));
}
else
{
OldTouchCurrDis = -1000;
}
skipDoubleScale = false;
//else
//{
//}
} else if (Input.touchCount == 1)
{
var oneTouch = Input.GetTouch(0);
TouchPhase touchPhase = oneTouch.phase;
if(touchPhase == TouchPhase.Began)
{
onOneTouchBegin(oneTouch.position);
}
else if(touchPhase == TouchPhase.Moved)
{
onOneTouchMove(oneTouch.position, oneTouch.deltaPosition);
}else if(touchPhase == TouchPhase.Ended)
{
onOneTouchEnd(oneTouch.position, oneTouch.deltaPosition);
}else if(touchPhase == TouchPhase.Canceled)
{
clearOneTouchBegin();
}
}
}
void onDoubleTouchHandler(float nowTouchDistance)
{
if(OldTouchCurrDis < -999)
{
OldTouchCurrDis = nowTouchDistance;
return;
}
var nowOffset = (nowTouchDistance - OldTouchCurrDis) * 0.002f;
if(nowOffset < 0)
{
if (nowScale.x + nowOffset < minScale)
{
skipDoubleScale = true;
nowOffset = 0.8f;
}
}else if(nowOffset > 0)
{
if(nowScale.x + nowOffset > maxScale)
{
skipDoubleScale = true;
return;
}
}
nowScale = new Vector2(nowScale.x + nowOffset,nowScale.y + nowOffset) ;
OldTouchCurrDis = nowTouchDistance;
for (int i = 0; i < transform.childCount; i++)
{
var itemChild = transform.GetChild(i);
itemChild.localScale = nowScale;
}
}
void clearOneTouchBegin()
{
utilsTools.delUserDataByKey(gameObject, "oneTouchBegin");
}
//左键点击 右键缩放
void checkInputByComputer()
{
////0 1 2 鼠标 左中右
//bool RDown = Input.GetMouseButtonDown(2);
//bool LDown = Input.GetMouseButtonDown(0);
//if (RDown == LDown && RDown == true)//当作双指滑动 制造缩放
//{
//}else if(RDown != LDown && LDown == true)
//{
// Vector3 mousePosition = Input.mousePosition;
// if (utilsTools.ContainsKey(gameObject, "oneTouchBegin") == false)
// onOneTouchBegin(new Vector2(mousePosition.x, mousePosition.y));
// else
// {
// }
//}else if(LDown == false){
// if (utilsTools.ContainsKey(gameObject, "oneTouchBegin") == false) return;
//}
}
private void onOneTouchBegin(Vector2 vector2)
{
utilsTools.setUserData(gameObject, "oneTouchBegin", vector2);
}
private void onOneTouchMove(Vector2 nowPos,Vector2 delatPos)
{
Vector3 tempVector3 = new Vector3(0, 0);
Quaternion quaternion = new Quaternion();
for (int i = 0; i < transform.childCount; i++)
{
var itemChild = transform.GetChild(i);
if(i == 0)
{
itemChild.GetLocalPositionAndRotation(out tempVector3, out quaternion);
float width = nowScale.x * nowSize.x;
float height = nowScale.y * nowSize.y;
if(delatPos.x < 0 && (tempVector3.x + delatPos.x) < (width / -2))
{
delatPos.x = 0;
}else if(delatPos.x > 0 && (tempVector3.x + delatPos.x) > (width / 2))
{
delatPos.x = 0;
}
if(delatPos.y < 0 && (tempVector3.y + delatPos.y) < (height / -2))
{
delatPos.y = 0;
}
else if(delatPos.y > 0 && (tempVector3.y + delatPos.y) > (height / 2))
{
delatPos.y = 0;
}
if (delatPos.x == delatPos.y && delatPos.x == 0) return;//都不用移动就不更新坐标
tempVector3.x += delatPos.x;
tempVector3.y += delatPos.y;
}
itemChild.SetLocalPositionAndRotation(tempVector3, quaternion);
}
//更新坐标 应该记录当前显示的位置
}
private void onOneTouchEnd(Vector2 nowPos, Vector2 delatPos)
{
utilsTools.getUserData<Vector2>(gameObject, "oneTouchBegin", out tempVector, defvalueVector);
if (tempVector.Equals(defvalueVector))//说明之前已经移除了
{
return;
}
float sqrMagnitude = (nowPos - defvalueVector).SqrMagnitude();
clearOneTouchBegin();
if (sqrMagnitude > 20f) {//按下和抬起距离太远就不算点击
return;
}
onOneClick(nowPos);
}
private void onOneClick(Vector2 nowPos)
{
print("点击>>>>" + nowPos.ToString());
}
// Update is called once per frame
void Update()
{
setCheckInput();
checkInput();
}
}
fileFormatVersion: 2
guid: c3deb1e6e6112304d918d673de43ccc6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment