using UnityEngine;
///
/// Prepares variables to be used by PopupDrawer.
///
public class PopupAttribute : PropertyAttribute
{
public string[] list;
public object variableType;
#region PopupAttribute()
///
/// Makes necessary operations to prepare the variables for later use by PopupDrawer.
///
/// Parameters array to be analized and assigned.
public PopupAttribute(params object[] list)
{
if (IsVariablesTypeConsistent(list) && AssignVariableType(list[0]))
{
this.list = new string[list.Length];
for (int i = 0; i < list.Length; i++)
{
this.list[i] = list[i].ToString();
}
}
else
{
return;
}
}
#endregion
#region Helper Methods.
#region AssignVariableType()
///
/// Checks if variable type is valid, and assignes the variable type to the proper variable.
///
/// Object to get type from.
/// Returns true if variable type is valid, and false if it isn't.
private bool AssignVariableType(object variable)
{
if (variable.GetType() == typeof(int))
{
variableType = typeof(int[]);
return true;
}
else if (variable.GetType() == typeof(float))
{
variableType = typeof(float[]);
return true;
}
else if (variable.GetType() == typeof(double))
{
Debug.LogWarning("Popup Drawer doesn't properly support double type, for float variables please use 'f' at the end of each value.");
variableType = typeof(float[]);
return true;
}
else if (variable.GetType() == typeof(string))
{
variableType = typeof(string[]);
return true;
}
else
{
Debug.LogError("Popup Property Drawer doesn't support " + variable.GetType() + " this type of variable");
return false;
}
}
#endregion
#region IsVariablesTypeConsistent()
///
/// Checks to see if there is only one variable type in the given value.
///
/// Array of variables to be checked.
/// True if there is only one type, false if there is 2 or more.
private bool IsVariablesTypeConsistent(object[] list)
{
for (int i = 0; i < list.Length; i++)
{
if (i == 0)
{
variableType = list[i].GetType();
}
else if (variableType != list[i].GetType())
{
Debug.LogError("Popup Property Drawer can only contain one type per variable");
return false;
}
}
return true;
}
#endregion
#endregion
}