Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gimoj · May 12, 2014 at 05:15 AM · 2dinstantiatebuttonspawnmouse-drag

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);
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DylanWilson · May 12, 2014 at 05:28 AM 0
Share

Waiting for my answer to be approved by a moderator... anyways, here you go:

  1. Capture the position of the mouse after the mouse is up.

  2. Spawn the robot to the location.

http://codepaste.net/p6ec9y

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gimoj · May 12, 2014 at 08:44 AM 0
Share

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, "");
 }
avatar image robertbu · May 12, 2014 at 08:49 AM 0
Share

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");
     }
 
avatar image gimoj · May 12, 2014 at 09:18 AM 0
Share

Got it to work! I think I understand the logic of the code too. Thanks! Really appreciate it!

avatar image
0

Answer by DylanWilson · May 12, 2014 at 06:43 AM

  1. Capture the position of the mouse after the mouse is up.

  2. 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); } }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gimoj · May 12, 2014 at 08:45 AM 0
Share

I tried your code but it doesn't spawn anymore. Nothing comes out when i click it or try to drag and drop bots.

avatar image
0

Answer by nextage575 · Dec 31, 2019 at 10:42 AM

@gimoj how can i do this with with canvas button?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to spawn 2 events with a score gap..? 0 Answers

Spawn Random Enemies 2D 1 Answer

Changing button positions on different resolutions 1 Answer

Multiple Prefab Instantiation, Camera, Button and Scroll View Question 1 Answer

Object instantiates on top of the previous object 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges