Last active
February 3, 2023 13:14
-
-
Save amatiasq/8eec84a212d9aa0bde988289bbb40f2a to your computer and use it in GitHub Desktop.
Using ExposedReferences in Unity
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.Collections.Generic; | |
using UnityEngine; | |
public class ExposedReferencesTable : MonoBehaviour, IExposedPropertyTable | |
{ | |
public List<PropertyName> properties = new(); | |
public List<Object> references = new(); | |
public T Get<T>(ExposedReference<T> reference) where T : Object | |
{ | |
var result = GetReferenceValue(reference.exposedName, out var idValid); | |
return idValid ? result as T : null; | |
} | |
public void Set<T>(ExposedReference<T> reference, T value) | |
where T : Object | |
{ | |
SetReferenceValue(reference.exposedName, value); | |
} | |
public Object GetReferenceValue(PropertyName id, out bool idValid) | |
{ | |
var index = properties.IndexOf(id); | |
if (index == -1) { | |
idValid = false; | |
return null; | |
} | |
idValid = true; | |
return references[index]; | |
} | |
public void SetReferenceValue(PropertyName id, Object value) | |
{ | |
var index = properties.IndexOf(id); | |
if (index == -1) { | |
properties.Add(id); | |
references.Add(value); | |
} else { | |
references[index] = value; | |
} | |
} | |
public void ClearReferenceValue(PropertyName id) | |
{ | |
var index = properties.IndexOf(id); | |
if (index == -1) | |
return; | |
properties.RemoveAt(index); | |
references.RemoveAt(index); | |
} | |
} |
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 UnityEngine; | |
public static class ComponentExtensions | |
{ | |
public static ExposedReferencesTable GetExposedReferencesResolver(this Component transform) | |
{ | |
var found = gameObject.GetComponent<ExposedReferencesTable>(); | |
return found == null ? gameObject.AddComponent<ExposedReferencesTable>() : found; | |
} | |
} |
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 UnityEngine; | |
public class MyPlayable : PlayableBehaviour | |
{ | |
private ExposedReferencesTable referenceResolver; | |
[SerializeField] | |
private ExposedReference<SomeType> _someType; | |
public SomeType someType { | |
get => referenceResolver.Get(_someType); | |
set => referenceResolver.Set(_someType, value); | |
} | |
public override void ProcessFrame(Playable playable, FrameData info, object playerData) | |
{ | |
if (playerData is not Component component) | |
return; | |
if (referenceResolver == null) | |
referenceResolver = component.GetExposedReferencesResolver(); | |
// magic | |
Debug.Log(someType); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment