Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example use of the Unity 4.1 AirPlay API - gives a setup with the iOS device as controller of the remote display.
using UnityEngine;
using System.Collections;
/*
Runtime use:
To be available, AirPlay needs to be switched on for the device either via a native AirPlay UI component or
via the global AirPlay mirroring setting. Your options are:
A: Modify your Xcode project to layer a native AirPlay Cocoa control somewhere over your Unity content.
B: Enable global AirPlay mirroring on the device.
- You will find this next to the global volume control - available when you slide the multi-task bar
(accessible from double-home-button-click) to the right.
- If AirPlay receivers are available on the network, you should see an AirPlay button next to the volume
control.
- Tapping it pops a target selector up and with a display capable target selected, toggle on "mirroring".
- Returning to your application, AirPlay should now be available.
Though it is a more cumbersome user experience, I would recommend going with option B as long as it makes
sense, with an in-game description - to keep app simplicity.
If you want to test AirPlay, but do not have an AppleTV, I have successfully used AirServer for testing:
http://www.airserverapp.com
*/
namespace UnityAssets
{
public class DualDisplay : MonoBehaviour
{
public Camera mainCamera, controlsCamera;
public bool autoEnable = true;
public bool Available
{
get
{
return enabled && Display.displays.Length > 1;
}
}
public bool Active
{
get
{
return enabled && controlsCamera.enabled;
}
set
{
if (!value || !Available)
{
controlsCamera.enabled = false;
controlsCamera.SetTargetBuffers (Display.main.colorBuffer, Display.main.depthBuffer);
mainCamera.SetTargetBuffers (Display.main.colorBuffer, Display.main.depthBuffer);
}
else
{
Display secondDisplay = Display.displays[1];
mainCamera.SetTargetBuffers (secondDisplay.colorBuffer, secondDisplay.depthBuffer);
controlsCamera.SetTargetBuffers (Display.main.colorBuffer, Display.main.depthBuffer);
controlsCamera.enabled = true;
}
}
}
void Reset ()
{
mainCamera = Camera.main;
}
void Start ()
{
if (mainCamera == null || controlsCamera == null)
{
Debug.LogError ("DualDisplay missing a camera reference");
Destroy (this);
return;
}
controlsCamera.enabled = false;
}
void Update()
{
if (Available)
{
if (autoEnable)
{
Active = true;
}
}
else
{
Active = false;
}
}
}
}
@AngryAnt

This comment has been minimized.

Copy link
Owner Author

commented Mar 14, 2013

Initial version was using old API names. Sorry about that. Now updated.

@AngryAnt

This comment has been minimized.

Copy link
Owner Author

commented Mar 15, 2013

Added runtime use description.

@ghost

This comment has been minimized.

Copy link

commented Mar 15, 2013

it can only find one display with an ipad 2 with ios 6.1.2, i tried with airserver and reflector on osx 10.8.2, both the same, just one display and just the main camera shows up, is there anything extra to set up?

@superblockio

This comment has been minimized.

Copy link

commented Mar 27, 2013

This has been my experience as well, with an iPhone 5 and Apple TV or Airserver, or plugged directly in via lightening to HDMI. Display.displays.Length always returns 1. For now I print out Display.displays.Length to an NGUI label in my Update method, and I've checked to make sure it is actually updating. Unity just never detects the extra display. Am I missing something? And yes, I've enabled airplay mirroring and can see everything on both screens.

@SoundGuy

This comment has been minimized.

Copy link

commented May 24, 2013

I'm trying to do this with my iPAD 2, mirroring is on. i have a debug string on the screen showing "Display.displays.Length" which always returns 1. any ideas why or what should i do?

@jquentin

This comment has been minimized.

Copy link

commented Aug 28, 2013

Thanks for this example.
Do you know how to display a camera on both the device AND the external screen?
Thanks!

@vitapoly

This comment has been minimized.

Copy link

commented Nov 5, 2013

Thanks for this example. For those that have Display.displays.Length == 1, it's because AirPlay is only for Unity Pro.

@Neogene

This comment has been minimized.

Copy link

commented Feb 20, 2014

I'm working on apple tv v3 and iPad mini retina, the mirroring doesn't work at full quality , it seem streamed to a lower quality, i added the Display.displays[1].SetRenderingResolution(1920,1080) to force the full resolution to the secondary display without success, any suggestion?

@johugh

This comment has been minimized.

Copy link

commented Mar 25, 2014

I am experiencing bad quality in dual display as well. Antialisasing seems to not be working on the second display. Tried on AirServer and AppleTV2. Was this something anyone else experiences?

@PAHeartBeat

This comment has been minimized.

Copy link

commented Mar 10, 2015

Hi Guys I have tried it for a protrait game with AirServer Trial version and iPod 5th Gen, this script work perfect. but I notice main camera should behave like landscape game in AirServer (MAC). any guess about that...? I cant find any property about rstrict aspect of Main Cam which show on Air Server (second display or so on...)

I also have some confusiona about this line....
A: Modify your Xcode project to layer a native AirPlay Cocoa control somewhere over your Unity content.

@juxus1

This comment has been minimized.

Copy link

commented Feb 7, 2017

Hallo,

i would like to send or mirror via HDMI but in full hd without black boarders on the sides. i want to display a vuforia AR app on a large tv.

@zcwaa22

This comment has been minimized.

Copy link

commented Oct 13, 2018

Has anyone managed to get this working with Image Effects on the Airplay screen, they refuse to work for me on Unity 2018.2.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.