Only use button every few seconds in Unity [duplicate] - c#

This question already has answers here:
How to make the script wait/sleep in a simple way in unity
(7 answers)
Closed 1 year ago.
I am trying to make it so that a button in Unity that can only be pressed and activated every few seconds. I have an array with famous quotes and when I press my button it does display these quotes, but if you press the button continuously then it will cycle through all of the quotes without the ability to be thoughtful and read through them. Now, I have tried something like below, but it did nothing (as in the button still was able to be pressed and no effect was apparent):
Bool pressed;
void Function () {
if (pressed) {
//code to call array
}
}
I thought that a simple combination of bool and an if loop would be sufficient, but this does not work. Am I close to this concept? What would need to be changed to get this operating?

There are a few things you can do to achieve this. Your current code is never toggling the bool pressed. You can just catch the unwanted button presses with a localized bool like you are doing now, but there might be a better approach.
I would consider changing the intractability of the button to also show the end-user that they can not press the button for a short amount of time.
using UnityEngine.UI;
// button that will be pressed
[SerializeField] private Button btn = null;
// time until the button is re-enabled after being pressed
private float timeToWait = 5f;
// reference to the coroutine that toggles our button
private Coroutine buttonDisabled = null;
private void Start()
{
// you can either add the OnClick in the inspector, or you can programmatically add it here
btn.onClick.AddListener(ClickButton);
}
public void ClickButton()
{
// if we have an ongoing coroutine do not start another
if(buttonDisabled != null)
return;
// start our coroutine to re-enable the button
buttonDisabled = StartCoroutine(DisableButtonForSeconds(timeToWait));
// put any other code needed here such as displaying a new quote
}
private IEnumerator DisableButtonForSeconds(float seconds)
{
// disable the button
btn.interactable = false;
// wait for our amount of time to re-enable
yield return new WaitForSeconds(seconds);
// re-enable the button
btn.interactable = true;
// reset our reference to be called again
buttonDisabled = null;
}
Assign the button in the editor to btn and set whatever time you want to wait between button presses for timeToWait. You will also need to assign the button's OnClick to ClickButton, either in code or in the inspector. With this solution the function will not be able to be called as the button itself will be grayed out and not be able to be clicked. After the timeToWait duration is over, the button will reactivate. Place whatever other code you need in the ClickButton method that drives your quote cycling.
Edit: Here is similar code but using an Invoke instead of Coroutine as requested
using UnityEngine.UI;
// button that will be pressed
[SerializeField] private Button btn = null;
// time until the button is re-enabled after being pressed
private float timeToWait = 5f;
private void Start()
{
// you can either add the OnClick in the inspector, or you can programmatically add it here
btn.onClick.AddListener(ClickButton);
}
public void ClickButton()
{
// do not start the function if we are already in the process
if (IsInvoking("ReEnableButton"))
return;
// disable our button interactability
btn.interactable = false;
// call our function ReenableButton in timeToWait seconds
Invoke("ReEnableButton", timeToWait);
}
private void ReEnableButton()
{
// re-enable the button
btn.interactable = true;
}
Edit 2: There are a few ways you can go about having one button disable all four buttons. In general practice, you do not want these buttons knowing about each other, so it would be a good idea to make a button manager that handles the OnClick now and sends the event to all Buttons it knows about.
using UnityEngine.UI;
public class ButtonManager : MonoBehaviour
{
// assign these button references in the inspector by dragging them into the list
[SerializeField] private List<YourButtonScript> YourButtons = new List<YourButtonScript>();
private void Start()
{
// assign the onClick of each button here INSTEAD of the Start() in the button class
// you can still have an onClick in the Button itself, but do NOT have it do the disabling
foreach(YourButtonScript btn in YourButtons)
{
// assign our onClick to callback to disable all buttons
btn.SetDisableOnclick(DisableAllButtons);
}
}
public void DisableAllButtons()
{
foreach(YourButtonScript btn in YourButtons)
btn.DisableButton();
}
}
public class YourButtonScript : MonoBehaviour
{
using UnityEngine.UI;
// button that will be pressed
[SerializeField] private Button btn = null;
// time until the button is re-enabled after being pressed
private float timeToWait = 5f;
// reference to the coroutine that toggles our button
private Coroutine buttonDisabled = null;
public delegate void DisableButtonCallback();
public void SetDisableOnclick(DisableButtonCallback callback)
{
// add the callback to the manager
btn.onClick.AddListener(delegate{callback();});
}
private void Start()
{
// do NOT call the ClickButton anymore from here as the manager handles it
// if you have other button specific data put it in HandleSpecificButtonQuoteData() now
btn.onClick.AddListener(HandleSpecificButtonQuoteData);
}
private void HandleSpecificButtonQuoteData()
{
// put whatever code here that is specific to JUST this button
// if it handles the quotes or what not, display it here
}
public void DisableButton()
{
// if we have an ongoing coroutine do not start another
if(buttonDisabled != null)
return;
// start our coroutine to re-enable the button
buttonDisabled = StartCoroutine(DisableButtonForSeconds(timeToWait));
}
private IEnumerator DisableButtonForSeconds(float seconds)
{
// disable the button
btn.interactable = false;
// wait for our amount of time to re-enable
yield return new WaitForSeconds(seconds);
// re-enable the button
btn.interactable = true;
// reset our reference to be called again
buttonDisabled = null;
}
}
Let me know if you have issues with this.

