Skip to content

Instantly share code, notes, and snippets.

@sujoyu
Last active August 3, 2016 04:55
Show Gist options
  • Save sujoyu/90aa1cbcb3cf744b4cbe9f68650c50c3 to your computer and use it in GitHub Desktop.
Save sujoyu/90aa1cbcb3cf744b4cbe9f68650c50c3 to your computer and use it in GitHub Desktop.
A Simple unity script for modal dialogs on uGUI. When click outside the modal, close it.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System.Linq;
public class Closer : MonoBehaviour, IPointerClickHandler {
public int ancestorLevel = 0;
public void OnPointerClick(PointerEventData data) {
var target = Enumerable.Range (0, ancestorLevel)
.Aggregate (gameObject.transform, (acc, _) => acc.transform.parent);
Destroy (target.gameObject);
data.Use ();
}
}
@sujoyu
Copy link
Author

sujoyu commented Aug 3, 2016

Usage

  1. Create a GameObject UI -> Panel under the canvas.
  2. Edit and add components like this. An Image component is required for handling click event. If it is a hindrance, set transparent of the image to 100%.
    2016-08-03 13 23 04
  3. Add GameObjects of dialog panel and it's contents under the created object at 1..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment