#region 触摸和旋转
//上次记录的两个触模点之间距离
float lastDistance = 0f;
float preRadian = 0f;
bool isTwoFinger;
void Update(){
if(IapPopUp.Instance!=null) return;
if(secondContent.childCount>0 && secondContent.GetChild(0)==penIcons){
if(Input.GetMouseButton(0)){
paintCanvas.Drawing(Input.mousePosition);
}else if(Input.GetMouseButtonUp(0)){
paintCanvas.EndDraw();
}
}
if(m_SelectedSticker)
{
if(Input.touchCount<2){
if(!isTwoFinger){
//移动
Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
worldPos.z = m_SelectedSticker.transform.position.z;
m_SelectedSticker.transform.position = worldPos;//+new Vector3(-0.4f,0.4f,0f);//后面加的是偏移
}
}
else
{
//多点时也移动
// Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// worldPos.z = m_SelectedSticker.transform.position.z;
// m_SelectedSticker.transform.position = worldPos;//+new Vector3(-0.4f,0.4f,0f);//后面加的是偏移
//缩放
Touch t1 = Input.touches[0];
Touch t2 = Input.touches[1];
var dx = t1.position.x - t2.position.x;
var dy = t1.position.y - t2.position.y;
float distance = Mathf.Sqrt(dx * dx + dy * dy);
if(!isTwoFinger) lastDistance = distance;
//判断当前距离与上次距离变化,确定是放大还是缩小
const float factor = 1f;
const float minSc = 70f;
const float maxSc = 300f;
float sc = (distance - lastDistance) * factor;
m_SelectedSticker.transform.localScale += new Vector3(sc,sc,0f);
if(m_SelectedSticker.transform.localScale.x<minSc){
m_SelectedSticker.transform.localScale = new Vector3(minSc,minSc,1f);
}else if(m_SelectedSticker.transform.localScale.x>maxSc){
m_SelectedSticker.transform.localScale = new Vector3(maxSc,maxSc,1f);
}
lastDistance = distance;
//旋转
float nowRadian = Mathf.Atan2(t1.position.y- t2.position.y,t1.position.x-t2.position.x);
if(!isTwoFinger) preRadian = nowRadian;
m_SelectedSticker.transform.Rotate(0f,0f, 180f / Mathf.PI * (nowRadian - preRadian));
preRadian = nowRadian;
isTwoFinger = true;
}
}
if(Application.platform== RuntimePlatform.IPhonePlayer || Application.platform== RuntimePlatform.Android){
if(Input.touchCount==0){
isTwoFinger = false;
if(m_SelectedSticker){
if(m_SelectedSticker.transform.localPosition.x<0 ||
m_SelectedSticker.transform.localPosition.x>stickerContainer.sizeDelta.x ||
m_SelectedSticker.transform.localPosition.y<0 ||
m_SelectedSticker.transform.localPosition.y>stickerContainer.sizeDelta.y
){
Destroy(m_SelectedSticker.gameObject);
}
m_SelectedSticker = null;
}
}
}
}
#endregion
public void AddSticker(GameObject go){
if(stickerContainer.childCount<20){
GameObject icon = new GameObject(go.name);
SpriteRenderer sr = icon.AddComponent<SpriteRenderer>();
sr.material = maskMat;
sr.sortingLayerName = "UI";
sr.sortingOrder = 10;
sr.sprite = go.transform.FindChild("Icon").GetComponent<Image>().sprite;
icon.transform.parent = stickerContainer;
icon.layer = stickerContainer.gameObject.layer;
icon.transform.localScale = Vector3.one*100f;
icon.transform.localPosition = new Vector3(stickerContainer.sizeDelta.x*0.5f,stickerContainer.sizeDelta.y*0.5f,-stickerContainer.childCount*0.001f);
BoxCollider2D col = icon.AddComponent<BoxCollider2D>();
col.isTrigger = true;
GamePageManager.gamePageInstance.PlayRandomSound();
}
else
{
AudioManager.Instance.PlaySoundEffect("UI/Error",1f,0.2f);
}
}
void OnDown( SpriteLayerInputManager.TouchEvent evt){
if(m_SelectedSticker==null && m_SelectedSticker!=evt.pressObject){
if(evt.pressObject.transform.parent==stickerContainer){
m_SelectedSticker = evt.pressObject;
m_SelectedSticker.transform.SetAsLastSibling();
for(int i=0;i<stickerContainer.childCount;++i){
Transform child = stickerContainer.GetChild(i);
Vector3 v = child.localPosition;
v.z = -i*0.001f;
child.localPosition = v;
}
}
}
}
void OnUp( SpriteLayerInputManager.TouchEvent evt){
if(m_SelectedSticker && m_SelectedSticker==evt.pressObject){
if(evt.pressObject.transform.parent==stickerContainer){
Transform tran = m_SelectedSticker.transform;
if(tran.localPosition.x<0 ||
tran.localPosition.x>stickerContainer.sizeDelta.x ||
tran.localPosition.y<0 ||
tran.localPosition.y>stickerContainer.sizeDelta.y
){
Destroy(m_SelectedSticker.gameObject);
}
m_SelectedSticker = null;
}
}
}
2d多点旋转,缩放,移动
最新推荐文章于 2022-08-31 17:12:17 发布