- Home /
Limit the selection box to not go inside the UI
So I make the selection box and as you can see in the picture I wanted the box to stay within the void area and not going into the UI on the right. Also, I wanted the selection to stay in place once drag out and not cancel itself when I move the slider around.
Here is what my script looks like
public class mousescript : MonoBehaviour {
[SerializeField]
private RectTransform selectSquareImage;
Vector3 startPos;
Vector3 endPos;
// Use this for initialization
void Start () {
selectSquareImage.gameObject.SetActive (false);
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
{
startPos = hit.point;
}
}
if (Input.GetMouseButton(0))
{
if (!selectSquareImage.gameObject.activeInHierarchy)
{
selectSquareImage.gameObject.SetActive(true);
}
endPos = Input.mousePosition;
Vector3 squareStart = Camera.main.WorldToScreenPoint(startPos);
squareStart.z = 0f;
Vector3 centre = (squareStart + endPos) / 2f;
selectSquareImage.position = centre;
float sizeX = Mathf.Abs(squareStart.x - endPos.x);
float sizeY = Mathf.Abs(squareStart.y - endPos.y);
selectSquareImage.sizeDelta = new Vector2 (sizeX, sizeY);
}
}
}
asfasg.jpg
(43.1 kB)
Comment
Your answer
Follow this Question
Related Questions
How do disable uGUI/EventSystem selection feature? 0 Answers
Simple text GUI 2 Answers
Problem with GUI element following enemy 0 Answers
UI Slider - making the touch area for slide larger than the UI image handle 2 Answers
GameObject with SpriteRenderer either on top of everything or not showing 0 Answers