67 lines
1.5 KiB
C#
67 lines
1.5 KiB
C#
using UnityEngine;
|
|||
|
|
using System.Collections;
|
||
|
|
using System.Linq;
|
||
|
|
using System;
|
||
|
|
|
||
|
|
public class Plot2DVariableFunction : Plot2DFunction
|
||
|
|
{
|
||
|
|
public GameObject target;
|
||
|
|
public string variable;
|
||
|
|
|
||
|
|
// Use this for initialization
|
||
|
|
void Start ()
|
||
|
|
{
|
||
|
|
ReadValue();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public override float f(float x)
|
||
|
|
{
|
||
|
|
return ReadValue();
|
||
|
|
}
|
||
|
|
|
||
|
|
float ReadValue()
|
||
|
|
{
|
||
|
|
if (target != null) {
|
||
|
|
var pathBlocks = variable.Split('.');
|
||
|
|
var c = target.GetComponent(pathBlocks[0]);
|
||
|
|
|
||
|
|
if (c == null)
|
||
|
|
{
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
//find target
|
||
|
|
int depth = 1;
|
||
|
|
object o = c;
|
||
|
|
System.Reflection.FieldInfo field = null ;
|
||
|
|
|
||
|
|
while (depth < pathBlocks.Length) {
|
||
|
|
var t = o.GetType();
|
||
|
|
field = t.GetField(pathBlocks[depth], System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
|
||
|
|
if (field == null)
|
||
|
|
{
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
o = field.GetValue(o);
|
||
|
|
depth++;
|
||
|
|
}
|
||
|
|
try
|
||
|
|
{
|
||
|
|
float val = Convert.ToSingle(o);
|
||
|
|
return val;
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
Debug.LogFormat("Failed to convert property {0} to float type!", field.Name);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update () {
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|