Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

Commit

Permalink
Fixing issue where the Object Manipulation example fails in Unity 201…
Browse files Browse the repository at this point in the history
…8.3 due to the order of calls to OnEnable and OnAwake (1) being different than previous versions of the editor and (2) exposing an initialization bug in ManipulationSystem.cs. This commit refactors ManipulationSystem.cs to allow access to static member ManipulationSystem.Instance before any calls to non-static member Awake.
  • Loading branch information
small-potatoes committed Feb 16, 2019
1 parent 9448df3 commit 0d6bcd3
Showing 1 changed file with 65 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public class ManipulationSystem : MonoBehaviour
{
private static ManipulationSystem s_Instance = null;

private DragGestureRecognizer m_DragGestureRecognizer = new DragGestureRecognizer();

private PinchGestureRecognizer m_PinchGestureRecognizer = new PinchGestureRecognizer();

private TwoFingerDragGestureRecognizer m_TwoFingerDragGestureRecognizer = new TwoFingerDragGestureRecognizer();

private TapGestureRecognizer m_TapGestureRecognizer = new TapGestureRecognizer();

private TwistGestureRecognizer m_TwistGestureRecognizer = new TwistGestureRecognizer();

/// <summary>
/// Gets the ManipulationSystem instance.
/// </summary>
Expand All @@ -43,7 +53,15 @@ public static ManipulationSystem Instance
{
if (s_Instance == null)
{
Debug.LogError("No instance of ManipulationSystem exists in the scene.");
var manipulationSystems = FindObjectsOfType<ManipulationSystem>();
if (manipulationSystems.Length > 0)
{
s_Instance = manipulationSystems[0];
}
else
{
Debug.LogError("No instance of ManipulationSystem exists in the scene.");
}
}

return s_Instance;
Expand All @@ -53,27 +71,57 @@ public static ManipulationSystem Instance
/// <summary>
/// Gets the Drag gesture recognizer.
/// </summary>
public DragGestureRecognizer DragGestureRecognizer { get; private set; }
public DragGestureRecognizer DragGestureRecognizer
{
get
{
return m_DragGestureRecognizer;
}
}

/// <summary>
/// Gets the Pinch gesture recognizer.
/// </summary>
public PinchGestureRecognizer PinchGestureRecognizer { get; private set; }
public PinchGestureRecognizer PinchGestureRecognizer
{
get
{
return m_PinchGestureRecognizer;
}
}

/// <summary>
/// Gets the two finger drag gesture recognizer.
/// </summary>
public TwoFingerDragGestureRecognizer TwoFingerDragGestureRecognizer { get; private set; }
public TwoFingerDragGestureRecognizer TwoFingerDragGestureRecognizer
{
get
{
return m_TwoFingerDragGestureRecognizer;
}
}

/// <summary>
/// Gets the Tap gesture recognizer.
/// </summary>
public TapGestureRecognizer TapGestureRecognizer { get; private set; }
public TapGestureRecognizer TapGestureRecognizer
{
get
{
return m_TapGestureRecognizer;
}
}

/// <summary>
/// Gets the Twist gesture recognizer.
/// </summary>
public TwistGestureRecognizer TwistGestureRecognizer { get; private set; }
public TwistGestureRecognizer TwistGestureRecognizer
{
get
{
return m_TwistGestureRecognizer;
}
}

/// <summary>
/// Gets the current selected object.
Expand All @@ -85,12 +133,16 @@ public static ManipulationSystem Instance
/// </summary>
public void Awake()
{
_InitializeSingleton();
DragGestureRecognizer = new DragGestureRecognizer();
PinchGestureRecognizer = new PinchGestureRecognizer();
TwoFingerDragGestureRecognizer = new TwoFingerDragGestureRecognizer();
TapGestureRecognizer = new TapGestureRecognizer();
TwistGestureRecognizer = new TwistGestureRecognizer();
if (Instance != this)
{
Debug.LogWarning("Multiple instances of ManipulationSystem detected in the scene." +
" Only one instance can exist at a time. The duplicate instances" +
" will be destroyed.");
DestroyImmediate(gameObject);
return;
}

DontDestroyOnLoad(gameObject);
}

/// <summary>
Expand All @@ -116,7 +168,7 @@ internal void Deselect()
/// <summary>
/// Select an object.
/// </summary>
/// <param name="target">The object to select.</param>
/// <param name="target">The object to select.</param>
internal void Select(GameObject target)
{
if (SelectedObject == target)
Expand All @@ -127,22 +179,5 @@ internal void Select(GameObject target)
Deselect();
SelectedObject = target;
}

private void _InitializeSingleton()
{
if (s_Instance == null)
{
s_Instance = this;
DontDestroyOnLoad(gameObject);
}
else if (Instance != this)
{
Debug.LogWarning("Multiple instances of ManipulationSystem detected in the scene." +
" Only one instance can exist at a time. The duplicate instances" +
" will be destroyed.");
DestroyImmediate(gameObject);
return;
}
}
}
}

0 comments on commit 0d6bcd3

Please sign in to comment.