- Home /
Select Object from panel and inset into different object
Hi, I have a panel, and a gameboard. The panel includes different a number of different sprites as buttons. What i want to do is after i click on a sprite click a place on the game board and have the sprite go to that position(and removed from the panel). After every sprite is removed from the panel i want to transfer the game board over to a new scene keeping the positions of the sprites and add on to the board in my new scene and new script. I have code that draws and describes different spaces on the gameboard. I have prefabs of what will be on the gameboard. For example in the panel i have a 2d sprite of a green cat. when i click the green cat and then click a place on the board, the green cat 3d prefab will appear at the position specified. I'm not sure how to get the sprite off the panel and match with the correct prefab and place it at the specific spot on the gameboard. Then saving the board, to be transfered to a new scene and new script.
So far my code is as follows:
public class SelectionProcess : MonoBehaviour
{
public static SelectionProcess Instance { set; get; }
public piece[,] Pieces { set; get; }
private piece selectedPiece;
private const float TILE_SIZE = 1.0f;
private const float TILE_OFFSET = 0.5f;
private int selectionX = -1;
private int selectionY = -1;
public List<GameObject> PiecePrefabs;
private List<GameObject> activePiece;
private Material previousMat;
public Material selectedMat;
private Quaternion orientation = Quaternion.Euler(0, 180, 0);
private void Start()
{
Instance = this;
//SpawnAllPieces();
}
private void Update()
{
UpdateSelection();
DrawGameboard();
}
private void UpdateSelection()
{
if (!Camera.main)
return;
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 25.0f, LayerMask.GetMask("GamePlane")))
{
//Debug.Log(hit.point);
selectionX = (int)hit.point.x;
selectionY = (int)hit.point.z;
}
else
{
selectionX = -1;
selectionY = -1;
}
}
/*private void SpawnPiece(int index, int x, int y)
{
GameObject go = Instantiate(PiecePrefabs[index], GetTileCenter(x, y), orientation) as GameObject;
go.transform.SetParent(transform);
mypieces[x, y] = go.GetComponent<piece>();
mypieces[x, y].setPosition(x, y);
activepiece.Add(go);
} */
/* private void SpawnAllpieces()
{
activepiece = new List<GameObject>();
mypieces = new piece[8, 8];
SpawnPiece(0, 3, 0);
SpawnPiece(1, 4, 0);
SpawnPiece(2, 0, 0);
SpawnPiece(2, 7, 0);
SpawnPiece(3, 2, 0);
SpawnPiece(3, 5, 0);
SpawnPiece(4, 1, 0);
SpawnPiece(4, 6, 0);
} */
private Vector3 GetTileCenter(int x, int y)
{
Vector3 origin = Vector3.zero;
origin.x += (TILE_SIZE * x) + TILE_OFFSET;
origin.z += (TILE_SIZE * y) + TILE_OFFSET;
return origin;
}
private void Drawgameboard()
{
Vector3 widthLine = Vector3.right * 8;
Vector3 heightLine = Vector3.forward * 8;
for (int i = 0; i <= 8; i++)
{
Vector3 start = Vector3.forward * i;
Debug.DrawLine(start, start + widthLine);
for (int j = 0; j <= 8; j++)
{
start = Vector3.right * j;
Debug.DrawLine(start, start + heightLine);
}
}
//Draw the selection
if (selectionX >= 0 && selectionY >= 0)
{
Debug.DrawLine(
Vector3.forward * selectionY + Vector3.right * selectionX,
Vector3.forward * (selectionY + 1) + Vector3.right * (selectionX + 1));
Debug.DrawLine(
Vector3.forward * (selectionY + 1) + Vector3.right * selectionX,
Vector3.forward * selectionY + Vector3.right * (selectionX + 1));
}
}
}
So after you select a piece its coordinates should get sent to the spawnpiece().
Your answer
Follow this Question
Related Questions
How do i make my ui elements stay the same size and position on all andorid devices? 1 Answer
Issues rotating a UI image while the canvas is parented to the Main Camera 0 Answers
Resizing UI Image but with minimum width 2 Answers
Canvas Screen Space Issue 1 Answer
Access sprite secondary texture in shader for UI canvas image 1 Answer