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 /
avatar image
0
Question by Malekob · Dec 27, 2020 at 07:30 AM · touchscreen

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.

Comment
Add comment · Show 1
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 Llama_w_2Ls · Dec 27, 2020 at 09:53 AM 0
Share
  #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

0 Replies

· Add your reply
  • Sort: 

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

112 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 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

Touchscreens, Unity and all that hub-bub 1 Answer

C# Touch Script - Fix GameObject touch. 0 Answers

Build an text editor in unity 3d 0 Answers

Bug moving a character with onscreen Joystick prefab 0 Answers

Can Unity be used to create touch based controls on Mac Platform? 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