Skip to content

Instantly share code, notes, and snippets.

@oskar-szulc
Last active October 1, 2015 19:44

Revisions

  1. oskar-szulc revised this gist Oct 1, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions PropertyBinding.cs
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,9 @@ void Awake()
    TargetProperty = tempTarget.FirstOrDefault();
    SourceProperty = tempSource.FirstOrDefault();
    #endif
    }

    void Start()
    {
    SourceProperty.ObserveEveryValueChanged(x => x.GetValue(Source, null)).Subscribe(UpdateTargetProperty);
    }
  2. oskar-szulc created this gist Oct 1, 2015.
    25 changes: 25 additions & 0 deletions PropertyBinding.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    [HideInInspector] public PropertyInfo SourceProperty;
    [HideInInspector] public PropertyInfo TargetProperty;
    [HideInInspector] public string SourcePropertyName;
    [HideInInspector] public string TargetPropertyName;

    public MonoBehaviour Source;
    public MonoBehaviour Target;

    void Awake()
    {
    #if !UNITY_EDITOR
    var tempTarget = from targetProp in Target.GetType().GetProperties()
    where targetProp.Name == TargetPropertyName
    select targetProp;

    var tempSource = from sourceProp in Source.GetType().GetProperties()
    where sourceProp.Name == SourcePropertyName
    select sourceProp;

    TargetProperty = tempTarget.FirstOrDefault();
    SourceProperty = tempSource.FirstOrDefault();
    #endif

    SourceProperty.ObserveEveryValueChanged(x => x.GetValue(Source, null)).Subscribe(UpdateTargetProperty);
    }