/// <summary> /// 更改属性值 /// </summary> /// <param name="value"></param> public void SetChangingValue(float value) { if (handleRect == null) { Debug.LogError("未指定当前滚动对象:handleRect属性为Null"); return; } if (minValue == null || maxValue == null) { SetRectData(); } Vector3 position = Vector3.zero; float moveValue = 0; switch (KguiAxis) { case Axis.X: //if (horizontal == Horizontal.RightToLeft) // value = 1 - value; moveValue = minValue.Value + value * sumValue; //在将屏幕坐标转化为世界坐标 Vector3 handleScreenX = MUtility.UIWorldToScreenPoint(handleRect.position); position = MUtility.UIScreenToWorldPoint(new Vector3(moveValue, handleScreenX.y, handleScreenX.z)); position.y = handleRect.position.y; position.z = handleRect.position.z; break; case Axis.Y: //if (vertical == Vertical.TopToBottom) // value = 1 - value; moveValue = maxValue.Value - value * sumValue; Vector3 handleScreenY = MUtility.UIWorldToScreenPoint(handleRect.position); position = MUtility.UIScreenToWorldPoint(new Vector3(handleScreenY.x, moveValue, handleScreenY.z)); position.x = handleRect.position.x; position.z = handleRect.position.z; break; default: break; } handleRect.position = position; }
/// <summary> /// 判断是否在区域内 /// </summary> /// <param name="handIndex"></param> /// <returns></returns> bool IsAreaContains(int handIndex) { if (panelType == PanelType.UI) { return(KGUI_Utility.IsAreaContains(transform, handIndex) || OtherRectAreaContains(handIndex)); } else { Vector3 screenPoint = MUtility.UIWorldToScreenPoint(transform.position); return(KGUI_Utility.IsAreaContains(screenPoint, panelSize, handIndex) || OtherRectAreaContains(handIndex)); } }
/// <summary> /// 设置RectTransform基本数据 /// </summary> public void SetRectData() { if (handleRect == null) { Debug.LogError("请设置拖动滚动条参数"); return; } //获取到屏幕坐标 Vector3 scrollbarScreen = MUtility.UIWorldToScreenPoint(transform.position); RectTransform rectTransform = GetComponent <RectTransform>(); if (KguiAxis == Axis.X) { //设置父物体左右两边的值 float left = scrollbarScreen.x - rectTransform.sizeDelta.x / 2; float right = scrollbarScreen.x + rectTransform.sizeDelta.x / 2; //设置滚动条的最大可移动值和最小的可移动值 minValue = left + handleRect.sizeDelta.x / 2; maxValue = right - handleRect.sizeDelta.x / 2; } if (KguiAxis == Axis.Y) { float top = scrollbarScreen.y + rectTransform.sizeDelta.y / 2; float bottom = scrollbarScreen.y - rectTransform.sizeDelta.y / 2; minValue = bottom + handleRect.sizeDelta.y / 2; maxValue = top - handleRect.sizeDelta.y / 2; } var box = GetComponent <BoxCollider>(); box.size = new Vector3(rectTransform.sizeDelta.x, rectTransform.sizeDelta.y, 0); sumValue = Mathf.Abs(minValue.Value - maxValue.Value); }
/// <summary> /// 是否存在区域内 /// </summary> /// <param name="transform">指定物体</param> /// <param name="handIndex"></param> /// <returns></returns> public static bool IsAreaContains(Transform transform, int handIndex) { //if (!KinectTransfer.IsHandActive(handIndex)) return false; try { //获取到此时手的屏幕坐标屏幕坐标 Vector3 screenHandPoint = MOperateManager.GetHandScreenPoint(handIndex); Vector3 screenPoint = MUtility.UIWorldToScreenPoint(transform.position); //根据自身此时的屏幕坐标,去算区域 RectTransform rectTransform = transform.GetComponent <RectTransform>(); return(ScreenPointContains(screenPoint, rectTransform.sizeDelta, screenHandPoint)); } catch (Exception) { return(false); //throw new Exception("手势可能没激活,如果是在编辑器上遇到此问题,不用理会"); } }
/// <summary> /// 判断其他集合区域是否在范围内 /// </summary> /// <returns></returns> bool OtherRectAreaContains(int hand) { if (panelType == PanelType.UI) { if (OtherRects == null) { return(false); } foreach (var item in OtherRects) { if (KGUI_Utility.IsAreaContains(item, hand)) { return(true);//如果存在,就可以直接返回了 } } } else { if (OtherSizes == null) { return(false); } foreach (var item in OtherSizes) { Vector3 screenPoint = MUtility.UIWorldToScreenPoint(item.transform.position); if (KGUI_Utility.IsAreaContains(screenPoint, item.panelSize, hand)) { return(true); } } } return(false); }