• 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 quynn96 · Feb 01, 2019 at 02:23 AM · drag-and-drop

Drag and drop items to many box available

I'm being to create mini game. Request: drag items to object by corresponding number and then drop enough items like request, it will message: You Win! For example: if request 2, so you drag 2 items then drop 2 items to bellow object p/s: item in middle like picture must be more items, depending on requirements of program. do you scripts? I want to refer alt text

capture.png (28.6 kB)
Comment
Add comment
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

1 Reply

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

Answer by toddisarockstar · Feb 01, 2019 at 06:12 AM

Here is a simple example to get you started:

 using UnityEngine;
 using System.Collections;
 
 public class drop : MonoBehaviour {
 
     public class box { public Rect pos; public bool off;}
     public Texture2D background;
     public string answer;
     public box[] picks;
     public box[] drops;
     public int xidx;
     public int idx{get{return xidx;}set{
             if(value==-1&&xidx>-1){check(xidx);}xidx=value;}}
     public string[] dropnames = new string[]{"banannas", "carrots","peanuts","beer"};
     public string[] picknames = new string[]{"monkeys", "squirels","rabbits","grandmas"};
 
     public float sx,sy,mx,my,rx,ry;
     void Start () {
         background = new Texture2D (1, 1);
         sx = Screen.width;
         sy = Screen.height;
         int i = picknames.Length;
         picks  = new box[i];
         float xp = Screen.width * .2f;
         while(i>0){i--;
             picks[i]=new box();
             picks[i].pos = new Rect(xp,sy*.3f,sx*.12f,sy*.09f);
             xp+=sx*.15f;}    
         i = dropnames.Length;
         drops  = new box[i];
         xp = Screen.width * .2f;
         while(i>0){i--;
             drops[i]=new box();
             drops[i].pos = new Rect(xp,sy*.6f,sx*.12f,sy*.09f);
             xp+=sx*.15f;}}
     
     void OnGUI(){int i;
         GUI.Label (new Rect(0, 0, sx, sy * .2f), answer);
         i = drops.Length;
         while (i>0) {i--;
             if(!drops[i].off){GUI.DrawTexture(drops[i].pos,background);
                 GUI.Label(drops[i].pos,dropnames[i]);}}
         i = picks.Length;
         while (i>0) {i--;
             if(!picks[i].off){
             GUI.DrawTexture(picks[i].pos,background);
                 GUI.Label(picks[i].pos,picknames[i]);}}
     }
     void Update () {int i;
         mx = Input.mousePosition.x;
         my = sy-Input.mousePosition.y;
         if (Input.GetMouseButton(0)) {
             if (Input.GetMouseButtonDown (0)) {
                 i = picks.Length;while(i>0){i--;
                 if(!picks[i].off&&picks[i].pos.Contains(new Vector3(mx,my,0))){
 
                         rx=picks[i].pos.x-mx;ry=picks[i].pos.y-my;
                         print ("dragging:"+picknames[i]);idx = i;break;}
                 }}}else{idx=-1;}if (idx > -1) {
             picks[idx].pos = new Rect(mx+rx,my+ry,picks[idx].pos.width,picks[idx].pos.height);}    
      }
         void check(int v){
         int i = drops.Length;
         while(i>0){i--;if(drops[i].pos.Contains(new Vector3(mx,my,0))){
                 picks[v].off = true;
                 answer = "User thinks that "+picknames[v]+" eat "+dropnames[i];print(answer);break; }
         }}
 }
 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

99 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Click and drag an object 3 Answers

drag and drop into other cells 1 Answer

Can we limit game object within specific Collider/Colliders? 0 Answers

Drag And Drop não funciona 1 Answer

Move UI image on top of other elements 0 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