-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeakGameObject.cs
More file actions
47 lines (41 loc) · 1.22 KB
/
WeakGameObject.cs
File metadata and controls
47 lines (41 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Linq;
using UnityEngine;
namespace LostInBardo
{
[Serializable]
public struct WeakGameObject
{
[SerializeField] private GameObject _cachedGameObject;
[SerializeField] private string _cachedName;
[SerializeField] private string _cachedScene;
public string Name { get { return _cachedName; } }
public string Scene { get { return _cachedScene; } }
public GameObject GameObject
{
get
{
if (string.IsNullOrEmpty(_cachedName)) return null;
if (_cachedGameObject != null) return _cachedGameObject;
var cachedName = _cachedName;
var foundObj = Resources.FindObjectsOfTypeAll<Transform>().FirstOrDefault(transform =>
!string.IsNullOrEmpty(transform.gameObject.scene.name) && transform.name == cachedName);
if (foundObj == null) return null;
_cachedGameObject = foundObj.gameObject;
return _cachedGameObject;
}
}
public bool IsNull { get { return string.IsNullOrEmpty(_cachedName); } }
public WeakGameObject(GameObject gameObject)
{
_cachedGameObject = gameObject;
_cachedScene = gameObject.scene.name;
_cachedName = gameObject.name;
}
public void Remove()
{
_cachedGameObject = null;
_cachedName = _cachedScene = string.Empty;
}
}
}