- Home /
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
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;
}
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.
which one is more flexible if I want to use Ray to detect collider between those cards, i mean the performance in mobile device.
If you want this to work on mobile do not use OnGUI - the performance is terrible.
Your answer
Follow this Question
Related Questions
Help setting maxAceleration 0 Answers
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