Sorry, my english is bad…
Hello, are interested in this question, how do I move GUI.Box (2D) using a drag drop? Google, which is not found suitable, please tell me …
For more information:
I appears when you click GUI.BOX, I need to somehow move this box, tried to do so
function OnMouseDown () {
var screenSpace = Camera.main.WorldToScreenPoint(transform.position);
var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
while (Input.GetMouseButton(0))
{
var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
var curPosition =
Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
transform.position = curPosition;
yield;
}
}
....
if(usy1)
{
GUILayout.Box(tex, customGuiStyle);
OnMouseDown ();
}
Did not work, try just a script to connect to the object in the editor, worked but not what I wanted … Push on the little idea …
The GUI element needs to be a repeat button, so that if the mouse is clicking it it will return true. When it returns true you can then set the position of the button to the position of the mouse. Note if you don’t want the button to look like a button but look like a box you can specify how it is to look by using GUI styles.
Here is some code that I put together:
using UnityEngine;
using System.Collections;
public class DragGUI : MonoBehaviour
{
/// <summary>
/// A Rect object that specifies the position, width, and height of the btn
/// </summary>
public Rect btnPosition = new Rect(0,0,100,100);
/// <summary>
/// A GUIContent object that specifies the content that the button will display
/// </summary>
public GUIContent btnContent = new GUIContent();
//this is where GUI code goes
void OnGUI()
{
//Draw the button
if (GUI.RepeatButton(btnPosition, btnContent)) // if the button is clicked
{
//set btnPosition.center to the mouse position of the current event
btnPosition.center = Event.current.mousePosition;
}
}
}
Just put it on your camera, it should work. By the way the code is in C#.