Created
November 29, 2021 02:58
-
-
Save shivaduke28/dac8014a0730639c3f0303605a499e14 to your computer and use it in GitHub Desktop.
ObjectReference
This file contains hidden or 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
using System.Linq; | |
using System.Text; | |
using UnityEngine; | |
namespace MyNameSpace | |
{ | |
public class ObjectReference<T> where T : Component | |
{ | |
T value; | |
readonly string rootName; | |
readonly string path; // relative to root | |
public ObjectReference(T value) | |
{ | |
this.value = value; | |
(rootName, path) = GetPathInHierarchy(value.transform); | |
} | |
public T Value => value != null ? value : (value = Load()); | |
T Load() | |
{ | |
var scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene(); | |
var rootObjects = scene.GetRootGameObjects(); | |
var root = rootObjects.FirstOrDefault(t => t.name == rootName); | |
if (root == null) | |
{ | |
return null; | |
} | |
var target = root.transform.Find(path); | |
if (target == null) | |
{ | |
return null; | |
} | |
var comp = target.GetComponent<T>(); | |
return comp; | |
} | |
static (string root, string relative) GetPathInHierarchy(Transform t) | |
{ | |
var root = t.root; | |
var rootPath = root.name; | |
if (root == t) | |
{ | |
return (rootPath, string.Empty); | |
} | |
var builder = new StringBuilder(t.name); | |
var parent = t.parent; | |
while (parent != root) | |
{ | |
builder.Insert(0, parent.name + "/"); | |
parent = parent.parent; | |
} | |
return (rootPath, builder.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment