playMaker

Author Topic: Simulate button click with Playmaker  (Read 2057 times)

Siknoidas

  • Playmaker Newbie
  • *
  • Posts: 4
Simulate button click with Playmaker
« on: August 25, 2022, 05:45:46 AM »
Hello,

I have working quiz template from asset store. There is two button correct and false buttons. Everything is working fine. I need simulate button click with playmaker, because i want to do swipe action (no need press button). How use swipe, get key i know how to do.
Example right swipe = pressing true button, swipe left = pressing false button
I tried many variations with call method, set property, send message anything dont working. Easiest way i think is simulate button press. Why there is no playmaker action, choose button, make condition and button is pressed?:D because i think couple same stories here on forum with no luck...

here is main script and button script. I already tried many things and no luck :( Maybe you can help me? Thanks in advance.

Main script:
Code: [Select]
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Assertions;

namespace TriviaQuizKit
{
/// <summary>
/// The user interface to display with true or false questions.
/// </summary>
    public class TrueFalseQuestionUi : QuestionUi
    {
        public TextMeshProUGUI QuestionText;
    public Image QuestionImage;
        public List<FlatButton> AnswerButtons;
        public Sprite DefaultAnswerSprite;
        public Sprite CorrectAnswerSprite;
        public Sprite WrongAnswerSprite;

    private int correctAnswerIndex;

private int numWrongAnswers;

        public override void OnQuestionLoaded(GameScreen gameScreen, BaseQuestion question)
        {
    var trueFalseQuestion = question as TrueFalseQuestion;
    Assert.IsNotNull(trueFalseQuestion);
    QuestionText.text = trueFalseQuestion.Question;
    if (trueFalseQuestion.Image != null)
    {
    QuestionImage.sprite = trueFalseQuestion.Image;
    QuestionImage.preserveAspect = true;
    }


        correctAnswerIndex = trueFalseQuestion.IsTrue ? 0 : 1;

        AnswerButtons[0].OnPressedEvent.RemoveAllListeners();
        AnswerButtons[1].OnPressedEvent.RemoveAllListeners();
AnswerButtons[0].OnPressedEvent.AddListener(() => { gameScreen.OnPlayerAnswered(0); });
AnswerButtons[1].OnPressedEvent.AddListener(() => { gameScreen.OnPlayerAnswered(1); });
        }

        public override bool OnQuestionAnswered(int answerIdx)
        {
var correct = answerIdx == correctAnswerIndex;
if (correct)
{
HighlightCorrectAnswer();
Debug.Log("paspaude3");
}
else
{
HighlightCorrectAnswer();
HighlightWrongAnswer(answerIdx);
Debug.Log("paspaude4");
}

        AnswerButtons[0].OnPressedEvent.RemoveAllListeners();
        AnswerButtons[1].OnPressedEvent.RemoveAllListeners();

    return correct;
        }

        public override void HighlightCorrectAnswer ()
        {



AnswerButtons[correctAnswerIndex].GetComponent<Image>().sprite = CorrectAnswerSprite;
        }

        public override void HighlightWrongAnswer(int answerIdx)
        {
AnswerButtons[answerIdx].GetComponent<Image>().sprite = WrongAnswerSprite;
        }

    public override void LockUi()
    {
foreach (var button in AnswerButtons)
{
button.OnPressedEvent.RemoveAllListeners();
}
    }

    public override void UnlockUi()
    {
foreach (var button in AnswerButtons)
{
button.GetComponent<Image>().sprite = DefaultAnswerSprite;
}
    }

    public override void OnSelectButtonPressed()
    {
Debug.Log("kazkas vyksta");
    }

    }
}

And flat button script

Code: [Select]
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

namespace TriviaQuizKit
{
/// <summary>
/// Button class used in the kit. It provides a subtle 'pressed' animation effect when clicked/touched.
/// </summary>
public class FlatButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
{
public float PressedDisplacement = 5f;

public TextMeshProUGUI Text;

public UnityEvent OnPressedEvent;

private bool isMouseInside;

public void OnPointerEnter(PointerEventData pointerEventData)
{
isMouseInside = true;
}

public void OnPointerExit(PointerEventData pointerEventData)
{
isMouseInside = false;
}

public void OnPointerDown(PointerEventData pointerEventData)
{
var rectTransform = GetComponent<RectTransform>();
var newPos = rectTransform.anchoredPosition;
newPos.y -= PressedDisplacement;
rectTransform.anchoredPosition = newPos;
}

public void OnPointerUp(PointerEventData pointerEventData)
{
var rectTransform = GetComponent<RectTransform>();
var newPos = rectTransform.anchoredPosition;
newPos.y += PressedDisplacement;
rectTransform.anchoredPosition = newPos;

if (isMouseInside)
{
Debug.Log("spaudziama");
OnPressedEvent.Invoke();
}
}
}
}

GMPlay

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Simulate button click with Playmaker
« Reply #1 on: August 25, 2022, 06:16:19 AM »
By 'Swipe', do you mean for Mobile Phones? Or swipe (drag mouse) for pc?

Siknoidas

  • Playmaker Newbie
  • *
  • Posts: 4
Re: Simulate button click with Playmaker
« Reply #2 on: August 25, 2022, 01:17:33 PM »
Thanks for respond, mobile swipe :)
But how swipe i know, how simulate button press i dont.

GMPlay

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Simulate button click with Playmaker
« Reply #3 on: August 26, 2022, 02:59:58 AM »
Well, I am working on a project that will have to be built for pc as well as mobile.

 And I am gonna need to have actions like 'swipe' eventually.

So I have decided to first focus on the PC actions (mouse drag in this case).

The key thing to know is that when you use actions like MOUSE DRAG in the 'system events' this gets automatically converted to the corresponding actions on a mobile build.

For example, if you use 'mouse click button down' action but then make a build for android, it automatically converts this action to the corresponding 'touch' by default. Sure, it will warn you about this while building but it works fine.

Whether we should ideally do this or not?  I don't know yet. But it serves the purpose temporarily while we work on the project for testing purpose.

Other option is to perhaps look for Touch related actions in the 'Ecosystem for Playmaker' and download it from there if they are not available on vanilla Playmaker.