Maxime FRAPPAT

Hum …no thanks ! – Lordinaire

Tag: WP8 (Page 2 of 2)

[Unity][iOS/WP8/Android] Tap gesture helper

An helper class to handle single tap gesture in 2D games. It uses the raycast mechanism to detect collisions.

using UnityEngine;

namespace Assets.Scripts.Framework
{
	public static class TapHelper
	{
		/// <summary>
		/// Get touch position
		/// </summary>
		/// <returns>Current touch position</returns>
		public static Vector3 GetTouchedPosition()
		{
			Input.simulateMouseWithTouches = true;
			if (Input.touchCount > 0 || Input.GetMouseButtonDown(0))
			{
#if UNITY_EDITOR
                return Input.mousePosition;
#else
				return Input.touches[0].position;

#endif
			}
			return Vector3.zero;
		}

		/// <summary>
		/// Return if a game object is touch
		/// </summary>
		/// <param name="name">game object name</param>
		/// <returns>true if touched, otherwise false</returns>
		public static bool IsGameObjectTouched(string name)
		{
			Input.simulateMouseWithTouches = true;
			if (Input.touchCount > 0 || Input.GetMouseButtonDown(0))
			{
#if UNITY_EDITOR
                var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 1f);
#else
				var hit = Physics2D.Raycast(Camera.main.ScreenPointToRay(Input.touches[0].position).origin, Vector2.zero, 1f);
#endif

				if (hit.collider != null)
				{
					return hit.collider.name == name;
				}
			}

			return false;
		}

		/// <summary>
		/// Return if a game object is touched
		/// </summary>
		/// <param name="objectToDetect">instance of a game object</param>
		/// <returns>true if touched, otherwise false</returns>
		public static bool IsGameObjectTouched(GameObject objectToDetect)
		{
			Input.simulateMouseWithTouches = true;
			if (Input.touchCount > 0 || Input.GetMouseButtonDown(0))
			{
#if UNITY_EDITOR
                var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 1f);
#else
				var hit = Physics2D.Raycast(Camera.main.ScreenPointToRay(Input.touches[0].position).origin, Vector2.zero, 1f);
#endif

				if (hit.collider != null)
				{
					return hit.collider.gameObject == objectToDetect;
				}
			}

			return false;
		}
	}
}

[Unity] Interact between Unity and Windows Phone 8 / Windows Store apps

In this thread, we will see how to interact between Unity and our WP8 or Windows Store apps.

Introduction

When you work with those platforms with Unity, it will create a Visual Studio solution with all the basics of a new solution for this kind of project (App.xaml, MainPage.xaml, …) adapted to be able  to start your game.

In this new solution, we can have access to all the framework of this platform, it’s platform specific.

Create a gateway

We want to be able to launch a platform specific action, like sharing a picture, in our Unity source code that can be handle by our WP8/Windows Store project.

using System;

/// <summary>
/// Windows specific and interop between Unity and Windows Store or Windows Phone 8
/// </summary>
public static class WindowsGateway
{
	public static Action<string, string, string> OnActionWithParametersRaised = delegate { };

	public static Action OnActionRaised = delegate { };

	static WindowsGateway()
	{
		OnActionWithParametersRaised = delegate { };
		OnActionRaised = delegate { };
	}
}

We use a static class with empty delegates. Those delegates will be handle in our platform specific project, in the MainPage.xaml.cs for example.

namespace MyUnityApp
{
	public partial class MainPage : PhoneApplicationPage
	{
		// [...]

		private void Unity_Loaded()
		{
			// [...]

			WindowsGateway.OnActionWithParametersRaised += OnActionWithParameters;
			WindowsGateway.OnActionRaised += OnAction;
		}

		private void OnAction()
		{

		}

		private void OnActionWithParameters(string url, string title, string message)
		{

		}

		// [...]

	}
}

That’s all :)

Page 2 of 2

Powered by WordPress & Theme by Anders Norén

%d bloggers like this: