Created
November 18, 2014 16:17
-
-
Save asus4/4190d8b05816818a9bc9 to your computer and use it in GitHub Desktop.
Follow abstruct poroperty.
This file contains 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; | |
using System; | |
using System.Reflection; | |
public class PropertyFollow : MonoBehaviour { | |
[Serializable] | |
public class TargetProp { | |
public string compName; | |
public string propName; | |
} | |
public GameObject target; | |
public TargetProp prop; | |
PropertyInfo propInfo; | |
Component targetComp; | |
Component myComp; | |
void Start () { | |
// Check component | |
targetComp = target.GetComponent (prop.compName); | |
myComp = this.GetComponent (prop.compName); | |
if(targetComp == null || myComp == null) { | |
Debug.LogWarning("No target component"); | |
this.enabled = false; | |
return; | |
} | |
// Cache property | |
Type type = myComp.GetType (); | |
propInfo = type.GetProperty (prop.propName); | |
if(propInfo == null) { | |
Debug.LogWarning("No target property"); | |
this.enabled = false; | |
return; | |
} | |
} | |
void Update () { | |
propInfo.SetValue (myComp, propInfo.GetValue (targetComp, null), null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment