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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by prototype7 · Sep 14, 2012 at 04:44 AM · 2dguimovement

How to Convert each 2D Array to GameObject

How to Convert each 2D Array to GameObject so i can transform its position (drag each card )

 using UnityEngine;
 using System.Collections;
 
 public class test : MonoBehaviour {
     
     private int cols = 4;
     private int rows = 4;
     private Cards[,] aGrid;
     private bool gameStarted = false;
     [SerializeField]
     private Texture blue;
     private GameObject[] myGO;
     
     
     void Start () {
         aGrid = new Cards[cols,rows];
         
         for(int i=0; i<rows; i++)
         {
             for(int j=0; j<cols; j++)
             {
                 Cards myCard = new Cards();
                 myCard.position.x = j * 50;
                 myCard.position.y = i * 50;
                 aGrid[j,i] = myCard;
             }
         }
         gameStarted = true;
     }
     
     void OnGUI(){
         if(gameStarted)
         {
             for(int i=0; i<rows; i++)
             {
                 for(int j=0; j<cols; j++)
                 {
                     Cards myCard = aGrid[j,i];
                     GUI.DrawTexture(new Rect(myCard.position.x, myCard.position.y, 50, 50), blue);
                     /*I try this but doesn't work*/
                     //myGO = new GameObject[j,i];
                       //if(Input.GetMouseButton(0)){
              //myGO[j,i].transform.Translate(Input.GetAxisRaw("Mouse X") * Time.smoothDeltaTime * 12, 0,
                 //Input.GetAxisRaw("Mouse Y") * //Time.smoothDeltaTime * 12);
         }
                 }
             }
         }
     }
 }
 
 public class Cards {
     public Vector2 position;
 }


[1]: /storage/temp/3479-scene.png

scene.png (7.1 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

2 Replies

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

Answer by prototype7 · Dec 20, 2012 at 12:53 AM

 using UnityEngine;
 using System.Collections;
 
 public class test : MonoBehaviour {
 
     private int cols = 4;
     private int rows = 4;
     private Cards[,] aGrid;
     private GameObject[,] GOarray;
     private bool gameStarted = false;
     /*card is a Prefab that i have to set to be seen to cam, suppose it scale x=1,y=1*/
     [SerializeField]
     private GameObject card;
     private GameObject[] myGO;
 
 
     void Awake () {
        aGrid = new Cards[cols,rows];
 
        for(int i=0; i<rows; i++)
        {
          for(int j=0; j<cols; j++)
          {
           Cards myCard = new Cards();
           myCard.position.x = j * 1.1f;
           myCard.position.y = i * 1.1f;
           aGrid[j,i] = myCard;
           GOarray = new GameObject[cols, rows];
          }
        }
        gameStarted = true;
     }
 
     void Start(){
        if(gameStarted)
        {
          for(int i=0; i<rows; i++)
          {
           for(int j=0; j<cols; j++)
           {
               Cards myCard = aGrid[j,i];
               GameObject cardMember = GameObject.Instantiate(card, new Vector3(myCard.position.x, myCard.position.y), Quaternion.identity) as GameObject;
               cardMember.transform.localScale = new Vector3(0.5f,0.5f,0.5f);
               GOarray[j,i] = cardMember;
                }
           }
          }
        }
  }
 
 
 public class Cards {
     public Vector2 position;
 }
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
avatar image
0

Answer by whydoidoit · Sep 14, 2012 at 09:02 AM

You need to think about this in a different way:

  • Either - make your playing cards a Plane with a texture on it. Then you can attach this plane to a GameObject with a MeshFilter and a MeshRenderer and then use the OnMouseDown and other events to transform its position. This is very different to the OnGUI approach you have now.

  • Or - if you want to use OnGUI then you need to write your own handling for the position of the cursor and the state of the drag. OnGUI is called with different events (available from Event.current) for the user clicking and moving the mouse as well as drawing the display. You would need to detect whether a click was over a card, then move the card while events indicate that the player is moving the mouse, then stop moving it when they lift off. You would just modify the Cards.position in this case.

Comment
Add comment · Show 2 · 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 prototype7 · Sep 15, 2012 at 10:13 AM 0
Share

which one is more flexible if I want to use Ray to detect collider between those cards, i mean the performance in mobile device.

avatar image whydoidoit · Sep 15, 2012 at 10:14 AM 0
Share

If you want this to work on mobile do not use OnGUI - the performance is terrible.

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

10 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

Related Questions

Help setting maxAceleration 0 Answers

Does anyone know of a way to assign an Axis or KeyCode to a GUI Button or Texture? I'm trying to make a 2D platformer on a mobile device. As you can tell, I have no Idea what I'm doing... 1 Answer

SpriteManager 2 1 Answer

How to make a object move right automatically in 2d. 2 Answers

How can i make a sprite move in the direction it is rotated? 1 Answer


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