UI detections working on PC but not on Android - c#

im creating an android map game and i cant solve a problem, i have a map and i also have a menu that pops up once i press on a gameobject with a collider. I had a problem that if i would press on that UI menu it would go throught it and it would turn the menu off, because i coded that if i press on another gameobject with a collider it would turn off. I fixed that by making a UI detection script (found it on google), where it draws a raycast from my cursor and detects a gameobject with a specific layer and returns a true or false value.
THE PROBLEM: Right now everything works fine on pc, since i am hovering with my cursor, but if i go on my unity remote once i press on the menu, it still turns off, because i guess its not quick enough to detect that its a UI element?
SOME CODE:
This is a UI Detection code:
public bool isOverUI;
private void Start()
{
UILayer = LayerMask.NameToLayer("MAINMENUCOMPONENTS");
}
private void Update()
{
print(IsPointerOverUIElement() ? "Over UI" : "Not over UI");
}
//Returns 'true' if we touched or hovering on Unity UI element.
public bool IsPointerOverUIElement()
{
return IsPointerOverUIElement(GetEventSystemRaycastResults());
}
//Returns 'true' if we touched or hovering on Unity UI element.
private bool IsPointerOverUIElement(List<RaycastResult> eventSystemRaysastResults)
{
for (int index = 0; index < eventSystemRaysastResults.Count; index++)
{
RaycastResult curRaysastResult = eventSystemRaysastResults[index];
if (curRaysastResult.gameObject.layer == UILayer)
{
isOverUI = true;
return true;
}
}
isOverUI = false;
return false;
}
//Gets all event system raycast results of current mouse or touch position.
static List<RaycastResult> GetEventSystemRaycastResults()
{
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.position = Input.mousePosition;
List<RaycastResult> raysastResults = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, raysastResults);
return raysastResults;
}
This is the menu code:
public UIDETECTIONS UID;
if (UID.isOverUI == true)
{
}
else if (UID.isOverUI == false)
{
if (infoMenuOn == false)
{
if (isCountryClicked == false)
{
isCountryClicked = true;
infoMenuOn = true;
Debug.Log("MENU ON");
}
else if (isCountryClicked == true)
{
isCountryClicked = false;
}
}
else if (infoMenuOn == true && isCountryClicked == true)
{
infoMenuOn = false;
isCountryClicked = false;
Debug.Log("MENU OFF");
}
}

I Should have done more research :D, derHugo gave me a hint, which was kind of an answer.
I found this piece of code which works for android. Works quite the same.
private bool IsPointerOverUIObject() {
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
return results.Count > 0;
}
With this code you can check if youre clicking over ui elements:
if (IsPointerOverUIObject() == true) // true or false

Related

How to make my character play after clicking a button, then bots auto play?

I am creating this simple 3D game but I'm having an issue with player vs computer mode. I want to be able to play vs 1 to 3 bots and when it's my turn I want the game to wait for me to click, but when it's the bots turn I want to them to auto play. What really happens with my code is that everything goes to auto play. I know the issue is with Update function because Update is called once per frame but I don't know how to fix it. Can you help me? The idea is to use a tracker/counter like i = 0 first. When it's 0 it means my character will play so it should wait for my mouse to be pressed. When it's not 0 it will be bots turn. This is the section of the code. Function veprimetELojtareve is the same as the code inside if(!bota).
private int i = 0;
// Update is called once per frame
void Update()
{
if (!bota)
{
if (Input.GetMouseButtonDown(0)) // Left Click
{
if (hasLanded)
Reset();
RollDice();
}
if (rb.IsSleeping() && !hasLanded && thrown)
{
hasLanded = true;
rb.useGravity = false;
rb.isKinematic = true;
SideValueCheck();
}
else if (rb.IsSleeping() && hasLanded && diceValue == 0)
RollAgain();
}
else
{
if (i == 0)
veprimetELojtareve();
else
{
if (hasLanded)
Reset();
RollDice();
if (rb.IsSleeping() && !hasLanded && thrown)
{
hasLanded = true;
rb.useGravity = false;
rb.isKinematic = true;
SideValueCheck();
}
else if (rb.IsSleeping() && hasLanded && diceValue == 0)
RollAgain();
}
i = (i + 1) % numriLojtareve;
Debug.Log("Vlera e i eshte: " + i);
}
}

