Skip to content

Commit 7f93eea

Browse files
committed
feat(Util): Add find object with in scene
1 parent d54115f commit 7f93eea

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

Assets/JCSUnity/Scripts/Util/JCS_Util.cs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -814,23 +814,38 @@ public static string RemoveCloneString(Object obj)
814814
/// </summary>
815815
public static Object FindObjectByType(System.Type type)
816816
{
817-
#if UNITY_2023_1_OR_NEWER
818-
return UnityEngine.Object.FindFirstObjectByType(type);
819-
#else
820-
return UnityEngine.Object.FindObjectOfType(type);
821-
#endif
817+
return Object.FindFirstObjectByType(type);
818+
}
819+
public static Object FindObjectByType(System.Type type, Scene scene)
820+
{
821+
Object[] objects = FindObjectsByType(type, scene);
822+
823+
if (objects.Length == 0)
824+
return null;
825+
826+
return objects.First();
822827
}
823828

824829
/// <summary>
825830
/// Retrieves a list of all loaded objects of Type type.
826831
/// </summary>
827-
public static UnityEngine.Object[] FindObjectsByType(System.Type type)
832+
public static Object[] FindObjectsByType(System.Type type)
833+
{
834+
return Object.FindObjectsByType(type, FindObjectsSortMode.None);
835+
}
836+
public static Object[] FindObjectsByType(System.Type type, Scene scene)
828837
{
829-
#if UNITY_2023_1_OR_NEWER
830-
return UnityEngine.Object.FindObjectsByType(type, FindObjectsSortMode.None);
831-
#else
832-
return UnityEngine.Object.FindObjectsOfType(type);
833-
#endif
838+
Object[] objects = FindObjectsByType(type);
839+
840+
return objects.Where((obj) =>
841+
{
842+
var trans = obj.GetComponent<Transform>();
843+
844+
if (trans == null)
845+
return false;
846+
847+
return trans.gameObject.scene == scene;
848+
}).ToArray();
834849
}
835850

836851
/// <summary>

0 commit comments

Comments
 (0)