- Home /
How can I resize an image with drag-and-drop?
Before attempting to post this question here, I have spent the past day trying to create a method of resizing an image for my virtual "OS" desktop. The end goal was for me to have resizable windows, but I have seemingly exhausted every possible option that I could think of.
At this point I have a square image with colliders around the outer edges, but I can't seem to figure out how to properly tell if they are being clicked, let alone dragged.
If anyone has any ideas, I would be very grateful. I try to not ask questions on here if I don't have to, as I don't want to clog it up, but I have legitimately no idea how to proceed.
A few details that could use clarification:
You mention that you're making a "virtual OS desktop", and that you're having trouble "resizing an image".
What exactly are you referring to when you say "image" in this context? Do you actually mean an image, as-is, or do you mean something like resizing a window in Windows, for example?
Is all of this done using a (one/multiple) Canvas(es)? Is it just floating GameObjects?
There's a lot of missing context, so the more information you can provide about what you're trying to do and how you've tried to do it, the better.
Answer by GeroNL · Aug 27, 2021 at 04:08 AM
Hello, did you mean one handed resizing?
i never do this, but i will giving my idea, onbtndown you save the pos, i wil say it StartPos vector2, and on drag you will measure current pos(click draged) in ondragbtn with StartPos, if the CurPos.x > StartPos ||CurPos.x < StartPos then do resizing of image(you calculated it with your value, you can resizing per value different or divide it to get good resizing) and so on to the y value. and up to let it not resizing anymore.
Hope it help,.
My issue is that, first of all, I can't resize the image via code, as far as I know. Second of all, I don't know how to detect when the player is clicking any of the 4 BoxCollider2Ds that I have on there.
Also, what exactly do you mean by "one-handed resizing"?
Thank you for your help, by the way, I appreciate it. :)
well , on handed resizing cause you resizing something with one hand(in smart phone you can do scaling with two fingers, well should a must say 1 finger resizing, lol, i meshed up)/ by clicking if on pc.
why you cant resize the image?
// this resizing your btn in btndown
yourtBtn.gameObject.transform.localScale *= 0.75f;
Resize the game object that on it, all in hierarchy is Gameobject.
To detect 2dRay form touch(if pc by clicking) do like this :
//ONupdate
Ray ray = new Ray();
RaycastHit hit;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if(hit == yourimage that have collider)
{
//do something
}
}
do something like tht.
to get first pos you can do it on btndown, your btn down must be have parameter like "PointerEventData eventData", eventData have position, you can use it. onDrag will have too.
You sir, are a genius. You managed to give me an idea to break the mold. So far it's working, but I'll mark it as the solution as I just need to work out a few mathematical kinks and personal errors that happen because me dumb :)
For anyone who reads this, who has the same problem, I did this with buttons around the image.
There are 8 buttons, of which clicking and dragging the top, bottom, left, or right buttons control one axis of stretch, and clicking and dragging any corner offers dual axis control.
Your answer
Follow this Question
Related Questions
Resizing GUI position with screen size? 0 Answers
Is it possible to drag UI item from ScrollRect to elsewhere without mask blocking item? 1 Answer
Screen space camera makes unity UI object loose track of mouse. 2 Answers
Help Changing GUI Text size 0 Answers
Open a popup window with text and images when clicking on an object - please help! 3 Answers