Unity IsPointerOverGameObject Issue

I know it's a common problem but none worked. Maybe the button setup is wrong. I have a Panel with no image, and inside a settings button on the top left that once clicked opens another scene. I tried using those 3 methods but none works. It always detects it as a Game object.
The bool isGameStarted is checking if the player should move or no.
Please guide me through
Have tried
if (Swipe.Instance.Tap && !isGameStarted)
{
if (EventSystem.current.IsPointerOverGameObject() )
{
isGameStarted = false;
}
else
{
isGameStarted = true;
motor.StartRunning();
gameCanvas.SetTrigger("Show");
}
}
Also tried using trigger but it goes through the UI.
This is the original code.
if (Swipe.Instance.Tap && !isGameStarted)
{
isGameStarted = true;
motor.StartRunning();
gameCanvas.SetTrigger("Show");
}
Once you tap on the screen the player starts moving. I need it NOT to move or start the game if clicked on the settings button.
I had the same problem until I figured out that IsPointerOverGameObject seems to return true for any GameObject (possibly with a collider) and not only UI objects.
So I wrote a custom static class to check for UI objects only. You will need to set the layer of each panel, image, button, etc. to UI for it to work.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public static class MouseOverUILayerObject
{
public static bool IsPointerOverUIObject()
{
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
for (int i = 0; i < results.Count; i++)
{
if (results[i].gameObject.layer == 5) //5 = UI layer
{
return true;
}
}
return false;
}
}
Use it like this:
private void OnMouseDown()
{
if (!MouseOverUILayerObject.IsPointerOverUIObject())
HandleClick();
}
The overload of IsPointerOverGameObject takes a parameter
int pointerId Pointer (touch / mouse) ID.
and
If you use IsPointerOverGameObject() without a parameter, it points to the "left mouse button" (pointerId = -1);
therefore when you use IsPointerOverGameObject for touch, you should consider passing a pointerId to it.
So from the example
if (Swipe.Instance.Tap && !isGameStarted)
{
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
isGameStarted = false;
return;
}
isGameStarted = true;
motor.StartRunning();
gameCanvas.SetTrigger("Show");
}
otherwise if you have multiple possible touches you could also do the check for all of them like
if (Swipe.Instance.Tap && !isGameStarted)
{
foreach(var touch in Input.touches)
{
if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
{
isGameStarted = false;
return;
}
}
isGameStarted = true;
motor.StartRunning();
gameCanvas.SetTrigger("Show");
}
also look at this thread

Unity 3d Animation

