unity3d ngui dragableCamera 与scrollbar
ngui 拖动物体的第二个例子里没有和scrollbar
自己写了一个
using UnityEngine;
using System.Collections;
public class ScrollBar : MonoBehaviour {
public UIScrollBar sb;
public UIDraggableCamera dragCam;
private bool isPressed;
public Transform scrollBarBack;
private Vector3 lastCamPos;
// Use this for initialization
void Start () {
if (sb == null && dragCam == null) {
Debug.Log("not set value");
}
lastCamPos = dragCam.transform.localPosition;
isPressed = false;
//sb.onChange += setScrollvalue;
}
// Update is called once per frame
void Update () {
if (isPressed) {
isPressed = Input.touchCount == 1&&Input.GetMouseButton(0);
}
if (!isPressed)
{
Vector3 nowpos = dragCam.transform.localPosition;
if (lastCamPos != nowpos)
{
float hhh = nowpos.y - lastCamPos.y;
float h = (nowpos - lastCamPos).magnitude*(nowpos.magnitude>lastCamPos.magnitude?-1:1);
float H = h;
Bounds b = NGUIMath.CalculateAbsoluteWidgetBounds(dragCam.rootForBounds);//整个面板的bounds
float hh = b.extents.y * Screen.height * dragCam.scale.y + b.extents.x * Screen.width * dragCam.scale.x;
h = h / hh;
sb.scrollValue += h;
lastCamPos = nowpos;
//Debug.Log("HH/"+H+" h/"+h+" hh/"+hhh+" scrollv/"+sb.scrollValue);
}
}
}
void OnPress(bool pressed)
{
isPressed = true;
Debug.Log("On pressed/"+isPressed);
if (enabled && NGUITools.GetActive(gameObject)&&dragCam!=null)
{
dragCam.Press(pressed);
}
}
void OnDrag(Vector2 delta)
{
//Debug.Log("delta/"+delta);
Bounds b = NGUIMath.CalculateAbsoluteWidgetBounds(dragCam.rootForBounds);//整个面板的bounds
float h=scrollBarBack.localScale.y;//scrollbar的高度
float w = scrollBarBack.localScale.x;
delta.y =dragCam.scale.y* (delta.y / h) * b.extents.y * Screen.height ;
delta.x =dragCam.scale.x* (delta.x / w) * b.extents.x * Screen.width;
Debug.Log("panel bounds:" + b + " scale/" + dragCam.scale+ " delta/" + delta);
if (enabled && NGUITools.GetActive(gameObject) && dragCam != null)
{
dragCam.Drag(delta*-1);
}
}
}
浙公网安备 33010602011771号