Touch event does not do any action on the Unity c # smartphone - c#

I have this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Audio;
using UnityEngine.EventSystems;
public class start : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public GameObject musica;
public bool pulsado;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnPointerDown(PointerEventData eventData)
{
pulsado = true;
audio();
SceneManager.LoadScene("pajaro");
}
public void OnPointerUp(PointerEventData eventData)
{
pulsado = false;
}
void audio()
{
musica.SetActive(true);
}
}
When I export it to Android, touch does not work, but if it works in unity

You're using OnPointerDown, this is specific to the mouse, this event does not fire for Touch input (touch input does not have a concept of "up" and "down").
In order to have touch input, you have to use Input.touches during the Update() loop.

Related

OnTriggerEnter2D doesn't work after switching scenes

I have a game that is similar to "Flappy Bird" and I have main menu where I can start game and change skin of a pigeon. My skin collection is implemented with scroll rect and in the center there is a trigger which starts an animation of scaling a pigeon, it works fine until I click "start" and the scene changes to game and when I return to my main menu and click "skins" this trigger doesn't work anymore.
Script what is attached to all scroll rect elements to detect collisions with trigger:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResizeFieldScript : MonoBehaviour
{
private Animator _anim;
private void Start()
{
_anim = GetComponent<Animator>();
}
public void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log("Trigger is working");
if(collider.tag == "ResizeField")
{
Debug.Log("Condition is working");
_anim.SetBool("isInTrigger", true);
}
}
public void OnTriggerExit2D(Collider2D collider)
{
_anim.SetBool("isInTrigger", false);
}
}
Script what is attached to an empty object to change scenes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
public class UIController : MonoBehaviour
{
[SerializeField] private List<string> sceneNameList;
private string sceneToFind;
private int index = 0;
public void SceneChanger()
{
sceneToFind = EventSystem.current.currentSelectedGameObject.name;
foreach(string str in sceneNameList)
{
if(str == sceneToFind)
{
SceneManager.LoadScene(index);
index = 0;
break;
}
index++;
}
}
public void Exit()
{
Application.Quit();
}
public void BackMenu()
{
SceneManager.LoadScene(4);
}
}
OnTriggerEnter2D doesn't work after switching scenes. We could use OnTriggerEnter2D to jump scenes.
code show as below:
private void Update() {
// If E is pressed
if (Input. GetKeyDown(KeyCode. E)) {
// scene switching
SceneManager.LoadScene(4);
}
}
private void OnTriggerEnter2D(Collider collision) {
if (collision. tag == "ResizeField") {
// The UI prompts the user to press E to jump
EnterDialog.SetActive(true);
Debug.Log("Condition is working");
// _anim.SetBool("isInTrigger", true);
}
}
Hope it helps you.

Executing a Function Between Script Files

Tell me please. I have 2 scripts that hang on different Unity objects. The first script is for clicking a button. The second one is for executing the function.
How can I execute the AddItem function if the button is pressed?
Script 1 (button click):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UseButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
public bool isClick = false;
public void OnPointerDown(PointerEventData ped)
{
isClick = true;
}
public void OnPointerUp(PointerEventData ped)
{
isClick = false;
}
}
Script 2 (Adding Items):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InventoryManager : MonoBehaviour
{
public void AddItem(ItemScriptableObject _item, int _amount)
{
foreach(InventorySlot slot in slots)
{
if (slot.item == _item)
{
slot.amount += _amount;
return;
}
}
foreach(InventorySlot slot in slots)
{
//print(_item+" "+_amount);
if (slot.isEmpty == true)
{
slot.item = _item;
slot.amount = _amount;
slot.isEmpty = false;
slot.SetIcon(_item.icon);
return;
}
}
}
If you dont have a variable on the second script, of the first script, you should do that by using:
GameObject go;
go.GetComponent<InventoryManager>();
try changing the first script to something like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UseButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
public bool isClick = false;
void Update()
{
if (isClick)
{
Debug.Log("do something")
}
}
public void OnPointerDown(PointerEventData ped)
{
isClick = true;
}
public void OnPointerUp(PointerEventData ped)
{
isClick = false;
}
}
This way you can modify the variable: is Click easily.

How to select and deselect in a crossword game