I m a newbie to Unity3d and was just playing with unity animation, i was trying to implement 2 Main UI Button such as when either of them is pressed it should check the condition whether a animation is already played before by 2nd button available if yes then remove those gameobject from scene using reverse animation else play the default animation attach to the particular button.
Problem is out of 6 case only 4 are getting operational 2 of them are not executing (marked in the code as Case Not Operational)
Animation anim;
private bool isOpen = false;
private bool open = false;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animation>();
//isOpen = false;
//open = false;
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
isOpen = GUI.Toggle(new Rect(50, 50, 200, 100), isOpen, "IsOpen");
open = GUI.Toggle(new Rect(65, 65, 200, 100), open, "Open");
}
public void ButtonControl()
{
string name = EventSystem.current.currentSelectedGameObject.name;
if (name == "Main Button 1")
{
if (isOpen == false && open == false)
{ Debug.Log("False False 1");
Anim1();
isOpen = true;
}
else if (isOpen == false && open == true) // case not operational
{ Debug.Log("False True 2");
ReverseAnim_2();
open = false;
Anim1();
isOpen = true;
}
else if(isOpen == true)
{
Debug.Log("True 3");
ReverseAnim_1();
isOpen = false;
}
}
else if (name == "Main Button 2")
{
if (open == false && isOpen == false)
{ Debug.Log("False False 4");
Anim2();
open = true;
}
else if(open == false && isOpen == true) // case not operational
{ Debug.Log("False True 5");
ReverseAnim_1();
isOpen = false;
Anim2();
open = true;
}
else if(open == true)
{
Debug.Log("True 6");
ReverseAnim_2();
open = false;
}
}
}
void ReverseAnim_1()
{
anim["1"].speed = -1;
anim["1"].time = anim["1"].length;
anim.Play();
}
void Anim1()
{
anim["1"].speed = 1;
anim.Play();
}
void Anim2()
{
anim["2"].speed = 1;
anim.Play();
}
void ReverseAnim_2()
{
anim["2"].speed = -1;
anim["2"].time = anim["2"].length;
anim.Play();
}
The problem is that you have two Main_B2 scripts and they are each tracking their own values for the isOpen and open fields.
One solution for this could be to make the fields static so that they are in common across all instances of Main_B2.
Animation anim;
private static bool isOpen = false;
private static bool open = false;
If you do this, your code should work as intended - each instance of Main_B2 would then be referring to the same fields whenever they reference isOpen or open.
With that said, static fields can get kind of sloppy if you ever want to try and re-use code so a better option might be to have only one instance of Main_B2 somewhere else such as the Canvas, instead of one on each button.
Then you could have in it public GameObject fields that you can drag the button objects into, and private Animation fields for the animations.:
public GameObject button1
public GameObject button2
private Animation anim1
private Animation anim2
Then in Start:
anim1 = button1.GetComponent<Animation>();
anim2 = button2.GetComponent<Animation>();
And then wherever you referred to anim you would refer to anim1 or anim2 instead:
void ReverseAnim_1()
{
anim1.["1"].speed = -1;
anim1.["1"].time = anim1["1"].length;
anim1.Play();
}
void Anim1()
{
anim1["1"].speed = 1;
anim1.Play();
}
void Anim2()
{
anim2["2"].speed = 1;
anim2.Play();
}
void ReverseAnim_2()
{
anim2["2"].speed = -1;
anim2["2"].time = anim2["2"].length;
anim2.Play();
}
Your scripts 'anim' variable is an Animation. Normally to adjust animation parameters you set them on an Animator, not Animation. But perhaps your solution can work.
For clarity. An animator organizes how animations play out, when, for how long and how they transition.
I would personally advise you to use an animator with behaviour patterns and transitionings between animations. Instead of the current approach.
Either that or use one of the many Tweening asset packs from the asset store. They are specifically made to create UI effects like what you describe.
At least consider these solutions if you keep being stuck :-)

Disable a group of buttons' Interactable state by Tag

I have two groups of buttons on my play area that I'd like to toggle between. I currently have each group tagged differently in the editor and I have another button that I intend to use as the toggle to swap which buttons can be interacted with. Here's what I've got so far, but no luck. I'm getting the error on the IsInteractable line: No overload for method IsInteractable' takes1' arguments.
Where am I going wrong?
if(keySet == true)
{
// turn ON Interactable for Alpha buttons
for(int i = 0; i > GameSetup.alphaKeys.Length; i++)
{
GameObject alphaButton = GameObject.FindGameObjectWithTag("AlphaKey");
alphaButton.GetComponent<UnityEngine.UI.Button>().IsInteractable(true);
}
// turn OFF Interactable for Shape buttons
for(int i = 0; i > GameSetup.symbolKeys.Length; i++)
{
GameObject alphaButton = GameObject.FindGameObjectWithTag("SymbolKey");
alphaButton.GetComponent<UnityEngine.UI.Button>().IsInteractable(false);
}
}
else
{
// Do the reverse if false
}
Got it! I added a Canvas Group component to each group of buttons and it made it super easy to control all of the properties I needed to access.
public void HandleActiveKeySet (bool keySet)
{
if(keySet == true)
{
GameObject alphaGroup = GameObject.FindGameObjectWithTag("AlphaGroup");
alphaGroup.GetComponent<CanvasGroup>().interactable = true;
alphaGroup.GetComponent<CanvasGroup>().blocksRaycasts = true;
GameObject symbolGroup = GameObject.FindGameObjectWithTag("SymbolGroup");
symbolGroup.GetComponent<CanvasGroup>().interactable = false;
symbolGroup.GetComponent<CanvasGroup>().blocksRaycasts = false;
}
else
{
GameObject alphaGroup = GameObject.FindGameObjectWithTag("AlphaGroup");
alphaGroup.GetComponent<CanvasGroup>().interactable = false;
alphaGroup.GetComponent<CanvasGroup>().blocksRaycasts = false;
GameObject symbolGroup = GameObject.FindGameObjectWithTag("SymbolGroup");
symbolGroup.GetComponent<CanvasGroup>().interactable = true;
symbolGroup.GetComponent<CanvasGroup>().blocksRaycasts = true;
}
}

