Binds a value stored in a so to a component by getting the value by name.
using System;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace _Scripts.Utility
{
public enum RefType
{
Instance,
Asset,
}
public enum FieldType
{
String,
Sprite
}
public class SOPropertyBinder : MonoBehaviour
{
public RefType refType;
[ShowIf("@refType == RefType.Instance")]public SOProvider soInstance;
[ShowIf("@refType == RefType.Asset")]public ScriptableObject scriptableObject;
public FieldType fieldType;
public string fieldName;
[ShowIf("@fieldType == FieldType.Sprite")]public Image image;
[ShowIf("@fieldType == FieldType.String")]public TextMeshProUGUI text;
private void Start()
{
Bind();
}
private void Bind()
{
var value = refType switch
{
RefType.Instance => soInstance.Get().GetValue(fieldName),
RefType.Asset => scriptableObject.GetValue(fieldName),
_ => null
};
switch (fieldType)
{
case FieldType.String:
text.text = (string) value;
break;
case FieldType.Sprite:
image.sprite = (Sprite) value;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
/**
This Method was extracted from the Unity Atoms Package.
Core/Editor/SerializedPropertyExtensions.cs
Unity Atoms is licensed under the MIT License
Copyright (c) 2018 Adam Ramberg
**/
public static object GetValue(this object source, string name)
{
if (source == null)
return null;
var type = source.GetType();
var f = type.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (f == null)
{
var p = type.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (p == null)
return null;
return p.GetValue(source, null);
}
return f.GetValue(source);
}
}
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace _Scripts.Utility
{
public enum RefType
{
Instance,
Asset,
}
public enum FieldType
{
String,
Sprite
}
public class SOPropertyBinder : MonoBehaviour
{
public RefType refType;
public SOProvider soInstance;
public ScriptableObject scriptableObject;
public FieldType fieldType;
public string fieldName;
public Image image;
public TextMeshProUGUI text;
private void Start()
{
Bind();
}
private void Bind()
{
var value = refType switch
{
RefType.Instance => soInstance.Get().GetValue(fieldName),
RefType.Asset => scriptableObject.GetValue(fieldName),
_ => null
};
switch (fieldType)
{
case FieldType.String:
text.text = (string) value;
break;
case FieldType.Sprite:
image.sprite = (Sprite) value;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
/**
This Method was extracted from the Unity Atoms Package.
Core/Editor/SerializedPropertyExtensions.cs
Unity Atoms is licensed under the MIT License
Copyright (c) 2018 Adam Ramberg
**/
public static object GetValue(this object source, string name)
{
if (source == null)
return null;
var type = source.GetType();
var f = type.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (f == null)
{
var p = type.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (p == null)
return null;
return p.GetValue(source, null);
}
return f.GetValue(source);
}
}