So, im doing a crossword game in Unity Engine, and when i select (with my left click button) a word, the white tiles should turn green (which occurs), but when i stop holding my left mouse click button, it does not turns white again as you can see on the print
And the code is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System;
public class Selecionar : MonoBehaviour, ISelectHandler, IPointerClickHandler, IDeselectHandler {
private int acertos;
public static HashSet<Selecionar> todosMeusSelecionaveis = new HashSet<Selecionar>();
public static HashSet<Selecionar> selecionado = new HashSet<Selecionar>();
Renderer myRenderer;
[SerializeField]
Material materialNaoSelecionado;
[SerializeField]
Material materialSelecionado;
void Awake()
{
todosMeusSelecionaveis.Add(this);
myRenderer = GetComponent<Renderer>();
}
public void OnDeselect(BaseEventData eventData)
{
myRenderer.material = materialNaoSelecionado;
}
public void OnPointerClick(PointerEventData eventData)
{
if(Input.GetKeyDown(KeyCode.Mouse0)){
DeselectAll(eventData);
}
OnSelect(eventData);
}
public void OnSelect(BaseEventData eventData)
{
selecionado.Add(this);
myRenderer.material = materialSelecionado;
}
public static void DeselectAll (BaseEventData eventData){
foreach (Selecionar selecionavel in selecionado)
{
selecionavel.OnDeselect(eventData);
}
selecionado.Clear();
}
}
I think you should return inside the if statement on OnPointerClick. When it enters, it executes DeselectAll followed by OnSelect.
Either do:
public void OnPointerClick(PointerEventData eventData)
{
if(Input.GetKeyDown(KeyCode.Mouse0)){
DeselectAll(eventData);
return; // <----
}
OnSelect(eventData);
}
or:
public void OnPointerClick(PointerEventData eventData)
{
if(Input.GetKeyDown(KeyCode.Mouse0)){
DeselectAll(eventData);
} else {
OnSelect(eventData);
}
}

I want to move 2d object only horizontally by touch in Unity

I implemented a touch drag.
But I can not move only horizontally.
I would expect to limit it to if, but I do not know what to do after that.
Here is my code.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public void OnBeginDrag (PointerEventData eventData)
{
Debug.Log ("OnBeginDrag");
}
public void OnDrag (PointerEventData eventData)
{
Debug.Log ("OnBeginDrag");
//this.transform.position = eventData.position;
GetComponent<Transform> ().position = eventData.position;
}
public void OnEndDrag (PointerEventData evnetData)
{
Debug.Log ("OnEndDrag");
}
}
You don't need to call GetComponent for transform.
If the code you were using works and you just want horizontal drag, something like this should work.
var currentPos = transform.position;
transform.position = new Vector3(eventData.position.x, currentPos.y, currentPos.z);

Buttons in Unity, without using UI?

Is there a way in Unity to create a simple 2D button without using the UI layer. I have a game with a lot of buttons and I don't want to make the whole app in the UI. Some more controls are welcome too: switches, sliders etc.
PS. I saw NGUI and I don't like it so far. Anything else?
Is there a way in Unity to create a simple 2D button without using the
UI layer
You can use Sprite/Sprite Render as a Button.First Create a GameObject and attach EventSystem and StandaloneInputModule to it. Attach Physics2DRaycaster to the Camera, implement IPointerClickHandler and override OnPointerClick function. Create a 2D Sprite by going to GameObject->2D Object->Sprite then attach your script to the Sprite. Here is a complete code to do that:
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class SPRITEBUTTON: MonoBehaviour, IPointerClickHandler,
IPointerDownHandler, IPointerEnterHandler,
IPointerUpHandler, IPointerExitHandler
{
void Start()
{
//Attach Physics2DRaycaster to the Camera
Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
addEventSystem();
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("Mouse Clicked!");
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Mouse Down!");
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Mouse Enter!");
}
public void OnPointerUp(PointerEventData eventData)
{
Debug.Log("Mouse Up!");
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("Mouse Exit!");
}
//Add Event System to the Camera
void addEventSystem()
{
GameObject eventSystem = null;
GameObject tempObj = GameObject.Find("EventSystem");
if (tempObj == null)
{
eventSystem = new GameObject("EventSystem");
eventSystem.AddComponent<EventSystem>();
eventSystem.AddComponent<StandaloneInputModule>();
}
else
{
if ((tempObj.GetComponent<EventSystem>()) == null)
{
tempObj.AddComponent<EventSystem>();
}
if ((tempObj.GetComponent<StandaloneInputModule>()) == null)
{
tempObj.AddComponent<StandaloneInputModule>();
}
}
}
}
EDIT:
If this is a 3D GameObject/Mesh, then you need to add a simple collider to it. If it is just Sprite then you must add a 2D collider to the sprite.
Another approach that is even simpler, is to just add a BoxCollider2D component, then add the following methods to the a new componenent, say UIButton, where you will to perform the button actions :
void OnMouseOver()
void OnMouseDown()
void OnMouseUp()
This avoids the use of an EventSystem, StandaloneInputModule and Physics2DRaycaster.
Example :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class UIButton : MonoBehaviour {
public Sprite regular;
public Sprite mouseOver;
public Sprite mouseClicked;
public TextMeshPro buttonText;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
private void OnMouseDown()
{
}
private void OnMouseEnter()
{
}
private void OnMouseExit()
{
}
private void OnMouseUpAsButton()
{
}
}
Tested in unity 2018.1. One difference I initially noticed to this and the above approach is that the right mouse button click is not detected in this model, but is detected in the EventSystemModel.

Categories