Touch buttons in XNA (with pressed, released, moved states)

I am trying to make touch buttons in WP8 with all the states (Pressed, Released, Moved), but the TouchLocationState.Released is not working.
Here's my code:
Class variables:
bool touching = false;
int touchID;
Button tempButton;
Button is a separate class with a method to switch states when touched.
The Update method contains the following code:
TouchCollection touchCollection = TouchPanel.GetState();
if (!touching && touchCollection.Count > 0)
{
touching = true;
foreach (TouchLocation location in touchCollection)
{
for (int i = 0; i < menuButtons.Count; i++)
{
touchID = location.Id; // store the ID of current touch
Point touchLocation = new Point((int)location.Position.X, (int)location.Position.Y); // create a point
Button button = menuButtons[i];
if (GetMenuEntryHitBounds(button).Contains(touchLocation)) // a method which returns a rectangle.
{
button.SwitchState(true); // change the button state
tempButton = button; // store the pressed button for accessing later
}
}
}
}
else if (touchCollection.Count == 0) // clears the state of all buttons if no touch is detected
{
touching = false;
for (int i = 0; i < menuButtons.Count; i++)
{
Button button = menuButtons[i];
button.SwitchState(false);
}
}
menuButtons is a list of buttons on the menu.
A separate loop (within the Update method) after the touched variable is true
if (touching)
{
TouchLocation location;
TouchLocation prevLocation;
if (touchCollection.FindById(touchID, out location))
{
if (location.TryGetPreviousLocation(out prevLocation))
{
Point point = new Point((int)location.Position.X, (int)location.Position.Y);
if (prevLocation.State == TouchLocationState.Pressed && location.State == TouchLocationState.Released)
{
if (GetMenuEntryHitBounds(tempButton).Contains(point))
// Execute the button action. I removed the excess
}
}
}
}
The code for switching the button state is working fine but the code where I want to trigger the action is not.
location.State == TouchLocationState.Released mostly ends up being false.
(Even after I release the touch, it has a value of TouchLocationState.Moved)
And what is more irritating that it sometimes works!
I am really confused and stuck for days now. Is this the right way? If yes then where am I going wrong? Or is there some other more effective way to do this?
I finally found the solution myself. It does not need the released state of the TouchLocationState
Posting it here. Hopefully it'll help others.
Thanks if anyone was trying.
The class variables are renamed:
private Point _touchPoint;
private TouchLocation _touchLocation;
private int _touchID;
private Button _selectedButton;
private bool _touched;
private bool _launchEvent;
The update method now has the following code
TouchCollection touchCollection = TouchPanel.GetState();
if (!_touched && touchCollection.Count > 0)
{
_touched = false;
_launchEvent = false;
foreach (TouchLocation location in touchCollection)
{
for (int i = 0; i < menuButtons.Count; i++)
{
Button button = menuButtons[i];
_touchID = location.Id;
_touchPoint = new Point((int)location.Position.X, (int)location.Position.Y);
if (GetButtonHitBounds(button).Contains(_touchPoint))
{
button.SwitchState(true);
_selectedButton = button;
}
}
}
}
else if (touchCollection.Count == 0)
{
_touched = false;
for (int i = 0; i < menuButtons.Count; i++)
{
Button button = menuButtons[i];
button.SwitchState(false);
if (GetButtonHitBounds(button).Contains(_touchPoint) && _launchEvent)
{
OnReleased(i, PlayerIndex.One);
_launchEvent = false;
}
}
}
///
// This if statement checks whether the touch is still inside the button area.
// Then assigns a value of true to the _launchEvent variable.
//
// The 'try' block is used because if the first touch is not on button, then the
// value of the _selectedButton is null and it will throw an exception.
///
if (touchCollection.FindById(_touchID, out _touchLocation))
{
if (_touchLocation.State == TouchLocationState.Moved)
{
try
{
if (GetButtonHitBounds(_selectedButton).Contains((int)_touchLocation.Position.X, (int)_touchLocation.Position.Y))
_launchEvent = true;
else
_launchEvent = false;
}
catch { }
}
}

Categories

Resources