- Home /
How can I spawn and drag from a button click?
I'm trying to make a button to spawn robots where you hold down on the button, instantiate, and be able to drag from there to where you want it. The problem is in this script it only instantiates and drops the robots directly below it.
if (GUI.Button (setPositionAndSize(sbposx, sbposy, sbbuttonw, sbbuttonh), SwatbotTexture, ""))
{
print(Input.mousePosition);
Vector2 p = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
print(p);
print(p.x);
print(p.y);
Instantiate(playerBots[0],new Vector2(p.x,p.y),Quaternion.identity);
}
Waiting for my answer to be approved by a moderator... anyways, here you go:
Capture the position of the mouse after the mouse is up.
Spawn the robot to the location.
Answer by robertbu · May 12, 2014 at 06:02 AM
GUI.Button() execute on the mouse up, so your current code is creating the object as the button is released. You need it to capture the mouse down. A way to do that is to use the current GUI event and check for the MouseDown event. It is kinda ugly to put the dragging code inside of OnGUI()...partly since the function gets called multiple times per frame. So here is some example code. I recommend you test it first on an new scene then take the logic and integrate it back into your code. To use:
Start a new scene
Create an empty game object
Drag and drop a prefab on the 'prefab' variable
Hit run
pragma strict
public var prefab : Transform;
private var spawn : Transform; private var rect = Rect(0,0,100,50);
function Update() {
if (Input.GetMouseButton(0) && spawn != null) { var pos = Input.mousePosition; pos.z = -Camera.main.transform.position.z; spawn.transform.position = Camera.main.ScreenToWorldPoint(pos); } if (Input.GetMouseButtonUp(0)) { spawn = null; } } function OnGUI() { var e = Event.current; if (e.type == EventType.MouseDown && rect.Contains(e.mousePosition)) { var pos = Input.mousePosition; pos.z = -Camera.main.transform.position.z; pos = Camera.main.ScreenToWorldPoint(pos); spawn = Instantiate(prefab, pos, Quaternion.identity); } GUI.Button (rect, "Button"); }
Note that we are not using the button as a button here...the call just displays the button. If you like, you can replace it with a GUI.DrawTexture to draw the texture of your choice. Your code creates the object with the 'z' position at 0.0, but does it as a side-effect. Your world position code would also require an Orthographic camera. I changed the position code so that it will work for both an Orthographic and Perspective camera.
thanks for the code, I'm trying it out now but I'm having a little trouble converting it to c#. the pos.z turns always turns red, can't seem to find a way to do it right. I did it this way but the pos.z always gives me an error
public Transform prefab;
public Texture SwatbotTexture;
private Transform spawn;
void Update() {
if (Input.Get$$anonymous$$ouseButton(0) && spawn != null) {
int pos = Input.mousePosition;
pos.z = -Camera.main.transform.position.z;
spawn.transform.position = Camera.main.ScreenToWorldPoint(pos);
}
if (Input.Get$$anonymous$$ouseButtonUp(0)) {
spawn = null;
}
}
Rect setPositionAndSize(float x, float y, float w, float h)
{
x = 0;
y = 0;
w = 100;
h = 50;
return new Rect(x,y,w,h);
}
void OnGUI() {
var e = Event.current;
if (e.type == EventType.$$anonymous$$ouseDown && rect.Contains(e.mousePosition)) {
int pos = Input.mousePosition;
pos.z = -Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
spawn = Instantiate(prefab, pos, Quaternion.identity);
}
GUI.Button (setPositionAndSize, SwatbotTexture, "");
}
Here is the C# translation:
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour {
public Transform prefab ;
private Transform spawn;
private Rect rect = new Rect(0,0,100,50);
void Update() {
if (Input.Get$$anonymous$$ouseButton(0) && spawn != null) {
var pos = Input.mousePosition;
pos.z = -Camera.main.transform.position.z;
spawn.transform.position = Camera.main.ScreenToWorldPoint(pos);
}
if (Input.Get$$anonymous$$ouseButtonUp(0)) {
spawn = null;
}
}
void OnGUI() {
Event e = Event.current;
if (e.type == EventType.$$anonymous$$ouseDown && rect.Contains(e.mousePosition)) {
var pos = Input.mousePosition;
pos.z = -Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
spawn = Instantiate(prefab, pos, Quaternion.identity) as Transform;
}
GUI.Button (rect, "Button");
}
Got it to work! I think I understand the logic of the code too. Thanks! Really appreciate it!
Answer by DylanWilson · May 12, 2014 at 06:43 AM
Capture the position of the mouse after the mouse is up.
Spawn the robot to the location.
if(GUI.Button(setPositionAndSize(sbposx, sbposy, sbbuttonw, sbbuttonh), SwatbotTexture, "") { if(Input.GetMouseButtonUp(1)) { Vector2 spawnPoint = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y)); Instantiate(playerBots[0], spawnPoint, Quaternion.identity); } }
I tried your code but it doesn't spawn anymore. Nothing comes out when i click it or try to drag and drop bots.