public virtual void OnBeforeSerialize() { #if UNITY_EDITOR foreach (FieldInfo field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) { object[] attributes = field.GetCustomAttributes(typeof(LoadOnDemand), true); if (attributes.Length == 1) { LoadOnDemand attribute = (LoadOnDemand)attributes[0]; FieldInfo linkedField = GetType().GetField(attribute.FieldName); object value = linkedField.GetValue(this); if (value != null) { if (typeof(ScriptableObjectExtended).IsAssignableFrom(value.GetType())) { ScriptableObjectExtended linkedObject = (ScriptableObjectExtended)value; if (linkedObject != null) { field.SetValue(this, linkedObject.Guid); } } else { field.SetValue(this, AssetToResourcePath(UnityEditor.AssetDatabase.GetAssetPath((Object)value))); } } } } #endif }
public void Unload(string fieldName = null) { foreach (FieldInfo field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) { object[] attributes = field.GetCustomAttributes(typeof(LoadOnDemand), true); if (attributes.Length == 1) { LoadOnDemand attribute = (LoadOnDemand)attributes[0]; string attributeFieldName = attribute.FieldName; if (fieldName == null || attributeFieldName == fieldName) { FieldInfo linkedField = GetType().GetField(attributeFieldName); linkedField.SetValue(this, null); } } } }
public void Load(string fieldName = null, bool async = false) { foreach (FieldInfo field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) { object[] attributes = field.GetCustomAttributes(typeof(LoadOnDemand), true); if (attributes.Length == 1) { LoadOnDemand attribute = (LoadOnDemand)attributes[0]; string attributeFieldName = attribute.FieldName; if (fieldName == null || attributeFieldName == fieldName) { FieldInfo linkedField = GetType().GetField(attributeFieldName); string fieldValue = (string)field.GetValue(this); if (fieldValue != null) { if (fieldValue.Length == 32 && Regex.IsMatch(fieldValue, c_GUIDRegex)) { // it's a guid linkedField.SetValue(this, ScriptableObjectDatabase.GetAsset(fieldValue, ScriptableObjectDatabase.c_ObjectResourcePath)); } else { // it's a path if (async) { Resources.LoadAsync(fieldValue); } else { // Sprite special case since by default Unity loads images as textures if (typeof(Sprite) == linkedField.FieldType) { linkedField.SetValue(this, Resources.Load <Sprite>(fieldValue)); } else { linkedField.SetValue(this, Resources.Load(fieldValue)); } } } } } } } }