Related

Doubst about Managin player actions and cooldown time

So i got this action object witch contains a button, when i press that button the action coroutine starts, sets the cooldown of the action and enabled=false the button.
Im trying to implement a cooldown system so every time the turn ends action.currentCooldown -=1.
Im yet to implement my turn manager and i must admit im a bit clueless about it, i guess it must have a state(allyTurn, enemyTurn) and a coroutine to update all action cooldown when the turn changes.
Also what i want to do is asing a List to each unit and then display each action buttons.
here are some screenshots of the code (keep in mind its just a first draft and im still learning the basics)
Hereis the action object
here is the code for the action
I apreciate all the help i can get
So there are a couple ways to implement what you are looking for. The way that I would want to go for is using events. You could create events for each state change (i.e. AllyTurnStart and EnemyTurnStart):
public class ExampleClass : MonoBehaviour
{
bool AllyTurn = true;
UnityEvent AllyTurnStart;
UnityEvent EnemyTurnStart;
BaseAction AllyAction;
BaseAction EnemyAction;
void Start()
{
// This will setup the event listeners that get called
if (AllyTurnStart == null)
AllyTurnStart = new UnityEvent();
if (EnemyTurnStart == null)
EnemyTurnStart = new UnityEvent();
// This is for example purposes but assigning actions to the events can happen anywhere
AllyAction = new BaseAction()
EnemyAction = new BaseAction()
AllyTurnStart.AddListener(AllyAction.StartOfTurnEvents);
EnemyTurnStart.AddListener(EnemyAction.StartOfTurnEvents);
}
//This method is meant to simulate switching turns back and forth.
void NextTurn()
{
if(AllysTurn)
{
AllyTurnStart.Invoke()
}
else
{
EnemyTurnStart.Invoke()
}
//This switches whos turn it is
AllysTurn = !AllysTurn;
}
}
public class BaseAction : Monobehaviour
{
int Cooldown;
public StartOfTurnEvents()
{
// Here you can reduce the cooldown, Check if it is ready, etc.
}
}
Hope this helps.

Any clues on this Unity code not working?

