Skip to content
This repository was archived by the owner on Oct 15, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public class NoSpheroConnectedView : MonoBehaviour {

// UI Padding Variables
int m_ViewPadding = 20;

private ThreadSafeLoadLevel m_threadSafeLoadLevel;

void Awake()
{
m_threadSafeLoadLevel = ThreadSafeLoadLevel.Instance;
}

void Start () {
ViewSetup();
Expand Down Expand Up @@ -59,7 +66,10 @@ private void ReceiveNotificationMessage(object sender, SpheroDeviceMessenger.Mes
SpheroDeviceNotification message = (SpheroDeviceNotification)eventArgs.Message;
if( message.NotificationType == SpheroDeviceNotification.SpheroNotificationType.CONNECTED ) {
// Go to the desired scene
Application.LoadLevel (m_NextLevel);
if (m_threadSafeLoadLevel != null)
{
m_threadSafeLoadLevel.LoadLevel(m_NextLevel);
}
}
}

Expand Down Expand Up @@ -125,7 +135,10 @@ void OnGUI() {
getASpheroButtonY = backgroundY+(backgroundHeight*0.85f) - (buttonHeight/2);
// If the get a Sphero button is clicked
if( GUI.Button (new Rect(getASpheroButtonX, getASpheroButtonY,buttonWidth,buttonHeight), "") ) {
Application.LoadLevel("SpheroConnectionScene");
if (m_threadSafeLoadLevel != null)
{
m_threadSafeLoadLevel.LoadLevel("SpheroConnectionScene");
}
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class SpheroConnectionView : MonoBehaviour {
Vector2 windowMargin = new Vector2(0,0);
Vector2 listMargin = new Vector2(40,40);
private Rect windowRect;

private ThreadSafeLoadLevel m_threadSafeLoadLevel;

/* Use this to initialize the view */
private void ViewSetup() {
Expand All @@ -72,6 +74,11 @@ private void ViewSetup() {
// Display that it doesn't work with these platforms?
#endif
}

void Awake()
{
m_threadSafeLoadLevel = ThreadSafeLoadLevel.Instance;
}

/* Use these for initialization */
void Start () {
Expand Down Expand Up @@ -106,7 +113,10 @@ void SetupIOS() {
*/
void CheckForSpheroConnection() {
if( m_SpheroProvider.GetConnectedSpheros().Length == 0 ) {
Application.LoadLevel("NoSpheroConnectedScene");
if (m_threadSafeLoadLevel != null)
{
m_threadSafeLoadLevel.LoadLevel("NoSpheroConnectedScene");
}
}
}

Expand Down Expand Up @@ -157,7 +167,10 @@ private void ReceiveNotificationMessage(object sender, SpheroDeviceMessenger.Mes
if( !m_MultipleSpheros ) {
m_Title = "Connection Success";
SpheroDeviceMessenger.SharedInstance.NotificationReceived -= ReceiveNotificationMessage;
Application.LoadLevel(m_NextLevel);
if (m_threadSafeLoadLevel != null)
{
m_threadSafeLoadLevel.LoadLevel(m_NextLevel);
}
}
}
else if( message.NotificationType == SpheroDeviceNotification.SpheroNotificationType.CONNECTION_FAILED ) {
Expand Down Expand Up @@ -281,7 +294,10 @@ void OnGUI() {
// Check if we are done adding robots
if( buttonLabel.Equals("Done") ){
SpheroDeviceMessenger.SharedInstance.NotificationReceived -= ReceiveNotificationMessage;
Application.LoadLevel(m_NextLevel);
if (m_threadSafeLoadLevel != null)
{
m_threadSafeLoadLevel.LoadLevel(m_NextLevel);
}
}
// Check if we have a Sphero connected
else if( m_SpheroLabelSelected >= 0 ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Threading;
using UnityEngine;

/// <summary>
/// The goal of this class is to let you load levels (from the Sphero callbacks) in a way that
/// doesn't try to make Unity calls on a thread other than the Unity thread. Unity takes issue with
/// this and will throw a crashing-tantrum.
/// </summary>
public class ThreadSafeLoadLevel : MonoBehaviour
{
#region Static instance management
/// <summary>
/// Static instance of this script
/// </summary>
private static ThreadSafeLoadLevel s_instance;

/// <summary>
/// No creating an instance after OnApplicationQuit is called - can get funky in the editor
/// </summary>
private static bool m_exiting;

/// <summary>
/// Gets the static instance of his script. This is *NOT* thread safe. Call it from Unity's
/// thread (creates a Unity object)
/// </summary>
public static ThreadSafeLoadLevel Instance
{
get
{
if (s_instance == null && !m_exiting)
{
var go = new GameObject("ThreadSafeLoadLevel");
s_instance = go.AddComponent<ThreadSafeLoadLevel>();
}
return s_instance;
}
}
#endregion

/// <summary>
/// An object to lock on
/// </summary>
private readonly Object m_lock = new Object();

/// <summary>
/// The level we want to load
/// </summary>
private string m_levelToLoad;

#region Unity-style callbacks
void Update()
{
// non-blocking lock acquire
if (Monitor.TryEnter(m_lock))
{
if (!string.IsNullOrEmpty(m_levelToLoad))
{
Application.LoadLevel(m_levelToLoad);
}

Monitor.Exit(m_lock);
}
}

void OnApplicationQuit()
{
m_exiting = true;
}
#endregion

/// <summary>
/// Loads the given level by name
/// </summary>
/// <param name="levelName">a level we wish to load</param>
public void LoadLevel(string levelName)
{
lock (m_lock)
{
m_levelToLoad = levelName;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public class NoSpheroConnectedView : MonoBehaviour {

// UI Padding Variables
int m_ViewPadding = 20;

private ThreadSafeLoadLevel m_threadSafeLoadLevel;

void Awake()
{
m_threadSafeLoadLevel = ThreadSafeLoadLevel.Instance;
}

void Start () {
ViewSetup();
Expand Down Expand Up @@ -59,7 +66,10 @@ private void ReceiveNotificationMessage(object sender, SpheroDeviceMessenger.Mes
SpheroDeviceNotification message = (SpheroDeviceNotification)eventArgs.Message;
if( message.NotificationType == SpheroDeviceNotification.SpheroNotificationType.CONNECTED ) {
// Go to the desired scene
Application.LoadLevel (m_NextLevel);
if (m_threadSafeLoadLevel != null)
{
m_threadSafeLoadLevel.LoadLevel(m_NextLevel);
}
}
}

Expand Down Expand Up @@ -125,7 +135,10 @@ void OnGUI() {
getASpheroButtonY = backgroundY+(backgroundHeight*0.85f) - (buttonHeight/2);
// If the get a Sphero button is clicked
if( GUI.Button (new Rect(getASpheroButtonX, getASpheroButtonY,buttonWidth,buttonHeight), "") ) {
Application.LoadLevel("SpheroConnectionScene");
if (m_threadSafeLoadLevel != null)
{
m_threadSafeLoadLevel.LoadLevel("SpheroConnectionScene");
}
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class SpheroConnectionView : MonoBehaviour {
Vector2 windowMargin = new Vector2(0,0);
Vector2 listMargin = new Vector2(40,40);
private Rect windowRect;

private ThreadSafeLoadLevel m_threadSafeLoadLevel;

/* Use this to initialize the view */
private void ViewSetup() {
Expand All @@ -72,6 +74,11 @@ private void ViewSetup() {
// Display that it doesn't work with these platforms?
#endif
}

void Awake()
{
m_threadSafeLoadLevel = ThreadSafeLoadLevel.Instance;
}

/* Use these for initialization */
void Start () {
Expand Down Expand Up @@ -106,7 +113,10 @@ void SetupIOS() {
*/
void CheckForSpheroConnection() {
if( m_SpheroProvider.GetConnectedSpheros().Length == 0 ) {
Application.LoadLevel("NoSpheroConnectedScene");
if (m_threadSafeLoadLevel != null)
{
m_threadSafeLoadLevel.LoadLevel("NoSpheroConnectedScene");
}
}
}

Expand Down Expand Up @@ -157,7 +167,10 @@ private void ReceiveNotificationMessage(object sender, SpheroDeviceMessenger.Mes
if( !m_MultipleSpheros ) {
m_Title = "Connection Success";
SpheroDeviceMessenger.SharedInstance.NotificationReceived -= ReceiveNotificationMessage;
Application.LoadLevel(m_NextLevel);
if (m_threadSafeLoadLevel != null)
{
m_threadSafeLoadLevel.LoadLevel(m_NextLevel);
}
}
}
else if( message.NotificationType == SpheroDeviceNotification.SpheroNotificationType.CONNECTION_FAILED ) {
Expand Down Expand Up @@ -281,7 +294,10 @@ void OnGUI() {
// Check if we are done adding robots
if( buttonLabel.Equals("Done") ){
SpheroDeviceMessenger.SharedInstance.NotificationReceived -= ReceiveNotificationMessage;
Application.LoadLevel(m_NextLevel);
if (m_threadSafeLoadLevel != null)
{
m_threadSafeLoadLevel.LoadLevel(m_NextLevel);
}
}
// Check if we have a Sphero connected
else if( m_SpheroLabelSelected >= 0 ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Threading;
using UnityEngine;

/// <summary>
/// The goal of this class is to let you load levels (from the Sphero callbacks) in a way that
/// doesn't try to make Unity calls on a thread other than the Unity thread. Unity takes issue with
/// this and will throw a crashing-tantrum.
/// </summary>
public class ThreadSafeLoadLevel : MonoBehaviour
{
#region Static instance management
/// <summary>
/// Static instance of this script
/// </summary>
private static ThreadSafeLoadLevel s_instance;

/// <summary>
/// No creating an instance after OnApplicationQuit is called - can get funky in the editor
/// </summary>
private static bool m_exiting;

/// <summary>
/// Gets the static instance of his script. This is *NOT* thread safe. Call it from Unity's
/// thread (creates a Unity object)
/// </summary>
public static ThreadSafeLoadLevel Instance
{
get
{
if (s_instance == null && !m_exiting)
{
var go = new GameObject("ThreadSafeLoadLevel");
s_instance = go.AddComponent<ThreadSafeLoadLevel>();
}
return s_instance;
}
}
#endregion

/// <summary>
/// An object to lock on
/// </summary>
private readonly Object m_lock = new Object();

/// <summary>
/// The level we want to load
/// </summary>
private string m_levelToLoad;

#region Unity-style callbacks
void Update()
{
// non-blocking lock acquire
if (Monitor.TryEnter(m_lock))
{
if (!string.IsNullOrEmpty(m_levelToLoad))
{
Application.LoadLevel(m_levelToLoad);
}

Monitor.Exit(m_lock);
}
}

void OnApplicationQuit()
{
m_exiting = true;
}
#endregion

/// <summary>
/// Loads the given level by name
/// </summary>
/// <param name="levelName">a level we wish to load</param>
public void LoadLevel(string levelName)
{
lock (m_lock)
{
m_levelToLoad = levelName;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading