I’m making buttons on the side of a window and resizing with with script if button is pressed it has to resize and it kinda works but the window edge doesn’t go exactly with my mouse it goes more like correct than it’s off allot.
this is how I was doing it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class WindowResize : MonoBehaviour, IBeginDragHandler, IDragHandler
{
public Transform window;
public bool vertical = false;
public bool horizontal = false;
private Vector2 startPosition;
public void OnBeginDrag(PointerEventData eventData)
{
startPosition = window.localScale;
}
public void OnDrag(PointerEventData eventData)
{
if (horizontal)
{
startPosition.x -= eventData.delta.x * 0.007f; // the factor that resized my window correctly at first but later mouse becomes too slow
window.localScale = startPosition;
}
}
}
You probably need to convert between whatever coordinate space the mouse comes in at (I think it is pixels? Check the docs…) to whatever coordinate space these other items exist in.
If these are RectTransforms, the RectTransformUtility class might come in handy.
It would also be dependent on the settings of the CanvasScaler, obviously.
This approach can also help you reason about the numeric ranges involved:
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);
If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.
You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.
You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
You must find a way to get the information you need in order to reason about what the problem is.
thank you very much for the infos I was able to dig deep and figured out the resize the left x and resize width along it so that it resizes only left border
this is my code that works only you need to attach this script to a button on a left border on a window
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class WindowResize : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
{
public RectTransform m_Parent;
public RectTransform m_Window;
public Canvas m_Canvas;
public Camera m_Camera;
public bool m_WindowMove = false;
private float m_Width;
private Vector2 m_StartPoint;
private float m_StartWidth;
public void OnPointerDown(PointerEventData eventData)
{
m_Width = m_Window.rect.width;
RectTransformUtility.ScreenPointToLocalPointInRectangle(m_Parent, Input.mousePosition, m_Canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : m_Camera, out m_StartPoint);
m_StartPoint.x -= m_Window.anchoredPosition.x;
m_StartPoint.y = m_Window.anchoredPosition.y * -1;
m_StartWidth = m_Window.anchoredPosition.x;
m_WindowMove = true;
}
public void OnPointerUp(PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Left)
{
m_WindowMove = false;
}
}
public void OnDrag(PointerEventData eventData)
{
Vector2 anchoredPos;
//resize
RectTransformUtility.ScreenPointToLocalPointInRectangle(m_Parent, Input.mousePosition, m_Canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : m_Camera, out anchoredPos);
m_Window.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, -anchoredPos.x + m_Width + m_StartWidth + m_StartPoint.x);
//move left border
RectTransformUtility.ScreenPointToLocalPointInRectangle(m_Parent, Input.mousePosition, m_Canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : m_Camera, out anchoredPos);
anchoredPos.y = 0;
m_Window.anchoredPosition = anchoredPos - m_StartPoint;
}
}