折腾了好久的地图缩放 ngui 各种偷懒实现

当时找到一篇cocos2dx 地图缩放的  很遗憾我用不了  也要记录一下 免得以后用ugui可以用 

转 http://blog.csdn.net/cocosnode/article/details/40829017

 

using projectQ;
using UnityEngine;

public class MapGestures : MonoBehaviour
{
public GameObject BoundrayLeft;
public GameObject BoundrayRight;
public GameObject BoundrayTop;
public GameObject BoundrayBottom;

float xScaleOrigin;
float yScaleOrigin;
float fScaleFactor = 0;
bool bBeginOpt = false;
bool bPinchState = false;

Camera cam;
Vector3 moveBeginPos;
Vector3 BoundrayVecLeft;
Vector3 BoundrayVecRight;
Vector3 BoundrayVecTop;
Vector3 BoundrayVecBottom;
Vector3 tmpart = Vector3.zero;
GameObject tobject;
void Start()
{
//transform.localScale = new Vector3(1.6f, 1.6f, 1);
//transform.localPosition = new Vector3(-130f, 130f, transform.localPosition.z);
BoundrayVecLeft = BoundrayLeft.GetComponent<Transform>().position;
BoundrayVecRight = BoundrayRight.GetComponent<Transform>().position;
BoundrayVecTop = BoundrayTop.GetComponent<Transform>().position;
BoundrayVecBottom = BoundrayBottom.GetComponent<Transform>().position;
cam = _R.ui.UICamera.GetComponent<Camera>();
xScaleOrigin = transform.localScale.x;
yScaleOrigin = transform.localScale.y;
}
//private void OnGUI()
//{

// if (GUI.Button(new Rect(10, 100, 120, 65), "放大"))
// {
// ScaleMapPanel(tmpos,true);
// }
//}
void OnDrag(DragGesture gesture)
{
// 当前识别器阶段 (Started/Updated/Ended)
ContinuousGesturePhase phase = gesture.Phase;
// 最后一帧的拖拽/移动数据
Vector2 deltaMove = gesture.DeltaMove;
//完整的拖拽数据
if (gesture.Phase == ContinuousGesturePhase.Started)
{
}
else if (gesture.Phase == ContinuousGesturePhase.Updated)
{
Vector3 vvRelate = new Vector3(transform.localPosition.x + deltaMove.x, transform.localPosition.y + deltaMove.y, transform.localPosition.z);
if (gesture.Fingers.Count == 1 && !bPinchState && CheckScreenBoundray(deltaMove))
{
transform.localPosition = new Vector3(vvRelate.x, vvRelate.y, transform.localPosition.z);
}
}
}


//双指缩放
void OnPinch(PinchGesture gesture)
{
ContinuousGesturePhase phase = gesture.Phase;
// 当前两个手指的距离
float gap = gesture.Gap;
// 当前与上一帧的变动值
float delta = gesture.Delta;
var bZoomIn = delta > 0 ? true : false;

if (gesture.Phase == ContinuousGesturePhase.Started)
{
moveBeginPos = gesture.Position;
bBeginOpt = false;
bPinchState = true;
}
if (gesture.Phase == ContinuousGesturePhase.Ended)
{
moveBeginPos = new Vector3();
bPinchState = false;
}
if (moveBeginPos.sqrMagnitude > 0)
{
ScaleMapPanel(moveBeginPos, bZoomIn);
}
}
void ScaleMapPanel(Vector3 tmpos, bool bZoomIn)
{
if (!bBeginOpt)
{
tmpos = cam.ScreenToWorldPoint(tmpos);
tmpos = transform.InverseTransformPoint(tmpos);
if (tobject == null)
{
tobject = new GameObject("maodian");
tobject.transform.SetParent(transform);
}
tobject.transform.localPosition = tmpos;
bBeginOpt = true;
tmpart = tobject.transform.position;
}
if (bZoomIn)
{
fScaleFactor += 0.1f;
}
else
{
fScaleFactor -= 0.1f;
}

fScaleFactor = Mathf.Clamp(fScaleFactor, 0, 3);

var tempScaleX = xScaleOrigin + fScaleFactor;
var tempScaleY = yScaleOrigin + fScaleFactor;

transform.localScale = new Vector3(tempScaleX, tempScaleY, 1);

Vector3 tposnow = tobject.transform.position;

Vector3 tdis = tposnow - tmpart;

//if(CheckWorldBoundray(tdis))
transform.position = transform.position - tdis;

UpdateBoundray();

}
bool CheckScreenBoundray(Vector3 vPos)
{
//DebugPro.LogError("rextest CheckBoundray x " + vPos.x + " rextest CheckBoundray y " + vPos.y + " rextest CheckBoundray z " + vPos.z);
if (vPos.x > 0)
{
var brL = cam.WorldToScreenPoint(BoundrayLeft.GetComponent<Transform>().position).x + vPos.x;
if (brL > 0)
return false;
}
else
{
var brR = cam.WorldToScreenPoint(BoundrayRight.GetComponent<Transform>().position).x + vPos.x;
if (brR < Screen.width)
return false;
}

//up
if (vPos.y > 0)
{
var brB = cam.WorldToScreenPoint(BoundrayBottom.GetComponent<Transform>().position).y + vPos.y;
if (brB > 0)
return false;
}
else
{
var brT = cam.WorldToScreenPoint(BoundrayTop.GetComponent<Transform>().position).y + vPos.y;
if (brT < Screen.height)
return false;
}


//DebugPro.LogError("rextest CheckBoundray W is : " + w + " H is : " + h);
//DebugPro.LogError("rextest CheckBoundray brL is : " + brL);
//DebugPro.LogError("rextest CheckBoundray brR is : " + brR);
//DebugPro.LogError("rextest CheckBoundray brT is : " + brT);
//DebugPro.LogError("rextest CheckBoundray brB is : " + brB);
return true;
}
void UpdateBoundray()
{

var brL = cam.WorldToScreenPoint(BoundrayLeft.GetComponent<Transform>().position).x;
if (brL > 0)
{
Vector3 offset = BoundrayVecLeft - BoundrayLeft.GetComponent<Transform>().position;
transform.position = transform.position + offset;
}

var brR = cam.WorldToScreenPoint(BoundrayRight.GetComponent<Transform>().position).x;
if (brR < Screen.width)
{
Vector3 offset = BoundrayVecRight - BoundrayRight.GetComponent<Transform>().position;
transform.position = transform.position + offset;
}

//up
var brB = cam.WorldToScreenPoint(BoundrayBottom.GetComponent<Transform>().position).y;
if (brB > 0)
{
Vector3 offset = BoundrayVecBottom - BoundrayBottom.GetComponent<Transform>().position;
transform.position = transform.position + offset;
}
var brT = cam.WorldToScreenPoint(BoundrayTop.GetComponent<Transform>().position).y;
if (brT < Screen.height)
{
Vector3 offset = BoundrayVecTop - BoundrayTop.GetComponent<Transform>().position;
transform.position = transform.position + offset;
}
}
}

 

posted @ 2017-12-05 15:24  rexzhao  阅读(344)  评论(0编辑  收藏  举报