Example #1
0
    protected static object EditorField(object value, string label = "", bool labelField = false, bool isVariableObject = false, params GUILayoutOption[] options)//, GUIContent glabel)
    {
        // Possibly make nice with http://docs.unity3d.com/ScriptReference/GUILayout.FlexibleSpace.html
        string          type;
        object          returnvalue = value;
        FixedWidthLabel fixedWidth  = null;

        if (ValidTypeStrings == null)
        {
            PopulateTypeArrays();
        }

        // Early case if empty
        if (value == null && !isVariableObject)
        {
            EditorGUILayout.LabelField(label, "null", options);
            return(value);
        }

        #region Handle variable object

        if (isVariableObject)
        {
            // Get index from type
            int index = value == null ? 0 : ValidTypes.FindIndex(t => t == value.GetType());

            fixedWidth = new FixedWidthLabel(label);
            float popUpWidht = GUI.skin.label.CalcSize(new GUIContent(ValidTypeStrings[index])).x + 10;
            index = EditorGUILayout.Popup("", index, ValidTypeStrings, GUILayout.Width(popUpWidht));

            label = "";

            type = ValidTypeStrings[index];
            Type selectedType = ValidTypes[index];

            #region set Default type
            if (value == null || value.GetType() != selectedType)
            {
                if (selectedType.IsValueType)
                {
                    value = Activator.CreateInstance(selectedType);
                }
                else if (selectedType == typeof(string))
                {
                    value = "";
                }
                else
                {
                    EditorGUILayout.LabelField(label, "null", options);
                    return(value);
                }
            }
            #endregion
        }
        else
        {
            type = value.GetType().ToString();
        }

        #endregion

        if (labelField)
        {
            EditorGUILayout.LabelField(label, value.ToString(), options);
            return(value);
        }

        // AutoHandle by type
        switch (type)
        {
        case "UnityEngine.Vector4":
            returnvalue = EditorGUILayout.Vector4Field(label, (Vector4)value, options);
            break;

        case "UnityEngine.Vector3":
            returnvalue = EditorGUILayout.Vector3Field(label, (Vector3)value, options);
            break;

        case "UnityEngine.Vector2":
            returnvalue = EditorGUILayout.Vector2Field(label, (Vector2)value, options);
            break;

        case "System.Single":
            returnvalue = EditorGUILayout.FloatField(label, (float)value, options);
            break;

        case "System.Int32":
            returnvalue = EditorGUILayout.IntField(label, (int)value, options);
            break;

        case "System.Boolean":
            returnvalue = EditorGUILayout.Toggle(label, (bool)value, options);
            break;

        case "System.String":
            returnvalue = EditorGUILayout.TextField(label, (string)value, options);
            break;

        //case "AI_AgentParameter":
        //    returnvalue = AI_AgentBlackBoardAccessParameterDrawer.CustomDraw(label,(AI_AgentParameter)value);
        //    break;
        default:
            EditorGUILayout.LabelField(label, value.ToString() + " - undefined", options);
            Debug.Log("EditorPlus.EditorField does not contain definition for " + value.GetType().ToString());
            break;
        }

        if (fixedWidth != null)
        {
            fixedWidth.Dispose();
        }
        //EditorGUILayout.EndHorizontal();

        return(returnvalue);
    }
    protected static object EditorField(object value, string label = "", bool labelField = false, bool isVariableObject = false, params GUILayoutOption[] options)//, GUIContent glabel)
    {
        // Possibly make nice with http://docs.unity3d.com/ScriptReference/GUILayout.FlexibleSpace.html
        string type;
        object returnvalue = value;
        FixedWidthLabel fixedWidth = null;

        if (ValidTypeStrings==null)
            PopulateTypeArrays();

        // Early case if empty
        if (value == null && !isVariableObject)
        {
            EditorGUILayout.LabelField(label, "null", options);
            return value;
        }

        #region Handle variable object

        if (isVariableObject)
        {
            // Get index from type
            int index = value == null ? 0 : ValidTypes.FindIndex(t => t == value.GetType());

            fixedWidth = new FixedWidthLabel(label);
            float popUpWidht = GUI.skin.label.CalcSize(new GUIContent(ValidTypeStrings[index])).x + 10;
            index = EditorGUILayout.Popup("", index, ValidTypeStrings, GUILayout.Width(popUpWidht));
            
            label = "";

            type = ValidTypeStrings[index];
            Type selectedType = ValidTypes[index];

            #region set Default type
            if (value == null || value.GetType() != selectedType)
            {
                if (selectedType.IsValueType)
                {
                    value = Activator.CreateInstance(selectedType);
                }
                else if(selectedType == typeof(string))
                {
                    value = "";
                }
                else
                {
                    EditorGUILayout.LabelField(label, "null", options);
                    return value;
                }
            }
            #endregion
        }
        else
            type = value.GetType().ToString();

        #endregion

        if (labelField)
        {
            EditorGUILayout.LabelField(label, value.ToString(), options);
            return value;
        }

        // AutoHandle by type
        switch (type)
        {
            case "UnityEngine.Vector4":
                returnvalue = EditorGUILayout.Vector4Field(label, (Vector4)value, options);
                break;
            case "UnityEngine.Vector3":
                returnvalue = EditorGUILayout.Vector3Field(label, (Vector3)value, options);
                break;
            case "UnityEngine.Vector2":
                returnvalue = EditorGUILayout.Vector2Field(label, (Vector2)value, options);
                break;
            case "System.Single":
                returnvalue = EditorGUILayout.FloatField(label, (float)value, options);
                break;
            case "System.Int32":
                returnvalue = EditorGUILayout.IntField(label, (int)value, options);
                break;
            case "System.Boolean":
                returnvalue = EditorGUILayout.Toggle(label, (bool)value, options);
                break;
            case "System.String":
                returnvalue = EditorGUILayout.TextField(label, (string)value, options);
                break;
            case "AI_AgentParameter":
                returnvalue = AI_AgentBlackBoardAccessParameterDrawer.CustomDraw(label,(AI_AgentParameter)value);
                break;
            default:
                EditorGUILayout.LabelField(label, value.ToString() + " - undefined", options);
                Debug.Log("EditorPlus.EditorField does not contain definition for " + value.GetType().ToString());
                break;

        }

        if (fixedWidth != null)
        {
            fixedWidth.Dispose();
        }
            //EditorGUILayout.EndHorizontal();

        return returnvalue;
    }