-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
105 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using UnityEngine; | ||
|
||
namespace Microsoft.MixedReality.Toolkit | ||
{ | ||
/// <summary> | ||
/// A static helper class which caches instances of a component of type T. | ||
/// This is called in place of calling FindFirstObjectByType and FindObjectsByType to improve performance. | ||
/// </summary> | ||
public static class ComponentCache<T> where T : Component | ||
{ | ||
private static T cacheFirstInstance = null; | ||
|
||
/// <summary> | ||
/// Finds the first instance of an object of the given type. | ||
/// </summary> | ||
/// <returns> | ||
/// The first instance of an object of the given type. | ||
/// </returns> | ||
public static T FindFirstActiveInstance() | ||
{ | ||
_ = TryFindFirstActiveInstance(out T result); | ||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// Finds the first instance of an object of the given type. | ||
/// </summary> | ||
/// <returns> | ||
/// True if an instance was found, and false otherwise. | ||
/// </returns> | ||
public static bool TryFindFirstActiveInstance(out T result) | ||
{ | ||
if (cacheFirstInstance == null || !cacheFirstInstance.gameObject.activeInHierarchy) | ||
{ | ||
cacheFirstInstance = Object.FindFirstObjectByType<T>(); | ||
} | ||
|
||
result = cacheFirstInstance; | ||
return result != null; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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