I have this function here to duplicate a UIPanel(Prefab) and its components. It duplicates, then succesfully assigns a objectname and enable/disable the Interactable setting of the button component. But the part where it sets the ''OnClick'' on my button does not work. I dont understand as I can play with other options of this button component...
Yes the function Click exists.
Yes I included my using using System.Collections.Generic; using UnityEngine.UI;
Any Help? Thanks.
public Text Title;
public Button Btn;
public void CreateEventSquares(string keyval, string valueval)
{
string Event = (keyval);
string Link = (valueval);
Debug.Log("Event :" + Event + "Link is :" + Link);
NewPanel = Instantiate(Event0);
NewPanel.transform.SetParent(EventPanel.transform);
NewPanel.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
NewPanel.name = keyval;
//Sets the button action to trigger Click function (Not Working)
Btn = NewPanel.GetComponent<Button>();
Btn.onClick.AddListener(Click);
Btn.interactable = false;
//Sets the title same as keyval (This Works)
Title = NewPanel.GetComponentInChildren<Text>();
Title.text = keyval;
}
Click
public void Click()
{
//do This
}
I've re-created this in Unity with your code and it's working perfectly fine for me! I think it's highly likely you are being thrown by the fact that the function is not showing up in the on click portion of the button in the Unity Inspector.
Try putting a debug.log in your click function and see what happens.
If you wanted to still define functions in the inspector you would have to use a Unity Event and then subscribe it to the onclick.
[Serializable]
public class ButtonClickedEvent : UnityEvent { }
[SerializeField]
public ButtonClickedEvent m_OnClick = new ButtonClickedEvent();
Btn.onClick.AddListener(Click);
void Click ()
{
m_OnClick.Invoke();
}

How do I close a UI element when a tap occurs outside of it?

There are a lot of solutions to this problem floating around, but none of them seem to work properly for me. I have a button that opens a list of selections in a UI element, and I want it to close when clicked outside of it. I currently have this:
private void OnEnable()
{
EventSystem.current.SetSelectedGameObject(gameObject);
}
public void OnDeselect(BaseEventData eventData)
{
//Close the Window on Deselect only if a click occurred outside this panel
if (!mouseIsOver)
gameObject.SetActive(false);
}
public void OnPointerEnter(PointerEventData eventData)
{
mouseIsOver = true;
EventSystem.current.SetSelectedGameObject(gameObject);
}
public void OnPointerExit(PointerEventData eventData)
{
mouseIsOver = false;
EventSystem.current.SetSelectedGameObject(gameObject);
}
Which works fine on a PC, but unfortunately due to there not being an actual pointer on mobile, it closes the panel even if clicked inside. I have tried using something like this:
foreach (Touch touch in Input.touches)
{
int id = touch.fingerId;
if (EventSystem.current.IsPointerOverGameObject(id))
{
isClicked = true;
}
if (Input.GetMouseButtonDown(0))
{
// Check if the mouse was clicked over a UI element
if (EventSystem.current.IsPointerOverGameObject())
{
isClicked = true;
}
}
}
But that has not worked either. This seems like an incredibly simple problem and I don't understand why I can't find a simple solution to it.
I recomend using Unity technic when dropdown is created.
Look at this link
The main idea is to create blocker behind your button and the other ui elements.
Your open panel funcion should look something like this:
private RectTransform gameObjectTransform;
public void OpenPanel()
{
CreateBlocker();
gameObjectTransform = transform.parent;
transform.SetParent(GetComponentInParent<Canvas>());
transform.SetAsLastSibling();
}
Create blocker method like in docs:
private GameObject currentBlocker;
public void CreateBlocker()
{
GameObject gameObject = new GameObject("Blocker");
RectTransform rectTransform = gameObject.AddComponent<RectTransform>();
rectTransform.SetParent(ChatGraphics.Instance.mainCanvas, false);
rectTransform.anchorMin = (Vector2)Vector3.zero;
rectTransform.anchorMax = (Vector2)Vector3.one;
rectTransform.sizeDelta = Vector2.zero;
Canvas canvas = gameObject.AddComponent<Canvas>();
canvas.overrideSorting = true;
canvas.sortingLayerID = component.sortingLayerID;
canvas.sortingOrder = component.sortingOrder - 1;
gameObject.AddComponent<GraphicRaycaster>();
gameObject.AddComponent<Image>().color = Color.clear;
gameObject.AddComponent<Button>().onClick.AddListener(delegate { Hide(); });
currentBlocker = gameObject;
}
And last is Hide method which should be called every time (even if blocker does not triggered)
public void Hide()
{
if (currentBlocker != null)
Destroy(currentBlocker);
if (gameObjectTransform != null)
transform.SetParent(gameObjectTransform);
// Your code to hide your panel
}

Unity preventing editing of variable through script

