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
Related
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
For instance, I found out that the Unity plugin "Octave3d Level Design" has this feature
I hope to make my own prefab manager, since Octave3d prefab manager is not fit for my need. So the question is: when mouse is overlapping Scene View, how to make mouse seize a object and put it in Scene View?
Edit:
Actually found out that you'll need to use SceneView.lastActiveSceneView.camera as your camera, and you can use Gizmos.DrawMesh() to draw the mesh instead of instantiating..
There's also SceneView.OnSceneGUIDelegate that can help you access the scene view.
here's some sample code on how to set that up
void OnGUI() {
if (objectThatFollowsMouse != null) {
SceneView.onSceneGUIDelegate -= OnSceneGUI;
SceneView.onSceneGUIDelegate += OnSceneGUI;
}
else { SceneView.onSceneGUIDelegate -= OnSceneGUI; }
}
void OnSceneGUI(SceneView sceneView) {
...
}
with some additional editor scripting, you can do something like this
Vector3 mousePoint = Camera.main.ViewportToWorldPoint(Input.mousePosition);
if (objectShouldFollowMouse) {
objectThatFollowsMouse.transform.position = mousePoint;
if (Event.current.type == EventType.MouseUp) {
objectShouldFollowMouse = false;
objectThatFollowsMouse = null;
}
}
if (prefabGotClicked) {
GameObject obj = Object.Instantiate(someObject,mousePoint,Quaternion.identity) as GameObject;
objectShouldFollowMouse = true;
objectThatFollowsMouse = obj;
}
In Octave3d, they implemented this function by:
When select the prefab in the custom editor window, instantiate a preview gameobject of the prefab in Scene, the preview gameobject will follow the mouse (by their custom raycast detection). The preview gameobject's parent is the octave manager or something like this.
When click in the scene, instantiate another gameobject in the scene at the mouse-scene intersection point.
And I think it was a bit overkill, something like Unity's own DragAndDrop is enough: a prefab can be drag from custom editorwindow, and drop on sceneview, hierarchy or object field. And this CUSTOM drag and drop can be implement like this:
In your custom editor window, listen to MouseDownEvent to start the DragAndDrop
In your custom editor window, listen to DragUpdatedEvent to move the dragging object
If you want to detect the Dropping on SceneView or Hierarchy, listen to DragPerform and DragExited in there ongui callback.
Code is here:
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class MyWindow : EditorWindow
{
[MenuItem("Window/MyWindow")]
private static void ShowWindow()
{
GetWindow<MyWindow>("MyWindow");
}
private GameObject prefab;
private bool m_IsDragPerformed = false;
private bool m_IsDragging = false;
private void OnEnable()
{
prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Cube.prefab");
var box = new VisualElement();
box.style.backgroundColor = Color.red;
box.style.flexGrow = 1f;
box.RegisterCallback<MouseDownEvent>(evt =>
{
DragAndDrop.PrepareStartDrag();
DragAndDrop.StartDrag("Dragging");
DragAndDrop.objectReferences = new Object[] { prefab };
Selection.activeGameObject = null;
m_IsDragPerformed = false;
m_IsDragging = true;
});
box.RegisterCallback<DragUpdatedEvent>(evt =>
{
DragAndDrop.visualMode = DragAndDropVisualMode.Move;
});
rootVisualElement.Add(box);
SceneView.duringSceneGui += sv => OnDragEnd();
EditorApplication.hierarchyWindowItemOnGUI += (id, rect) => OnDragEnd();
}
private void OnDragEnd()
{
if (Event.current.type == EventType.DragPerform)
{
m_IsDragPerformed = true;
}
if (Event.current.type == EventType.DragExited)
{
if (m_IsDragging && m_IsDragPerformed)
{
m_IsDragging = false;
m_IsDragPerformed = false;
var go = Selection.activeGameObject;
// Do your **OnDragEnd callback on go** here
}
}
}
private void OnDestroy()
{
SceneView.duringSceneGui -= sv => OnDragEnd();
EditorApplication.hierarchyWindowItemOnGUI -= (id, rect) => OnDragEnd();
}
}
Reference is here, this guy is a genius: How to start DragAndDrop action so that SceneView and Hierarchy accept it like with prefabs
So I have a canvas which doesnt get destroyed because it holds and slider whih shows a progressbar. It also has a button which is interactable when the progress of loading is 0.9.. The problem i am having is when I click the button (see activateNewScene()). Basically what happens is when I click on the button is that i load my scene and my disable my canvas. But the problem is that after I disable my canvas the old scene is still shown for about 0.5 sec and than the new scene is loaded. What I want is that after the canvas gets disabled the new scene should show up. without seeing the old scene for short time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SceneLoaderGameObject : MonoBehaviour {
private SceneLoader sl;
public Canvas cav;
void Start () {
sl = new SceneLoader ();
cav.GetComponent<Canvas> ().enabled = false;
DontDestroyOnLoad (this.gameObject);
DontDestroyOnLoad (cav.transform.parent);
}
public void setNewNameAndLoadScene(string name){
if (cav != null) {
Slider slid = cav.transform.GetChild (1).GetComponent<Slider> ();
Text tx = cav.transform.GetChild (2).GetComponent<Text> ();
Button bttn = cav.transform.GetChild (3).GetComponent<Button> ();
sl.setNewScene (name);
sl.setSliderAndTextToChange (slid, tx);
bttn.onClick.AddListener (() => activateNewScene ());
cav.GetComponent<Canvas> ().enabled = true;
cav.GetComponent<Canvas> ().sortingOrder = 1;
StartCoroutine (sl.LoadAsynchron (name, bttn));
}
}
public void activateNewScene(){
sl.AsgetOP().allowSceneActivation=true;
cav.GetComponent<Canvas> ().sortingOrder = 1;
cav.GetComponent<Canvas> ().enabled = false;
}
}
EDIT: Here is the code which loads the scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SceneLoader {
AsyncOperation operation;
string sceneName;
private Text txx =null;
private Slider slider=null;
public SceneLoader(){
}
public void setSliderAndTextToChange(Slider sl, Text tx){
txx = tx;
slider = sl;
}
public void setNewScene(string sceneName){
if (sceneName.Length == 0) {
throw new UnityException ("Please enter a name");
}
this.sceneName = sceneName;
}
public IEnumerator LoadAsynchron(string myMain, Button bttn){
operation = SceneManager.LoadSceneAsync (myMain);
operation.allowSceneActivation = false;
while (operation.isDone == false) {
float progress = Mathf.Clamp01 (operation.progress / 0.9f);
slider.value = progress;
txx.text = progress * 100f + " %";
if (progress * 100f == 100f) {
bttn.interactable = true;
}
yield return null;
}
}
public AsyncOperation AsgetOP(){
return operation;
}
}
I see. I think its because of the canvas. Try to put the code where you disable the canvas and set the order to 0 in the function OnLevelWasLoaded . So basically this:
void OnLevelWasLoaded(){
cav.GetComponent<Canvas> ().sortingOrder = 0;
cav.GetComponent<Canvas> ().enabled = false;
}
public void activateNewScene(string name){
sl.AsgetOP ().allowSceneActivation = true;
Scene sc = SceneManager.GetSceneByName(name);
if (sc.IsValid ()) {
SceneManager.SetActiveScene(sc);
}
}
I added the scene name to the function to activate it.
When SceneManager was first released, it is known that isDone will only be true in when two conditions are met:
1.When the scene is done loading
2.When scene is activated
I don't know if this is still true but I will assume it is.
Basically, when you set allowSceneActivation to false, condition 2 is not met and you will run into issues. It looks like while (operation.isDone == false) will still be executing causing bttn.interactable = true; to also be executing every frame while you are trying to load new scene. This is my guess.
Replace while (operation.isDone == false) with while (operation.progress < 0.9f) then add bttn.interactable = true; to the end of that function just in-case.
public IEnumerator LoadAsynchron(string myMain, Button bttn)
{
operation = SceneManager.LoadSceneAsync(myMain);
operation.allowSceneActivation = false;
while (operation.progress < 0.9f)
{
float progress = Mathf.Clamp01(operation.progress / 0.9f);
slider.value = progress;
txx.text = progress * 100f + " %";
if (progress * 100f == 100f)
{
bttn.interactable = true;
}
yield return null;
}
if (!bttn.interactable)
bttn.interactable = true;
}
If that does not work, I suggest you use SceneManager.SetActiveScene function. See the enableScene function from this post. I've also seen bugs with this, you may want to file for bug report from the Editor if this is still a problem.
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;
}
}
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 { }
}
}