using System.Collections.Generic; using System.Linq; using System.Reflection; using UnityEngine; namespace RelationsInspector.Backend.AutoBackend { public class RIAutoBackend : MinimalBackend where T : class { IEnumerable relatedFields; IEnumerable relatingFields; public override void Awake( GetAPI getAPI ) { relatingFields = ReflectionUtil.GetAttributeFields(); relatedFields = ReflectionUtil.GetAttributeFields(); if ( !relatingFields.Any() && !relatedFields.Any() ) Debug.LogError( "Type has auto backend attribute, but no fields marked as related or relating: " + typeof( T ) ); base.Awake( getAPI ); } public override IEnumerable> GetRelations( T entity ) { var outRelations = relatedFields .SelectMany( fInfo => ReflectionUtil.GetValues( fInfo, entity ) ) .Select( other => new Relation( entity, other, string.Empty ) ); var inRelations = relatingFields .SelectMany( fInfo => ReflectionUtil.GetValues( fInfo, entity ) ) .Select( other => new Relation( other, entity, string.Empty ) ); return outRelations.Concat( inRelations ); } } }