Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: doppelmonster on March 03, 2015, 04:56:48 AM

Title: Blocking click through uGUI
Post by: doppelmonster on March 03, 2015, 04:56:48 AM
Hello,
I created two custom actions to recognize if a pointer (mouse/touch) is over a uGUI object. That way you can prevent that those clicks are going into your 3d world.

Edit:
Please note that these actions currently dont work on mobile!!!

The first one is a general action which checks if the pointer is over a GUI object and sends events accordingly. The other action is an improvement of the Mouse Pick action. It has the feature added to optionally check if the mouse is over the GUI and returns null then (similar to the layer mask).


Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using UnityEngine;
using UnityEngine.EventSystems;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("uGui")]
[Tooltip("Checks if Pointer is over an uGui object and returns an event or bool ")]
public class uGuiCheckPointerOverGUI : FsmStateAction
{
[Tooltip("Event to send when the Pointer is over an uGui object.")]
public FsmEvent PointerOverGui;

[Tooltip("Event to send when the Pointer is NOT over an uGui object.")]
public FsmEvent PointerNotOverGui;

[UIHint(UIHint.Variable)]
public FsmBool isPointerOverGui;

[Tooltip("Repeat every frame.")]
public bool everyFrame;

public override void Reset()
{

PointerOverGui = null;
PointerNotOverGui = null;
isPointerOverGui = null;
everyFrame = false;
}

public override void OnEnter()
{
DoCheckPointer();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate()
{
DoCheckPointer();
}


void DoCheckPointer()
{
if (EventSystem.current.IsPointerOverGameObject())
{
Fsm.Event(PointerOverGui);
isPointerOverGui.Value = true;
}
else
{
Fsm.Event(PointerNotOverGui);
isPointerOverGui.Value = false;

}



}

}
}
Title: Re: Blocking click through uGUI
Post by: jeanfabre on March 04, 2015, 02:45:48 AM
Hi,

 did ou tested them? I can't get the isPointerOverGameObject to work, I may miss something obvious?

 Bye,

 Jean
Title: Re: Blocking click through uGUI
Post by: doppelmonster on March 04, 2015, 03:41:32 AM
Hmm,  I tested both actions in the Editor (PC) and they worked both.

Right now i only use the "uGuiCheckPointerOverGUI" action with events (not the bool) and its working in the editor on Mac and PC and in the Webplayer as well.

Where its not working is iOS (just discovered)! I have to check that again.

Where did you tested it?
Title: Re: Blocking click through uGUI
Post by: jeanfabre on March 04, 2015, 03:45:00 AM
Hi,

 mac, I'll have another go tomorow, have to move on to other stuff.

Bye,

 Jean
Title: Re: Blocking click through uGUI
Post by: doppelmonster on March 06, 2015, 04:47:36 AM
I made a lot of tests and research and i cannot find a working solution for mobile.

There seems to be an official answer of Unity support to the problem but I am not skilled enough to understand/implement the solution:

http://forum.unity3d.com/threads/touch-press-pass-through-all-ui-elements.272892/#post-1830840 (http://forum.unity3d.com/threads/touch-press-pass-through-all-ui-elements.272892/#post-1830840)

I also tried to use the Event Trigger Component with "Pointer enter" , "Pointer Exit" Events switching a FSM bool, this looked promising and works in the Editor. However when testing on mobile (iOS) the "pointer exit" event is never raised, but "pointer enter" is ... which makes it useless.
Title: Re: Blocking click through uGUI
Post by: jeanfabre on March 06, 2015, 05:55:06 AM
Hi,

 Yeah, I am bit puzzled and can't make it work myself.

have you tried: "Standalone Input Module" has "Allow Activation On Mobile Device", maybe this is the issue for your mobile tests. meanwhile , I just trying to get it to work in Unity itself, in all cases, and it's a bit of a pain...


Bye,

 Jean
Title: Re: Blocking click through uGUI
Post by: doppelmonster on March 06, 2015, 07:01:25 AM
Hi Jean,
you brought me on the right track! The actions are working on touch devices if you enable  "Allow Activation On Mobile Device" but you have to disable the "Touch Input Module" Component on the EventSystem at the same time!

