void RunMethod ( GameObject gameObject, Component component, string methodName, string[] arguments, bool printResult )
{
Type componentType = component.GetType();
MethodInfo[] mInfos = componentType.GetMethods();
object[] pMatchedArguments = new object[0];
int mostMatchingParams = -1;
int bestMatchNumParams = 0;
int bestMatch = -1;
bool willReturn = false;
object result = null;
for ( int i = 0; i < mInfos.Length; i++ )
{
MethodInfo mInfo = mInfos[i];
int numMatchingParams = 0;
if ( mInfo != null )
{
if ( mInfo.Name != methodName )
{
continue;
}
bool paramSuccess = true;
// Find arg types
ParameterInfo[] pInfo = mInfo.GetParameters();
object[] pArguments = new object[pInfo.Length];
for ( int pI = 0; pI < pInfo.Length; pI++ )
{
if ( arguments.Length > pI )
{
if ( pInfo[pI].ParameterType == typeof( int ) )
{
int intResult = 0;
paramSuccess = int.TryParse( arguments[pI], out intResult );
if ( paramSuccess )
{
pArguments[pI] = intResult;
numMatchingParams++;
}
}
else if ( pInfo[pI].ParameterType == typeof( float ) )
{
float floatResult = 0;
paramSuccess = float.TryParse( arguments[pI], out floatResult );
if ( paramSuccess )
{
pArguments[pI] = floatResult;
numMatchingParams++;
}
}
else if ( pInfo[pI].ParameterType == typeof( double ) )
{
double doubleResult = 0;
paramSuccess = double.TryParse( arguments[pI], out doubleResult );
if ( paramSuccess )
{
pArguments[pI] = doubleResult;
numMatchingParams++;
}
}
else if ( pInfo[pI].ParameterType == typeof( bool ) )
{
if ( arguments[pI].ToUpper() == "FALSE" )
{
pArguments[pI] = false;
numMatchingParams++;
}
else if ( arguments[pI].ToUpper() == "TRUE" )
{
pArguments[pI] = true;
numMatchingParams++;
}
else
{
int intResult = 0;
paramSuccess = int.TryParse( arguments[pI], out intResult );
if ( paramSuccess )
{
pArguments[pI] = ( intResult == 0 ) ? false : true;
numMatchingParams++;
}
}
}
else if ( pInfo[pI].ParameterType == typeof( string ) )
{
pArguments[pI] = arguments[pI];
numMatchingParams++;
}
else if ( pInfo[pI].ParameterType == typeof( Vector2 ) )
{
string[] splitArg = arguments[pI].Split( ',' );
if ( splitArg.Length == 2 )
{
float xResult = 0f;
bool xSuccess = false;
xSuccess = float.TryParse( splitArg[0], out xResult );
float yResult = 0f;
bool ySuccess = false;
ySuccess = float.TryParse( splitArg[2], out yResult );
if ( xSuccess && ySuccess )
{
pArguments[pI] = new Vector2( xResult, yResult );
numMatchingParams++;
}
else
{
paramSuccess = false;
}
}
}
else if ( pInfo[pI].ParameterType == typeof( Vector3 ) )
{
string[] splitArg = arguments[pI].Split( ',' );
if ( splitArg.Length == 3 )
{
float xResult = 0f;
bool xSuccess = false;
xSuccess = float.TryParse( splitArg[0], out xResult );
float yResult = 0f;
bool ySuccess = false;
ySuccess = float.TryParse( splitArg[1], out yResult );
float zResult = 0f;
bool zSuccess = false;
zSuccess = float.TryParse( splitArg[2], out zResult );
if ( xSuccess && ySuccess && zSuccess )
{
pArguments[pI] = new Vector3( xResult, yResult, zResult );
numMatchingParams++;
}
else
{
paramSuccess = false;
}
}
}
else if ( pInfo[pI].ParameterType == typeof( Quaternion ) )
{
string[] splitArg = arguments[pI].Split( ',' );
if ( splitArg.Length == 4 )
{
float xResult = 0f;
bool xSuccess = false;
xSuccess = float.TryParse( splitArg[0], out xResult );
float yResult = 0f;
bool ySuccess = false;
ySuccess = float.TryParse( splitArg[1], out yResult );
float zResult = 0f;
bool zSuccess = false;
zSuccess = float.TryParse( splitArg[2], out zResult );
float wResult = 0f;
bool wSuccess = false;
wSuccess = float.TryParse( splitArg[2], out wResult );
if ( xSuccess && ySuccess && zSuccess && wSuccess )
{
pArguments[pI] = new Quaternion( xResult, yResult, zResult, wResult );
numMatchingParams++;
}
else
{
paramSuccess = false;
}
}
}
else if ( pInfo[pI].ParameterType == typeof( Color ) )
{
string[] splitArg = arguments[pI].Split( ',' );
if ( splitArg.Length == 4 )
{
float xResult = 0f;
bool xSuccess = false;
xSuccess = float.TryParse( splitArg[0], out xResult );
float yResult = 0f;
bool ySuccess = false;
ySuccess = float.TryParse( splitArg[1], out yResult );
float zResult = 0f;
bool zSuccess = false;
zSuccess = float.TryParse( splitArg[2], out zResult );
float wResult = 0f;
bool wSuccess = false;
wSuccess = float.TryParse( splitArg[2], out wResult );
if ( xSuccess && ySuccess && zSuccess && wSuccess )
{
pArguments[pI] = new Color( xResult, yResult, zResult, wResult );
numMatchingParams++;
}
else
{
paramSuccess = false;
}
}
}
}
else
{
if ( pInfo[pI].DefaultValue != null )
{
paramSuccess = false;
}
else
{
pArguments[pI] = pInfo[pI].DefaultValue;
}
}
if ( !paramSuccess ) break;
}
if ( numMatchingParams > mostMatchingParams || ( ( numMatchingParams == mostMatchingParams ) && ( bestMatchNumParams > pInfo.Length ) ) )
{
bestMatchNumParams = pInfo.Length;
mostMatchingParams = numMatchingParams;
bestMatch = i;
pMatchedArguments = pArguments;
if ( mInfo.ReturnType != typeof( void ) )
{
willReturn = true;
}
else
{
willReturn = false;
}
}
}
}
if ( bestMatch >= 0 )
{
if ( willReturn )
{
result = mInfos[bestMatch].Invoke( component, pMatchedArguments );
if ( printResult ) PrintToConsole( result.ToString() );
}
else
{
mInfos[bestMatch].Invoke( component, pMatchedArguments );
}
}
}