Last active
August 29, 2015 14:08
-
-
Save alchemycs/67623a82a50e7eef2b9b to your computer and use it in GitHub Desktop.
Unity 3D Touchable/Clickable Hyperlinked Objects with 'guiText' Components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* For use with Unity 4.5 | |
*/ | |
using UnityEngine; | |
using System.Collections; | |
public class OpenWebLink : MonoBehaviour { | |
public string url; | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
if (IsClicked() || IsTouched()) { | |
Application.OpenURL(url); | |
} | |
} | |
bool IsTouched() { | |
bool touched = false; | |
if (Input.touchCount == 1) { | |
Touch touch = Input.touches[0]; | |
if (touch.tapCount == 1 && touch.phase == TouchPhase.Ended) { | |
if (guiText.HitTest(touch.position)) { | |
touched = true; | |
} | |
} | |
} | |
return touched; | |
} | |
bool IsClicked() { | |
bool clicked = false; | |
if (Input.GetMouseButtonUp(0)) { | |
if (guiText.HitTest(Input.mousePosition)) { | |
clicked = true; | |
} | |
} | |
return clicked; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* For use with Unity 4.6 | |
*/ | |
using UnityEngine; | |
using UnityEngine.EventSystems; | |
using System.Collections; | |
public class OpenWebLink : MonoBehaviour, IPointerClickHandler { | |
public string url; | |
public void Start() { | |
if (string.IsNullOrEmpty(url)) { | |
Debug.LogWarning("No URL has been set in the OpenWebLink component, disabling component"); | |
enabled = false; | |
} | |
} | |
public void OnPointerClick(PointerEventData eventData) { | |
OpenURL(); | |
} | |
public void OpenURL() { | |
Application.OpenURL(url); | |
} | |
public void OpenURL(string aUrl) { | |
Application.OpenURL(aUrl); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The updated class for Unity 4.6 is smaller and also works on any of the new GUI components.