Following this discussion:
http://forum.unity3d.com/threads/standalone-input-module-touch-input-module-behavior-difference.265309/ (http://forum.unity3d.com/threads/standalone-input-module-touch-input-module-behavior-difference.265309/)

I will do further testing if everything else is working when disabling "Touch Input Module"

Strange that you have such problems to get it to work at all ....
Title: Re: Blocking click through uGUI
Post by: jeanfabre on March 06, 2015, 09:12:33 AM
Hi,

 could you send me a pacakged scene that works? that would be great, I must be doing something very wrong...

 Bye,

 Jean
Title: Re: Blocking click through uGUI
Post by: doppelmonster on March 07, 2015, 05:38:09 AM
Attached you find a sample project. set your aspect ratio to 1280x720 to have overlapping gui/3d stuff. clicking the cube will rotate him. click the button to shake him. notice that the button and the panel block the rotation action. Tested in Editor only (should work on mobile as well when mouse down/up actions are replaced by touch actions). Check the component settings on the event system as well.
Title: Re: Blocking click through uGUI
Post by: jeanfabre on March 23, 2015, 03:03:39 AM
Hi,

 With what version did you packaged this? I have a script missing instead of a fsm on your scene.

 Bye,

 Jean
Title: Re: Blocking click through uGUI
Post by: polygon on March 23, 2015, 05:43:03 AM
Hi doppelmonster,

thanks for the reply in my thread (http://hutonggames.com/playmakerforum/index.php?topic=9892.0 (http://hutonggames.com/playmakerforum/index.php?topic=9892.0)) and posting the link to this thread!

The solution described above works really well in my case and I did some tests to confirm this. I created a very simple custom action that sends an event if the touch did not hit a ui object:

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using UnityEngine;
using UnityEngine.EventSystems;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Device)]
[Tooltip("Sends an event if a touch input does not hit a gui element.")]
public class TouchEventNotOnGuiPF : FsmStateAction
{
public FsmEvent sendEvent;
[UIHint(UIHint.Variable)]

public override void Reset()
{
sendEvent = null;
}

public override void OnUpdate()
{
if (Input.touchCount == 1) {

if (!EventSystem.current.IsPointerOverGameObject (-1)) {
Fsm.Event (sendEvent);
Debug.Log ("Touch Object!");
}

else {
Debug.Log ("Touch GUI!");
return;
}
}

}
}
}


I'm not really sure why there must be "-1" in (!EventSystem.current.IsPointerOverGameObject (-1)) becasue I thought that touch #1 equals id #1 but it works fine for me (you can also remove "-1" and leave the space blank, which will also work...)

This issue really drove me crazy and I really hope that this solution will work for us :)

Best regards,
Daniel
Title: Re: Blocking click through uGUI
Post by: doppelmonster on March 23, 2015, 09:17:59 AM
so you use your own action with my tip to make the changes in the input controllers?

I will check your action as i didnt filter for finger ids. so this might cause my problems.....
Title: Re: Blocking click through uGUI
Post by: polygon on March 23, 2015, 09:23:25 AM
Yep, I use this action in combination with your tip and it works pretty reliable so far :)

The finger id filter is always the tricky part and definately the main problem of this whole issue as far as I can say by now. This is the first piece of code that I've ever written and I hope it will work for you as well. Just let me know if it works for you :)
Title: Re: Blocking click through uGUI
Post by: doppelmonster on March 24, 2015, 03:43:11 AM
@Jean: you should try to change your window layout. I got also a script not found error when i just import playmaker in a fresh project. then switch layout and everything is fine. Maybe its somehow saved into my package....


its created with unity 5 and playmaker 1.78 (first release)
Title: Re: Blocking click through uGUI
Post by: jeanfabre on April 07, 2015, 02:58:07 AM
Hi,

 I could make it work, thanks :)

 Bye,

 Jean
Title: Re: Blocking click through uGUI
Post by: doppelmonster on October 02, 2015, 06:04:50 AM
Just a quick update. it looks like Unity fixed something in the mobile support. The block now also works on iOS (unity 5.2.1f1).
Title: Re: Blocking click through uGUI
Post by: jeanfabre on October 14, 2015, 07:15:17 AM
Hi,

 very nice, thanks for letting us know!

 Bye,

 Jean