public void OnBeginDrag(PointerEventData eventData) { rockerFrame.SetActive(true); RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, eventData.position, eventData.pressEventCamera, out localPos); rockerFrame.transform.localPosition = localPos; }
public void Update() { RectTransformUtility.ScreenPointToLocalPointInRectangle(_rect, Input.mousePosition, null, out mLocalMousePos); mLocalMousePos.y = mDimensions.y + mLocalMousePos.y; }
public void OnPointerDown(PointerEventData data) { transform.parent.SetAsLastSibling(); originalPanelLocalPosition = panelRectTransform.localPosition; RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition); }
public void OnPointerDown(PointerEventData data) { rectTransform.SetAsLastSibling(); RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, data.position, data.pressEventCamera, out previousPointerPosition); }
// Update is called once per frame void FixedUpdate() { if (reloading && Time.time > lastReloadTime + reloadTime[(int)currentWeapon]) { ammo = maxAmmo[(int)currentWeapon]; reloading = false; } if (health <= 0) { battleArea.addAliveTimeStat(gameObject.name, aliveTime); if (battleArea.kdStatText) { if (gameObject.name.StartsWith("MLAgent")) { battleArea.kdStatText.GetComponent <StatsCounter>().addMLSurviveTime(aliveTime); } else if (gameObject.name.StartsWith("MLDisAgent")) { battleArea.kdStatText.GetComponent <StatsCounter>().addMLDisSurviveTime(aliveTime); } else if (gameObject.name.StartsWith("RBSAgent")) { battleArea.kdStatText.GetComponent <StatsCounter>().addRBSSurviveTime(aliveTime); } else if (gameObject.name.StartsWith("Human")) { battleArea.kdStatText.GetComponent <StatsCounter>().addHumanSurviveTime(aliveTime); } } if (agent.brain) { agent.AddReward(-1f); agent.Done(); //gameObject.SetActive(false); } else { if (destroyOnDied) { battleArea.players.Remove(gameObject); Destroy(statusBar); Destroy(gameObject); } } } else { aliveTime += Time.deltaTime; } if (statusBar) { //uiText.GetComponent<Text>().text = "Health: " + health + "\nAmmo: " + ((reloading) ? "RE.." : ammo.ToString()) + "\n" + agent.GetReward().ToString("0.000"); statusBar.GetComponent <StatusBar>().updateBar(health, ammo, maxAmmo[(int)currentWeapon], reloading); // Offset position above object bbox (in world space) float offsetPosX = transform.position.x + 0.75f; // Final position of marker above GO in world space Vector3 offsetPos = new Vector3(offsetPosX, transform.position.y, transform.position.z); // Calculate *screen* position (note, not a canvas/recttransform position) Vector2 canvasPos; Vector2 screenPoint = Camera.main.WorldToScreenPoint(offsetPos); // Convert screen position to Canvas / RectTransform space <- leave camera null if Screen Space Overlay RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, null, out canvasPos); // Set statusBar.transform.localPosition = canvasPos; } }
public void OnPointerClick(PointerEventData eventData) { RectTransformUtility.ScreenPointToLocalPointInRectangle(thisRectTransform, eventData.position, eventData.pressEventCamera, out swipeStart); }
private Vector2 GetUIPos(Vector2 viewPos) { pos = Vector2.zero; RectTransformUtility.ScreenPointToLocalPointInRectangle(this.bgRectTransform, viewPos, UICamera, out pos); return(pos - bgOffsetPos); }
void main_phase() { t_c++; if (t_c > 100000) { t_c = 0; } if (efe_d != null) { for (int i = 0; i < efe_d.Count; i++) { if (efe_d[i].c >= 1) { if (efe_d[i].text_efe == 0) { efe_d[i].fish.transform.position = new Vector2(efe_d[i].x, efe_d[i].y); if (Mathf.Abs(efe_d[i].x - efe_d[i].xn) > 0.005) { efe_d[i].x -= (efe_d[i].x - efe_d[i].xn) / efe_d[i].slow; } if (Mathf.Abs(efe_d[i].y - efe_d[i].yn) > 0.005) { efe_d[i].y -= (efe_d[i].y - efe_d[i].yn) / efe_d[i].slow; } if (Mathf.Abs(efe_d[i].x - efe_d[i].xn) <= 0.005 && Mathf.Abs(efe_d[i].y - efe_d[i].yn) <= 0.005) { } efe_d[i].fish.transform.localScale = new Vector2(efe_d[i].zx, efe_d[i].zy); efe_d[i].color.a = efe_d[i].alpha; //efe_d[i].fish.GetComponent<SpriteRenderer>().color = new Color(0.5f, 0.5f, 0.5f, efe_d[i].alpha); efe_d[i].fish.GetComponent <SpriteRenderer>().color = efe_d[i].color; efe_d[i].alpha -= 0.02f; efe_d[i].zx = efe_d[i].zx * 0.95f - 0.002f; efe_d[i].zy = efe_d[i].zy * 0.95f - 0.002f; if (efe_d[i].zx <= 0 || efe_d[i].alpha <= 0) { efe_dest(i); } } else //テキスト { efe_d[i].fish.transform.localScale = new Vector2(efe_d[i].zx, efe_d[i].zy); efe_d[i].color.a = efe_d[i].alpha; efe_d[i].fish.GetComponent <Text>().color = efe_d[i].color; var pos = Vector2.zero; var uiCamera = Camera.main; var worldCamera = Camera.main; var canvasRect = canvas_obj.GetComponent <RectTransform>(); var screenPos = RectTransformUtility.WorldToScreenPoint(worldCamera, new Vector2(efe_d[i].x, efe_d[i].y)); RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPos, uiCamera, out pos); efe_d[i].fish.GetComponent <RectTransform>().localPosition = pos; if (Mathf.Abs(efe_d[i].x - efe_d[i].xn) > 0.005) { efe_d[i].x -= (efe_d[i].x - efe_d[i].xn) / efe_d[i].slow; } if (Mathf.Abs(efe_d[i].y - efe_d[i].yn) > 0.005) { efe_d[i].y -= (efe_d[i].y - efe_d[i].yn) / efe_d[i].slow; } if (Mathf.Abs(efe_d[i].x - efe_d[i].xn) <= 0.005 && Mathf.Abs(efe_d[i].y - efe_d[i].yn) <= 0.005) { } efe_d[i].alpha -= 0.02f; //efe_d[i].zx = efe_d[i].zx * 0.95f - 0.002f; //efe_d[i].zy = efe_d[i].zy * 0.95f - 0.002f; if (efe_d[i].zx <= 0 || efe_d[i].alpha <= 0) { efe_dest(i); } } } } } }
void UpdateSlotItemPosition(PointerEventData eventData) { RectTransformUtility.ScreenPointToLocalPointInRectangle(OverlayRect, eventData.position, OverlayCamera, out position); inventoryItemHolder.rectTransform.localPosition = position; }
private void Update() { if (m_gameState.IsActionsMenuOpened(m_viewport.LocalPlayerIndex)) { return; } if (m_gameState.IsMenuOpened(m_viewport.LocalPlayerIndex)) { return; } if (m_gameState.IsPaused || m_gameState.IsPauseStateChanging) { return; } Transform camTransform = m_viewport.Camera.transform; if (camTransform.position != m_prevCamPos || camTransform.rotation != m_prevCamRot) { m_prevCamPos = camTransform.position; m_prevCamRot = camTransform.rotation; float angle = camTransform.eulerAngles.y; m_rtMapBounds.rotation = Quaternion.Euler(new Vector3(0, 0, angle)); ProjectCamera(m_rtMapBounds.rotation); } if (m_input.GetButtonDown(InputAction.Start, m_viewport.LocalPlayerIndex, false, false)) { m_gotFocus = !m_gotFocus; if (m_gotFocus) { m_eventSystem.SetSelectedGameObjectOnLateUpdate(m_selectableMinimap.gameObject); } else { m_cameraController.CenterVirtualMouse(); m_eventSystem.SetSelectedGameObjectOnLateUpdate(null); } } if (m_input.GetButtonDown(InputAction.LMB, m_viewport.LocalPlayerIndex, false, false)) { Vector2 pt; if (RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)m_rtMapBounds.parent, m_cameraController.VirtualMousePosition, null, out pt)) { float normalizedDistance = pt.magnitude / m_rootRadius; if (normalizedDistance <= 1) { m_mouseManipulation = true; m_virtualMousePostion = m_cameraController.VirtualMousePosition; Move(camTransform, true, 0, 0, 0, 0); } } } else if (m_input.GetButtonUp(InputAction.LMB, m_viewport.LocalPlayerIndex, false, false)) { m_mouseManipulation = false; } if (m_gotFocus || m_input.GetButton(InputAction.LB, m_viewport.LocalPlayerIndex, false, false) && IsCursorInMinimapBounds()) { if (m_input.GetButtonDown(InputAction.B, m_viewport.LocalPlayerIndex, false, false)) { m_gotFocus = false; m_cameraController.CenterVirtualMouse(); m_eventSystem.SetSelectedGameObjectOnLateUpdate(null); } else if (m_input.GetButtonDown(InputAction.LB, m_viewport.LocalPlayerIndex, false, false)) { m_virtualMousePostion = m_cameraController.VirtualMousePosition; Move(camTransform, true, 0, 0, 0, 0); } else { bool aPressed = m_input.GetButton(InputAction.A, m_viewport.LocalPlayerIndex, false, false); bool pivotPreciseMode = aPressed | m_input.GetButton(InputAction.RightStickButton, m_viewport.LocalPlayerIndex, false, false); float pivotMultiplier = 1; if (!pivotPreciseMode) { pivotMultiplier = 5; } float deltaY = m_input.GetAxisRaw(InputAction.MoveForward, m_viewport.LocalPlayerIndex, false, false) * Time.deltaTime * m_camCtrlSettings.MoveSensitivity * pivotMultiplier; float deltaX = m_input.GetAxisRaw(InputAction.MoveSide, m_viewport.LocalPlayerIndex, false, false) * Time.deltaTime * m_camCtrlSettings.MoveSensitivity * pivotMultiplier; bool cursorPreciseMode = aPressed | m_input.GetButton(InputAction.LeftStickButton, m_viewport.LocalPlayerIndex, false, false); float cursorMultiplier = 4; if (!cursorPreciseMode) { cursorMultiplier = 12; } float cursorY = m_input.GetAxisRaw(InputAction.CursorY, m_viewport.LocalPlayerIndex, false, false) * Time.deltaTime * m_camCtrlSettings.CursorSensitivity * cursorMultiplier; float cursorX = m_input.GetAxisRaw(InputAction.CursorX, m_viewport.LocalPlayerIndex, false, false) * Time.deltaTime * m_camCtrlSettings.CursorSensitivity * cursorMultiplier; Move(camTransform, false, deltaY, deltaX, cursorY, cursorX); } } if (m_mouseManipulation) { if (m_input.GetButton(InputAction.LMB, m_viewport.LocalPlayerIndex, false, false)) { float cursorMultiplier = 12; float cursorY = m_input.GetAxisRaw(InputAction.CursorY, m_viewport.LocalPlayerIndex, false, false) * Time.deltaTime * m_camCtrlSettings.CursorSensitivity * cursorMultiplier; float cursorX = m_input.GetAxisRaw(InputAction.CursorX, m_viewport.LocalPlayerIndex, false, false) * Time.deltaTime * m_camCtrlSettings.CursorSensitivity * cursorMultiplier; Move(camTransform, false, cursorY, cursorX, 0, 0); } } }
private void Move(Transform camTransform, bool forceSetMapPivot, float deltaY, float deltaX, float cursorY, float cursorX) { if (deltaX != 0 || deltaY != 0) { m_virtualMousePostion += new Vector2(deltaX, deltaY); cursorX = 0; cursorY = 0; } else { m_virtualMousePostion += new Vector2(cursorX, cursorY); } if (m_virtualMousePostion != m_prevCursor || forceSetMapPivot) { Vector2 pt; if (RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)m_rtMapBounds.parent, m_virtualMousePostion, null, out pt)) { float normalizedDistance = pt.magnitude / m_rootRadius; float angle = camTransform.eulerAngles.y; Vector3 dir = Quaternion.Euler(new Vector3(0, angle, 0)) * new Vector3(pt.x, 0, pt.y).normalized; if (normalizedDistance <= 1) { if (deltaX != 0 || deltaY != 0 || forceSetMapPivot) { m_cameraController.SetMapPivot(dir, normalizedDistance); } } else { if (deltaX != 0 || deltaY != 0 || forceSetMapPivot) { m_cameraController.SetMapPivot(dir, 1); ProjectCursorToMinimap(m_viewport.Camera.transform); m_virtualMousePostion = m_cameraController.VirtualMousePosition; } else { ((RectTransform)m_rtMapBounds.parent).GetWorldCorners(m_corners); Vector3 center = m_corners[1] + (m_corners[3] - m_corners[1]) / 2; Vector2 screenCenter = RectTransformUtility.WorldToScreenPoint(null, center); m_virtualMousePostion = screenCenter + pt.normalized * m_rootRadius; } } } else { if (deltaX != 0 || deltaY != 0) { m_virtualMousePostion -= new Vector2(deltaX, deltaY); } else { m_virtualMousePostion -= new Vector2(cursorX, cursorY); } } m_prevCursor = m_virtualMousePostion; } m_cameraController.VirtualMousePosition = m_virtualMousePostion; }
public void OnPointerDown(PointerEventData data) { this.originalPanelLocalPosition = this.panelRectTransform.anchoredPosition; RectTransformUtility.ScreenPointToLocalPointInRectangle(this.parentRectTransform, data.position, data.pressEventCamera, out this.originalLocalPointerPosition); }
public void OnPointerDown(PointerEventData data) { originalLocalPosition = transform.localPosition; originalLocalRotation = transform.localRotation; originalLocalScale = transform.localScale; RectTransformUtility.ScreenPointToLocalPointInRectangle(handRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition); }
private void OnInitializePotentialDrag(PointerEventData e) { //print("OnInitializePotentialDrag"); originalPanelLocalPosition = window.localPosition; RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)window.parent.transform, e.position, e.pressEventCamera, out originalLocalPointerPosition); }
public virtual void OnPointerDown(PointerEventData eventData) { transform.SetAsLastSibling(); RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, eventData.position, eventData.pressEventCamera, out pointInObject); }
public void Refresh() { Vector2 topLeft = Vector2.zero; Vector2 bottomRight = Vector2.zero; if (_passThroughRect != null) { Vector3[] corners = new Vector3[4]; _passThroughRect.GetWorldCorners( corners ); //Check for a world space camera if we have one var worldCamera = _parentCanvas.worldCamera; //OK for camera to be null here if canvas render mode is screen space overlay Vector3 topLeftScreenPoint = RectTransformUtility.WorldToScreenPoint(worldCamera, corners[0]); Vector3 bottomRightScreenPoint = RectTransformUtility.WorldToScreenPoint(worldCamera, corners[2]); if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(_blockerParent, topLeftScreenPoint, worldCamera, out topLeft)) { Debug.LogError("plane of the RectTransform was not hit"); return; } if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(_blockerParent, bottomRightScreenPoint, worldCamera, out bottomRight)) { Debug.LogError("plane of the RectTransform was not hit"); return; } if (_highlight != null) { _highlight.gameObject.SetActive(true); _highlight.pivot = new Vector2(0.5f,0.5f); _highlight.anchorMin = new Vector2(0.5f,0.5f); _highlight.anchorMax = new Vector2(0.5f,0.5f); var rect = _passThroughRect.rect; _highlight.sizeDelta = new Vector2(rect.width,rect.height); _highlight.anchoredPosition = (topLeft + bottomRight) * 0.5f; } } else if (_highlight != null) { _highlight.gameObject.SetActive(false); } _leftBlocker.anchorMin = Vector2.zero; _leftBlocker.anchorMax = new Vector2(0.5f,1f); _leftBlocker.sizeDelta = new Vector2( topLeft.x, 0 ); _bottomBlocker.anchorMin = new Vector2(0f,0f); _bottomBlocker.anchorMax = new Vector2(1f,0.5f); _bottomBlocker.sizeDelta = new Vector2( 0, topLeft.y ); _rightBlocker.anchorMin = new Vector2(0.5f,0f); _rightBlocker.anchorMax = new Vector2(1f,1f); _rightBlocker.sizeDelta = new Vector2( -bottomRight.x, 0 ); _topBlocker.anchorMin = new Vector2(0f,0.5f); _topBlocker.anchorMax = new Vector2(1f,1f); _topBlocker.sizeDelta = new Vector2( 0, -bottomRight.y ); }
public void OnBeginDrag(PointerEventData eventData) { panelRectTransform.SetAsLastSibling(); RectTransformUtility.ScreenPointToLocalPointInRectangle(panelRectTransform, eventData.position, eventData.pressEventCamera, out pointerOffset); }
public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) { _sprite = _image.sprite; var rectTransform = (RectTransform)transform; Vector2 localPositionPivotRelative; RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)transform, sp, eventCamera, out localPositionPivotRelative); // convert to bottom-left origin coordinates var localPosition = new Vector2(localPositionPivotRelative.x + rectTransform.pivot.x * rectTransform.rect.width, localPositionPivotRelative.y + rectTransform.pivot.y * rectTransform.rect.height); var spriteRect = _sprite.textureRect; var maskRect = rectTransform.rect; var x = 0; var y = 0; // convert to texture space switch (_image.type) { case Image.Type.Sliced: { var border = _sprite.border; // x slicing if (localPosition.x < border.x) { x = Mathf.FloorToInt(spriteRect.x + localPosition.x); } else if (localPosition.x > maskRect.width - border.z) { x = Mathf.FloorToInt(spriteRect.x + spriteRect.width - (maskRect.width - localPosition.x)); } else { x = Mathf.FloorToInt(spriteRect.x + border.x + ((localPosition.x - border.x) / (maskRect.width - border.x - border.z)) * (spriteRect.width - border.x - border.z)); } // y slicing if (localPosition.y < border.y) { y = Mathf.FloorToInt(spriteRect.y + localPosition.y); } else if (localPosition.y > maskRect.height - border.w) { y = Mathf.FloorToInt(spriteRect.y + spriteRect.height - (maskRect.height - localPosition.y)); } else { y = Mathf.FloorToInt(spriteRect.y + border.y + ((localPosition.y - border.y) / (maskRect.height - border.y - border.w)) * (spriteRect.height - border.y - border.w)); } } break; case Image.Type.Simple: default: { // conversion to uniform UV space x = Mathf.FloorToInt(spriteRect.x + spriteRect.width * localPosition.x / maskRect.width); y = Mathf.FloorToInt(spriteRect.y + spriteRect.height * localPosition.y / maskRect.height); } break; } // destroy component if texture import settings are wrong try { return(_sprite.texture.GetPixel(x, y).a > 0); } catch (UnityException e) { Debug.LogError("Mask texture not readable, set your sprite to Texture Type 'Advanced' and check 'Read/Write Enabled'"); Destroy(this); return(false); } }
public void OnPointerDown(PointerEventData data) { panelRectTransform.SetAsLastSibling(); RectTransformUtility.ScreenPointToLocalPointInRectangle(panelRectTransform, data.position, data.pressEventCamera, out pointerOffset); MainManager.Instance.isDraggingWindow = true; }
void Update() { boundingBox.transform.position = hips.position + virtualCenter; if (kinectManager.IsUserDetected()) { /* * We must find the position of the hand constrained to the bounding box * translate it into screen coordinates */ //print ("Bounding box size: "+boundingBox.size.x+","+boundingBox.size.y); //print ("Bounding box position: " + boundingBox.transform.position); //print ("Right hand position: " + rightHand.transform.position); Vector2 bottomLeft = new Vector2(boundingBox.transform.position.x - boundingBox.bounds.extents.x, boundingBox.transform.position.y - boundingBox.bounds.extents.y); Vector2 topRight = new Vector2(boundingBox.transform.position.x + boundingBox.bounds.extents.x, boundingBox.transform.position.y + boundingBox.bounds.extents.y); Vector2 handClamped = new Vector2(Mathf.Clamp(rightHand.transform.position.x, bottomLeft.x, topRight.x), Mathf.Clamp(rightHand.transform.position.y, bottomLeft.y, topRight.y)); bottomLeftCube.transform.position = new Vector3(bottomLeft.x, bottomLeft.y, boundingBox.transform.position.z); topRightCube.transform.position = new Vector3(topRight.x, topRight.y, boundingBox.transform.position.z); handClampedCube.transform.position = new Vector3(handClamped.x, handClamped.y, boundingBox.transform.position.z); // the hand has moved enough to update screen position (jitter control / smoothing) //if (Mathf.Abs (handClamped.x - xPrevious) > moveThreshold || Mathf.Abs (handClamped.y - yPrevious) > moveThreshold) { //handClamped += new Vector2 (boundingBox.bounds.extents.x, boundingBox.bounds.extents.y); //print ("Before:"+handClamped.x + " " + handClamped.y); float widthFactor = (Mathf.Abs(handClamped.x - bottomLeft.x) / (topRight.x - bottomLeft.x)); float heightFactor = (Mathf.Abs(handClamped.y - bottomLeft.y) / (topRight.y - bottomLeft.y)); // print ("Factored :"+widthFactor + " " + heightFactor); float xScaled = widthFactor * totalScreenWidth; // * (actualScreenWidth/totalScreenWidth); float yScaled = (1 - heightFactor) * totalScreenHeight; // * (actualScreenHeight/totalScreenHeight); //float xScaled = (rightHand.position.x - boundingBox.transform.position.x) / (boundingBox.size.x * 2) * totalScreenWidth; //float yScaled = (rightHand.position.y - boundingBox.transform.position.y) / (boundingBox.size.y * 2)* totalScreenHeight; // print ("Mouse Position: "+xScaled + " " + yScaled); float rate = 2f; POINT current = new POINT(); GetCursorPos(out current); // print ("Current Position:"+current.X+","+current.Y); // xScaled = Mathf.Lerp(current.X,xScaled,Time.deltaTime*rate); yScaled = Mathf.Lerp(current.Y, yScaled, Time.deltaTime * rate); //MouseControl.MouseMove (new Vector3 (totalScreenWidth*0.5f,totalScreenHeight*0.5f,0), new GUIText ()); SetCursorPos((int)xScaled, (int)yScaled); xPrevious = handClamped.x; yPrevious = handClamped.y; Vector2 pos; RectTransformUtility.ScreenPointToLocalPointInRectangle(myCanvas.transform as RectTransform, Input.mousePosition, myCanvas.worldCamera, out pos); //transform.position = myCanvas.transform.TransformPoint(pos); transform.position = myCanvas.transform.TransformPoint(pos); if (debugText != null) { debugText.text = "Relative: " + widthFactor + "," + heightFactor + '\n' + "Current: " + (int)xScaled + "," + (int)yScaled; } //UnityEngine.EventSystems.PointerEventData update = new UnityEngine.EventSystems.PointerEventData (eventSystem); //update.position = handCursor.transform.position; //transform.position = myCanvas.transform.TransformPoint (pos); // } if (Input.GetKeyDown(KeyCode.Space)) { MouseControl.MouseClick(); } if (Input.GetKeyDown(KeyCode.L)) { BeginClicking(); } if (Input.GetKeyDown(KeyCode.K)) { CancelClicking(); } // Rect rt = GUILayoutUtility.g (guestButton, GUIStyle.none); // if (rt.Contains(Event.current.mousePosition)){ // print (count); // count++; // } // print ("moving cursor"); /*Vector2 cursorPosition * TrackHandMovement (); * Vector2 handPosition = new Vector2 (RightHandX, RightHandY); * print (handPosition);*/ } else { //handCursor.transform.position = Vector3.zero; } }
bool ICanvasRaycastFilter.IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera) { if (alphaHitTestMinimumThreshold <= 0) { return(true); } if (alphaHitTestMinimumThreshold > 1) { return(false); } if (activeSprite == null) { return(true); } Vector2 local; if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPoint, eventCamera, out local)) { return(false); } Rect rect = GetPixelAdjustedRect(); // Convert to have lower left corner as reference point. local.x += rectTransform.pivot.x * rect.width; local.y += rectTransform.pivot.y * rect.height; Rect spriteRect = activeSprite.rect; Vector4 border = activeSprite.border; Vector4 adjustedBorder = GetAdjustedBorders(border / pixelsPerUnit, rect); for (int i = 0; i < 2; i++) { if (local[i] <= adjustedBorder[i]) { continue; } if (rect.size[i] - local[i] <= adjustedBorder[i + 2]) { local[i] -= (rect.size[i] - spriteRect.size[i]); continue; } float lerp = Mathf.InverseLerp(adjustedBorder[i], rect.size[i] - adjustedBorder[i + 2], local[i]); local[i] = Mathf.Lerp(border[i], spriteRect.size[i] - border[i + 2], lerp); } // Normalize local coordinates. Rect textureRect = activeSprite.textureRect; Vector2 normalized = new Vector2(local.x / textureRect.width, local.y / textureRect.height); // Convert to texture space. float x = Mathf.Lerp(textureRect.x, textureRect.xMax, normalized.x) / activeSprite.texture.width; float y = Mathf.Lerp(textureRect.y, textureRect.yMax, normalized.y) / activeSprite.texture.height; switch (m_FillDirection) { case FillDirection.Right: if (x > m_FillAmount) { return(false); } break; case FillDirection.Left: if (1f - x > m_FillAmount) { return(false); } break; case FillDirection.Up: if (y > m_FillAmount) { return(false); } break; case FillDirection.Down: if (1f - y > m_FillAmount) { return(false); } break; } try { return(activeSprite.texture.GetPixelBilinear(x, y).a >= alphaHitTestMinimumThreshold); } catch (UnityException e) { Debug.LogError("Using alphaHitTestMinimumThreshold greater than 0 on Image whose sprite texture cannot be read. " + e.Message + " Also make sure to disable sprite packing for this sprite.", this); return(true); } }
public void OnBeginDrag(PointerEventData eventData) { RectTransformUtility.ScreenPointToLocalPointInRectangle(root, eventData.position, eventData.pressEventCamera, out pointerOffset); }
public InventoryCellsGroup FindBestGroup() { if (this.m_MatchingGroups.Count == 0) { return(null); } Vector3 zero = Vector3.zero; RectTransform rectTransform = Inventory3DManager.Get().m_InventoryImage.rectTransform; Vector2 zero2 = Vector2.zero; if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, null, out zero2)) { return(null); } zero.x = CJTools.Math.GetProportionalClamp(0f, 1f, zero2.x, -rectTransform.sizeDelta.x * 0.5f, rectTransform.sizeDelta.x * 0.5f); zero.y = CJTools.Math.GetProportionalClamp(0f, 1f, zero2.y, -rectTransform.sizeDelta.y * 0.5f, rectTransform.sizeDelta.y * 0.5f); GameObject gameObject = null; Vector3 vector = Vector3.zero; Inventory3DManager.Get().m_Camera.ViewportPointToRay(zero); RaycastHit[] backpackHits = Inventory3DManager.Get().m_BackpackHits; for (int i = 0; i < Inventory3DManager.Get().m_BackpackHitsCnt; i++) { if (backpackHits[i].collider.gameObject.transform.parent == this.m_Grid.transform) { gameObject = backpackHits[i].collider.gameObject; vector = backpackHits[i].point; break; } } if (!gameObject) { return(null); } List <InventoryCellsGroup> list = new List <InventoryCellsGroup>(); for (int j = 0; j < this.m_MatchingGroups.Count; j++) { if (this.m_MatchingGroups[j].Contains(gameObject)) { list.Add(this.m_MatchingGroups[j]); } } if (list.Count == 0) { return(null); } InventoryCellsGroup result = null; float num = float.MaxValue; foreach (InventoryCellsGroup inventoryCellsGroup in list) { float num2 = vector.Distance(inventoryCellsGroup.m_CenterWorld); if (num2 < num) { result = inventoryCellsGroup; num = num2; } } return(result); }
public void OnDrag(BaseEventData eventData) { if (!enabledMove) { return; } PointerEventData pointerEventData = eventData as PointerEventData; //thumb.position = pointerEventData.position; //RectTransformUtility.WorldToScreenPoint(pointerEventData.enterEventCamera, pointerEventData.position); //把屏幕坐标转换成当前节点坐标 Vector2 pos; if (RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, pointerEventData.position, pointerEventData.enterEventCamera, out pos)) { thumb.anchoredPosition = pos; } /* * angle = Mathf.Acos(Vector3.Dot(transform.right.normalized, thumb.localPosition.normalized)) * Mathf.Rad2Deg; * Vector3 cross = Vector3.Cross(transform.right.normalized, thumb.localPosition.normalized); * angle = cross.z < 0 ? -angle : angle; * * distance = Vector3.Distance(Vector3.zero, thumb.localPosition); * //float angle = Mathf.Atan( thumb.localPosition.y / thumb.localPosition.x ) * Mathf.Rad2Deg; * if (target) * { * //相机和人物正方向的夹角 45f * target.eulerAngles = new Vector3(0, -angle + targetEulerAnglesY, 0); * //target.eulerAngles = new Vector3(0, -angle , 0); * } */ //angle = Mathf.Atan((transform.position.y - thumb.position.y) / (transform.position.x - thumb.position.x)) * Mathf.Rad2Deg; angle = Mathf.Atan((-thumb.anchoredPosition.y) / (-thumb.anchoredPosition.x)) * Mathf.Rad2Deg; //Debug.LogWarningFormat(" angle = {0} ", angle); if (thumb.anchoredPosition.x < 0 && thumb.anchoredPosition.y > 0 && angle < 0) { angle = 180f + angle; } if (thumb.anchoredPosition.x < 0 && thumb.anchoredPosition.y < 0 && angle > 0) { angle = -90f - (90f - angle); } distance = Vector3.Distance(Vector3.zero, thumb.anchoredPosition); //float distanceMax = 100; /* * if (distance > distanceMax) * { * if (pointerEventData.position.x < transform.position.x) * { * curThumbPosition.x = transform.position.x - distanceMax * Mathf.Cos((angle) * Mathf.Deg2Rad); * curThumbPosition.y = transform.position.y - distanceMax * Mathf.Sin((angle) * Mathf.Deg2Rad); * thumb.position = curThumbPosition; * } * else * { * curThumbPosition.x = transform.position.x + distanceMax * Mathf.Cos((angle) * Mathf.Deg2Rad); * curThumbPosition.y = transform.position.y + distanceMax * Mathf.Sin((angle) * Mathf.Deg2Rad); * thumb.position = curThumbPosition; * } * } */ if (target) { //相机和人物正方向的夹角 45f target.eulerAngles = new Vector3(0, -angle + targetEulerAnglesY, 0); //target.eulerAngles = new Vector3(0, -angle , 0); } PlayRun(); //Debug.LogWarning("angle = " + angle + ",cross = " + cross + ",distance = " + distance + " , move = " + move); }
public void OnPointerDown(PointerEventData data) { originalSizeDelta = panelRectTransform.sizeDelta; RectTransformUtility.ScreenPointToLocalPointInRectangle(panelRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition); }
public void OnPointerDown(PointerEventData data) //If you press on the Inventory { //panelRectTransform.SetAsLastSibling(); //the Inventory RectTransform is getting set as the last sibling RectTransformUtility.ScreenPointToLocalPointInRectangle( panelRectTransform, data.position, data.pressEventCamera, out pointerOffset); //and the pointeroffset is getting calculated }
// this event happens when there is a drag on screen public virtual void OnDrag(PointerEventData ped) { Vector2 localPoint = Vector2.zero; // resets the localPoint out parameter of the RectTransformUtility.ScreenPointToLocalPointInRectangle function on each drag event // if the point touched on the screen is within the background image of this joystick if (RectTransformUtility.ScreenPointToLocalPointInRectangle(bgImage.rectTransform, ped.position, ped.pressEventCamera, out localPoint)) { /* * bgImage.rectTransform.sizeDelta is the size of the background image of this joystick * Example: if the image size is 150 by 150 pixels on the screen * the length of x will measure from the Right side of the image to the Left side as -150 to 0 * the length of y will measure from the Bottom side of the image to to Top side as 0 to 150 * * localPoint is the point within the joystick's background image that was touched * * get a ratio, divide the local screen point touched within the image by the size of the image itself, in order to get the following values * localPoint.x becomes (from Left to Right -1 to 0) (-1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0) * localPoint.y becomes (from Bottom to Top 0 to 1) (0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1) */ localPoint.x = (localPoint.x / bgImage.rectTransform.sizeDelta.x); localPoint.y = (localPoint.y / bgImage.rectTransform.sizeDelta.y); /* * the correct x and y point values are created here * localPoint.x becomes (from Left to Right -1 to 1) (-1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1) * localPoint.y becomes (from Bottom to Top -1 to 1) (-1, -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1) * * the inputVector (that will be output as the direction vector to control movement of a game object such as character or any desired game object) is created from these values * the inputVector however is still unormalized, this means that if you drag outside of the boundaries of the image the values * will continue to increase, also, dragging outside of the circular part of the image onto its transparent area will still register, * so that dragging to a corner of the image (for example the bottom left corner) will give a value of x,y (-1,-1) which is not correct because the * the circular part of the image does not actually reach that far * * the solution to this (further down this script) will be to normalize the vector (remove it's magnitude) so that its value of x or y never increases past 1 */ inputVector = new Vector3(localPoint.x * 2 + 1, localPoint.y * 2 - 1, 0); // before we normalize, we will save this unnormalized vector in order to move the joystick along with our drag unNormalizedInput = inputVector; inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector; // normalizes the vector, this will be used to ouput to a game object controller to control movement (for example, of a player character or any desired game object) // moves the joystick handle "knob" image joystickKnobImage.rectTransform.anchoredPosition = new Vector3(inputVector.x * (bgImage.rectTransform.sizeDelta.x / joystickHandleDistance), inputVector.y * (bgImage.rectTransform.sizeDelta.y / joystickHandleDistance)); // if the joystick is not set to stay in a fixed position if (joystickStaysInFixedPosition == false) { // if dragging outside the circle of the background image if (unNormalizedInput.magnitude > inputVector.magnitude) { var currentPosition = bgImage.rectTransform.position; currentPosition.x += ped.delta.x; currentPosition.y += ped.delta.y; // keeps the joystick within the screen currentPosition.x = Mathf.Clamp(currentPosition.x, 0 + bgImage.rectTransform.sizeDelta.x, Screen.width); currentPosition.y = Mathf.Clamp(currentPosition.y, 0, Screen.height - bgImage.rectTransform.sizeDelta.y); // moves the entire joystick along with the drag bgImage.rectTransform.position = currentPosition; } } } }
// mainloop IEnumerator TrackPointer() { var ray = GetComponentInParent <GraphicRaycaster>(); var input = FindObjectOfType <StandaloneInputModule>(); var input_holo = FindObjectOfType <HoloLensInputModule>(); var text = GetComponentInChildren <Text>(); if (ray != null && input != null) { while (Application.isPlaying) { // TODO: if mousebutton down if (GestureStarted) { Vector2 CurrentLocalPosition; // Mouse or gesture position RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, HandPosition2D, null, out CurrentLocalPosition); // local pos is the mouse position. CurrentLocalPosition.x = CurrentLocalPosition.x / (150); CurrentLocalPosition.y = CurrentLocalPosition.y / (150); // Debug.Log("CurrentLocalPosition.x=" + CurrentLocalPosition.x); // Debug.Log("CurrentLocalPosition.y=" + CurrentLocalPosition.y); float angle = (Mathf.Atan2(-CurrentLocalPosition.y, CurrentLocalPosition.x) * 180f / Mathf.PI + 180f) / 360f; GetComponent <Image>().fillAmount = angle; var temAng = ((angle) * 360f); //text.text = ((int)((angle)*360f )).ToString(); if (CurrentLocalPosition.magnitude < (0.35f * thisRect.rect.width)) { throttle_rect.anchoredPosition = CurrentLocalPosition; } else { throttle_rect.anchoredPosition = CurrentLocalPosition.normalized * (0.35f * thisRect.rect.width); // Debug.Log("CurrentLocalPosition.magnitude =" + CurrentLocalPosition.magnitude); } ; float CurrentLocalPositionMagnitudeScaler = 0.2f; rad = (CurrentLocalPosition.magnitude * CurrentLocalPositionMagnitudeScaler) / (0.5f * thisRect.rect.width); float tmpRad = (CurrentLocalPosition.magnitude) / (0.5f * thisRect.rect.width); this.SetAngRad(temAng, tmpRad); } //itsKGFOrbitCam.SetPanningEnable( !isPointerDown ); yield return(0); } } else { UnityEngine.Debug.LogWarning("Could not find GraphicRaycaster and/or StandaloneInputModule"); } }
void Update() { if (!draging) { return; } //如果鼠标按下 if (_dragType == DragType.GameObject && Input.GetMouseButtonDown(0)) { //获取碰撞信息 var hit = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, testLayer); //如果点在某个物体上 if (hit.Length >= 1) { //如果点击的物体是自己,可以开始拖拽! if (hit[0].transform == this.transform && !draging) { ToStartDrag(); } } } lastGameObject_obj = GetCurrentTarget(); if (_dragType == DragType.GameObject && Input.GetMouseButtonUp(0)) { ToEndDrag(); } else { // print("OnDraging: " + name); #region 判断是否进入某些物体 if (lastGameObject_obj) { eventOnEnter?.Invoke(lastGameObject_obj); } #endregion switch (_dragType) { case DragType.GameObject: var pos = transform.position; var mouseToPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); this.transform.position = new Vector3(mouseToPoint.x, mouseToPoint.y, pos.z); break; case DragType.Ui: var localPoint = Vector2.zero; if (RectTransformUtility.ScreenPointToLocalPointInRectangle( parentRect, Input.mousePosition, GlobalVar.CanvasComponent.worldCamera, out localPoint)) { var parentRectSizeDelta = parentRect.sizeDelta; selfRect.anchoredPosition = offset + localPoint + new Vector2(parentRectSizeDelta.x / 2, -parentRectSizeDelta.y / 2); } break; } eventOnDrag?.Invoke(); } }
private void GetTouchDirectionDown(Vector2 position, Camera cam) { Vector2 localPoint; RectTransformUtility.ScreenPointToLocalPointInRectangle(cachedRectTransform, position, cam, out localPoint); Vector2 buttonSize = this.rectTransform().sizeDelta / 3f; tmpAxis = Vector2.zero; if (localPoint.y >= -buttonSize.y / 2f && localPoint.y <= buttonSize.y / 2f && localPoint.x >= -buttonSize.x / 2f && localPoint.x <= buttonSize.x / 2f) { axisCenter.ResetAxis(); axisCenter.axisState = ETCAxis.AxisState.Down; } Vector2 LeftUp = new Vector2(-this.rectTransform().sizeDelta.x / 2f, this.rectTransform().sizeDelta.y / 2f); Vector2 LeftDown = new Vector2(-this.rectTransform().sizeDelta.x / 2f, -this.rectTransform().sizeDelta.y / 2f); Vector2 RightUp = new Vector2(this.rectTransform().sizeDelta.x / 2f, this.rectTransform().sizeDelta.y / 2f); Vector2 RightDown = new Vector2(this.rectTransform().sizeDelta.x / 2f, -this.rectTransform().sizeDelta.y / 2f); if ((localPoint.x < -buttonSize.x / 2f && Vector2.Dot(localPoint, LeftUp) > 0 && Vector2.Dot(localPoint, LeftDown) > 0 && dPadAxisCount == DPadAxis.Two_Axis) || (dPadAxisCount == DPadAxis.Four_Axis && localPoint.x < -buttonSize.x / 2f)) { tmpAxis.x = -1; } // right if ((localPoint.x > buttonSize.x / 2f && Vector2.Dot(localPoint, RightUp) > 0 && Vector2.Dot(localPoint, RightDown) > 0 && dPadAxisCount == DPadAxis.Two_Axis) || (dPadAxisCount == DPadAxis.Four_Axis && localPoint.x > buttonSize.x / 2f)) { tmpAxis.x = 1; } // Up if ((localPoint.y > buttonSize.y / 2f && Vector2.Dot(localPoint, LeftUp) > 0 && Vector2.Dot(localPoint, RightUp) > 0 && dPadAxisCount == DPadAxis.Two_Axis) || (dPadAxisCount == DPadAxis.Four_Axis && localPoint.y > buttonSize.y / 2f)) { tmpAxis.y = 1; } // Down if ((localPoint.y < -buttonSize.y / 2f && Vector2.Dot(localPoint, LeftDown) > 0 && Vector2.Dot(localPoint, RightDown) > 0 && dPadAxisCount == DPadAxis.Two_Axis) || (dPadAxisCount == DPadAxis.Four_Axis && localPoint.y < -buttonSize.y / 2f)) { tmpAxis.y = -1; } // Left // if ( (localPoint.x < -buttonSize.x/2f && localPoint.y > -buttonSize.y/2f && localPoint.y< buttonSize.y/2f && dPadAxisCount== DPadAxis.Two_Axis) // || (dPadAxisCount== DPadAxis.Four_Axis && localPoint.x < -buttonSize.x/2f) ){ // tmpAxis.x = -1; //} //// right //if ( (localPoint.x > buttonSize.x/2f && localPoint.y> -buttonSize.y/2f && localPoint.y< buttonSize.y/2f && dPadAxisCount== DPadAxis.Two_Axis) // || (dPadAxisCount== DPadAxis.Four_Axis && localPoint.x > buttonSize.x/2f) ){ // tmpAxis.x = 1; //} //// Up //if ( (localPoint.y > buttonSize.y/2f && localPoint.x>-buttonSize.x/2f && localPoint.x<buttonSize.x/2f && dPadAxisCount == DPadAxis.Two_Axis) // || (dPadAxisCount== DPadAxis.Four_Axis && localPoint.y > buttonSize.y/2f) ){ // tmpAxis.y = 1; //} //// Down //if ( (localPoint.y < -buttonSize.y/2f && localPoint.x>-buttonSize.x/2f && localPoint.x<buttonSize.x/2f && dPadAxisCount == DPadAxis.Two_Axis) // || (dPadAxisCount== DPadAxis.Four_Axis && localPoint.y <- buttonSize.y/2f) ){ // tmpAxis.y = -1; //} }