- Home /
How can I add a touch screen function for android mobile game :
I have a project which is a 2d Chess game and I am making it on Mobile. Everything is working perfectly but I have this single problem which is when I try to download it on my mobile the touch screen does not work on the chess pieces. For example when I click on any chess piece it does not respond. How to fix this problem can you please tell me?
I want the touch function to use the SelectPiece() Function because I want it to be when I touch any piece I want it to select the piece and shows me where I can move it when I touch again it should move to the specific place that I chose. Basically do the same as if I am playing it on PC.
I will send you the code. I believe I should add the touch screen function in the script below because it is the one responsible for the pieces.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.Events;
using System;
public abstract class BasePiece : EventTrigger
{
public class MovedEvent : UnityEvent<Vector3, Vector3> { }
public static MovedEvent Moved = new MovedEvent();
[HideInInspector]
public Color mColor = Color.clear;
public bool mIsFirstMove = true;
protected Cell mOriginalCell = null;
protected Cell mCurrentCell = null;
protected RectTransform mRectTransform = null;
protected PieceManager mPieceManager;
protected Vector3Int mMovement = Vector3Int.one;
protected List<Cell> mHighlightedCells = new List<Cell>();
public static BasePiece highlightedPiece = null;
private Image image = null;
private void OnEnable()
{
if (image)
image.raycastTarget = true;
}
private void OnDisable()
{
if (image)
image.raycastTarget = false;
}
public virtual void Setup(Color newTeamColor, Color32 newSpriteColor, PieceManager newPieceManager)
{
mPieceManager = newPieceManager;
mColor = newTeamColor;
image = GetComponent<Image>();
image.color = newSpriteColor;
mRectTransform = GetComponent<RectTransform>();
}
public virtual void Place(Cell newCell)
{
// Cell stuff
mCurrentCell = newCell;
mOriginalCell = newCell;
mCurrentCell.mCurrentPiece = this;
// Object stuff
transform.position = newCell.transform.position;
gameObject.SetActive(true);
}
public void Reset()
{
Kill();
mIsFirstMove = true;
Place(mOriginalCell);
}
public virtual void Kill()
{
// Clear current cell
mCurrentCell.mCurrentPiece = null;
// Remove piece
gameObject.SetActive(false);
}
public bool HasMove()
{
CheckPathing();
// If no moves
if (mHighlightedCells.Count == 0)
return false;
// If moves available
return true;
}
#region Movement
private void CreateCellPath(int xDirection, int yDirection, int movement)
{
// Target position
int currentX = mCurrentCell.mBoardPosition.x;
int currentY = mCurrentCell.mBoardPosition.y;
// Check each cell
for (int i = 1; i <= movement; i++)
{
currentX += xDirection;
currentY += yDirection;
// Get the state of the target cell
CellState cellState = CellState.None;
cellState = mCurrentCell.mBoard.ValidateCell(currentX, currentY, this);
// If enemy, add to list, break
if (cellState == CellState.Enemy)
{
mHighlightedCells.Add(mCurrentCell.mBoard.mAllCells[currentX, currentY]);
break;
}
// If the cell is not free, break
if (cellState != CellState.Free)
break;
// Add to list
mHighlightedCells.Add(mCurrentCell.mBoard.mAllCells[currentX, currentY]);
}
}
protected virtual void CheckPathing()
{
// Horizontal
CreateCellPath(1, 0, mMovement.x);
CreateCellPath(-1, 0, mMovement.x);
// Vertical
CreateCellPath(0, 1, mMovement.y);
CreateCellPath(0, -1, mMovement.y);
// Upper diagonal
CreateCellPath(1, 1, mMovement.z);
CreateCellPath(-1, 1, mMovement.z);
// Lower diagonal
CreateCellPath(-1, -1, mMovement.z);
CreateCellPath(1, -1, mMovement.z);
}
protected void ShowCells()
{
foreach (Cell cell in mHighlightedCells)
cell.mOutlineImage.enabled = true;
}
protected void ClearCells()
{
foreach (Cell cell in mHighlightedCells)
cell.mOutlineImage.enabled = false;
mHighlightedCells.Clear();
}
public virtual void MoveTo(Cell cell)
{
// Log
print(gameObject.name + mCurrentCell.name + " > " + cell.name);
// Hide
ClearCells();
// First move switch
mIsFirstMove = false;
// If there is an enemy piece, remove it
cell.RemovePiece();
// Clear current
mCurrentCell.mCurrentPiece = null;
// Move Event
Moved.Invoke(transform.position, cell.transform.position);
// Switch cells
mCurrentCell = cell;
mCurrentCell.mCurrentPiece = this;
// Move on board
transform.position = mCurrentCell.transform.position;
highlightedPiece = null;
// End turn
mPieceManager.SwitchSides(mColor);
SoundManager.PlaySound();
}
#endregion
#region Events
#if UNITY_EDITOR
public override void OnPointerClick(PointerEventData eventData)
{
base.OnPointerClick(eventData);
SelectPiece();
}
#endif
public void SelectPiece()
{
if (highlightedPiece == this)
{
highlightedPiece.ClearCells();
highlightedPiece = null;
return;
}
if (highlightedPiece)
{
highlightedPiece.ClearCells();
}
highlightedPiece = this;
// Test for cells
CheckPathing();
// Show valid cells
ShowCells();
}
#endregion
}
Thank you for reading my question and either way I will keep searching on the internet hopefully I can find something helpful. Any help would be appreciated.
#if UNITY_EDITOR
public override void OnPointerClick(PointerEventData eventData)
{
base.OnPointerClick(eventData);
SelectPiece();
}
#endif
Does this line #if UNITY_EDITOR
seem to be an issue? Will this code not running on mobile or build, affect the SelectPiece() function at all? @$$anonymous$$alekob