I'm trying to make a tooltip pop up when I hover my mouse over a button. I had some issues calling the main thread from a separate thread, since the main thread is the only thread you can edit game objects from. So I decided to use a boolean and an update function to get the desired result. However, I ran into an issue I have never seen before. I am setting my boolean showToolTip to true, which is supposed to trigger the create pop-up function. However, by the time my update function runs, showTooltip is always false. I know when I set it to true, it becomes true, because I used debug statements in the requestShowTooltip function to see this. However, it is always false when the update function runs. I did some tests by changing showToolTip and removeToolTip to public booleans. I can change them in the editor while running the application, and when I manually change them through the editor they work 100% as expected, and the tooltip will appear and hide as I change the correlating booleans. However when they are changed from the script they are retained as false. I have also tried leaving them unitialized and initializing them to false in the start, and I was still having the same problem. I even tried taking "showTooltip = true;" out of the thread and just having the function do it immediately, and I was still having the same issue. showTooltip is always false when the update function is called. I have determined this by adding a debug statement in the update function that reports the value of the showTootip and removeTooltip booleans. Furthermore, no matter what, "Calling create tooltip" never appears in the console. Except for when I made the booleans public and changed them through the editor.
Some further explanation on how I set this up. I made a Tooltip Game Object that is a child of the canvas that contains the buttons I want the tooltip for. I added the Tooltips script to the tooltip object. I created event triggers in the button Game Objects. the Pointer Enter trigger calls the requestShowTooltip() function, and the Pointer Exit trigger calls the requestHideTooltip() function.
The Tooltip Background and Tooltip Text Game Objects are also children of this same canvas.
public class Tooltips : MonoBehaviour
{
private GameObject toolTipBackground;
private GameObject toolTipText;
private GameObject inButtonObject;
private bool showTooltip = false;
private bool removeTooltip = false;
void Start()
{
toolTipBackground = GameObject.Find("Tooltip background");
toolTipText = GameObject.Find("Tooltip Text");
//inButton = GameObject.Find("Aft Button");
toolTipBackground.SetActive(false);
toolTipText.SetActive(false);
//Debug.Log("Tool Tip Start");
}
void Update()
{
// show the tooltip when the appropriate boolean has been set
// to true
if(removeTooltip)
{
hideTooltip();
}
// this if statement is always false, because showTooltip is
// always false when the update function is called??
if(showTooltip)
{
Debug.Log("Calling create tooltip");
createTooltip();
}
}
// A timed request for a tooltip to show. The tooltip should
// currently show one second after the request has been made. The
// text of the tooltip will be equal to the passed object's name
public void requestShowTooltip(GameObject inButtonObject)
{
Thread thread = new Thread(delegate ()
{
System.Threading.Thread.Sleep(1000);
this.inButtonObject = inButtonObject;
// I know this runs, but showTooltip is always returning
// to false when the update function is called??
showTooltip = true;
removeTooltip = false;
Debug.Log("Request function completed");
});
thread.start();
}
public void createTooltip()
{
toolTipText.SetActive(true);
toolTipBackground.SetActive(true);
string labelString = inButtonObject.name;
Button inButton = inButtonObject.GetComponent<Button>();
int width = labelString.Length * 10;
int height = 35;
Vector2 size = new Vector2(width, height);
Vector3 position = new Vector3(inButton.transform.x,
inButton.transform.position.y,
inButton.transform.position.z + 1);
toolTipBackground.GetComponent<RectTransform>().
sizeDelta = size;
toolTipText.GetComponent<RectTransform>().sizeDelta = size;
toolTipBackground.transform.position = position;
toolTipText.transform.position = position;
toolTipText.GetComponent<Text>().text = labelString;
showTooltip = false;
}
public void requestHideTooltip()
{
removeTooltip = true;
showTooltip = false;
}
public void hideTooltip()
{
Vector2 hide = new Vector2(0, 0);
toolTipBackground.GetComponent<RectTransform>().
sizeDelta = hide;
toolTipText.GetComponent<RectTransform>().sizeDelta = hide;
toolTipText.setActive(false);
toolTopBackground.SetActive(false);
removeTooltip = false;
showTooltip = false;
}
}
Since basically everything in Unity needs to be called in the main Thread (with very few exceptions) I would suggest to change your code using a ConcurrentQueue<Action> and TryDequeue to let the main thread work through all the responses from any thread.
// a queue for storing actions that shall be handled by the main thread
// a ConcurrentQueue in specific is thread-save
private ConcurrentQueue<Action> actions = new ConcurrentQueue<Action>();
private void Update()
{
// invoke all actions from the threads in the main thread
while(!actions.IsEmpty)
{
// TryDequeue writes the first entry to currentAction
// and at the same time removes it from the queue
// if it was successfull invoke the action otherwise do nothing
if(actions.TryDequeue(out var currentAction))
{
currentAction?.Invoke();
}
}
if(removeTooltip)
{
hideTooltip();
}
if(showTooltip)
{
Debug.Log("Calling create tooltip");
createTooltip();
}
}
Then in your Thread instead of making the thread directly execute the stuff itself rather pass it back to the main thread using the actions queue and add a new Action to the end using Enqueue
public void requestShowTooltip(GameObject inButtonObject)
{
Thread thread = new Thread(delegate()
{
System.Threading.Thread.Sleep(1000);
// add an Action to the end of the queue
// e.g. as lambda-expression
actions.Enqueue(()=>
{
this.inButtonObject = inButtonObject;
// I know this runs, but showTooltip is always returning
// to false when the update function is called??
showTooltip = true;
removeTooltip = false;
Debug.Log("Request function completed");
});
});
thread.start();
}
You might want to consider btw to make inButtonObject of type Button - first in order to make sure the passed object always has a/is a Button and second you can get rid of the performance intense GetComponent call.
The same way I would rather store
RectTransform toolTipBackgroundRectTransform;
Text toolTipText;
RectTransform toolTipTextrectTransform;
since those are the components you can get already once in Start and then you can re-use always the same references and get rid of all further GetComponent calls.

SlimDx Events on button pressed

I am using slimdx to interpret xbox controller button presses. I poll every 200ms to read the xbox button states and all works for me. I use
JoystickState state = Joystick.GetCurrentState();
// get buttons states
bool[] buttonsPressed = state.GetButtons();
Is there anyway to generate events on the button press instead of polling? To explain imagine if my poll time was 5 seconds. And the user presses a button in the 2nd second and releases it. In the next poll time my application will never know that the button was pressed
No - in DirectX you must poll. To do this efficiently you want to create a polling thread, and have a class which raises cross thread events to your consuming thread.
I know this is 4 years old but the answer is incorrect. The most efficient way may be to poll, but you can raise an event when you poll.
This is a work in progress but it should get someone started. Save this as a new class, it derives from a Timer, so once you add this to your project, build it, and drag it onto the Form you want to use it, you can then subscribe to the buttonPressed event.
public class GamePadController : Timer
{
public delegate void ButtonPressedDelegate(object sender, int ButtonNumber);
public event ButtonPressedDelegate ButtonPressed;
List<DeviceInstance> directInputList = new List<DeviceInstance>();
DirectInput directInput = new DirectInput();
List<SlimDX.DirectInput.Joystick> gamepads = new List<Joystick>();
SlimDX.DirectInput.JoystickState state;
public GamePadController()
{
this.Interval = 10;
this.Enabled = true;
this.Tick += GamePadController_Tick;
RefreshGamePads();
}
private void RefreshGamePads()
{
directInputList.Clear();
directInputList.AddRange(directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly));
gamepads.Clear();
foreach (var device in directInputList)
{
gamepads.Add(new SlimDX.DirectInput.Joystick(directInput, directInputList[0].InstanceGuid));
}
}
private void GamePadController_Tick(object sender, EventArgs e)
{
foreach (var gamepad in gamepads)
{
if (gamepad.Acquire().IsFailure)
continue;
if (gamepad.Poll().IsFailure)
continue;
if (SlimDX.Result.Last.IsFailure)
continue;
state = gamepad.GetCurrentState();
bool[] buttons = state.GetButtons();
for (int i = 0; i < buttons.Length; i++)
{
if (buttons[i])
{
if (ButtonPressed != null)
{
ButtonPressed(gamepad, i);
}
}
}
gamepad.Unacquire();
}
}
}
}

Categories

Resources