public override void OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren)
        {
            if (property.propertyType != SerializedPropertyType.Enum)
            {
                EditorGUI.LabelField(position, label.text, "Use with Enum.");
                return;
            }

            if (s_Style == null)
            {
                s_Style = new GUIStyle(EditorStyles.popup);
                s_Style.normal.background = null;
            }

            m_DynamicNames.UpdateAndCheckInitial(names, property);

            if (m_Names == null)
            {
                var exclude = this.exclude.Split(',');
                m_Indexs = new List <int>();
                for (int i = 0; i < property.enumNames.Length; i++)
                {
                    if (!exclude.Contains(property.enumNames[i]))
                    {
                        m_Indexs.Add(i);
                    }
                }

                if (names == null)
                {
                    m_Names = property.enumDisplayNames.Except(exclude).ToArray();
                }
                else
                {
                    var array = m_DynamicNames.GetValue();
                    m_Names = array == null?names.Split(',') : array;
                }
            }

            int selected = m_Indexs.IndexOf(property.enumValueIndex);

            label = EditorGUI.BeginProperty(position, label, property);
            var value = EditorGUI.Popup(position, label.text, selected, m_Names);

            if (value < 0 || value >= m_Names.Length)
            {
                if (!property.hasMultipleDifferentValues)
                {
                    EditorGUI.LabelField(position, " ", placeHolder, s_Style);
                }
            }
            else if (value != selected)
            {
                property.enumValueIndex = m_Indexs[value];
            }
            EditorGUI.EndProperty();
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren)
        {
            switch (property.propertyType)
            {
            case SerializedPropertyType.Generic:
            case SerializedPropertyType.Enum:
            case SerializedPropertyType.LayerMask:
            case SerializedPropertyType.ArraySize:
            case SerializedPropertyType.Gradient:
#if UNITY_5_6_OR_NEWER
            case SerializedPropertyType.ExposedReference:
#endif
#if UNITY_2017_1_OR_NEWER
            case SerializedPropertyType.FixedBufferSize:
#endif
                EditorGUI.LabelField(position, label.text, "Not supported.");
                return;
            }

            if (s_Style == null)
            {
                s_Style = new GUIStyle(EditorStyles.popup);
                s_Style.normal.background = null;
            }

            if (m_TargetType == null)
            {
                m_TargetType = m_FieldInfo.FieldType;
                if (m_TargetType.IsArrayOrList())
                {
                    m_TargetType = m_TargetType.GetArrayOrListElementType();
                }
                m_TargetType = m_TargetType.MakeArrayType();
            }

            m_DynamicNames.UpdateAndCheckInitial(names, property);
            m_DynamicValues.UpdateAndCheckInitial(m_ValuesGetter, property, m_TargetType);

            if (m_Names == null)
            {
                var values = (Array)m_DynamicValues.GetValue();
                if (values == null)
                {
                    EditorGUI.LabelField(position, label.text, "Getter not correct.");
                    return;
                }

                m_Values = values.Cast <object>().ToArray();
                if (names == null)
                {
                    m_Names = m_Values.Select(v => v.ToString()).ToArray();
                }
                else
                {
                    var array = m_DynamicNames.GetValue();
                    m_Names = array == null?names.Split(',') : array;
                }

                var length = Mathf.Min(m_Names.Length, m_Values.Length);
                m_Names  = m_Names.Take(length).ToArray();
                m_Values = m_Values.Take(length).ToArray();
            }

            if (m_Values == null)
            {
                EditorGUI.LabelField(position, label.text, "Getter not correct.");
                return;
            }

            var propertyValue = property.GetValueAsObject();
            int selected      = Array.IndexOf(m_Values, propertyValue);

            label = EditorGUI.BeginProperty(position, label, property);
            var value = EditorGUI.Popup(position, label.text, selected, m_Names);
            if (value < 0 || value >= m_Names.Length)
            {
                if (!property.hasMultipleDifferentValues)
                {
                    EditorGUI.LabelField(position, " ", placeHolder, s_Style);
                }
            }
            else if (value != selected)
            {
                property.SetObjectToValue(m_Values[value]);
            }
            EditorGUI.EndProperty();
        }
Exemple #3
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren)
        {
#if !UNITY_5_6_OR_NEWER
            EditorGUI.LabelField(position, label.text, "Use with Unity 5.6 or above.");
            return;
#else
            if (property.propertyType != SerializedPropertyType.Enum)
            {
                EditorGUI.LabelField(position, label.text, "Use with Enum.");
                return;
            }

            m_DynamicNames.UpdateAndCheckInitial(names, property);

            if (m_Names == null)
            {
                var exclude = this.exclude.Split(',');
                var type    = m_FieldInfo.FieldType;
                type     = type.IsArrayOrList() ? type.GetArrayOrListElementType() : type;
                m_Values = Enum.GetNames(type).Except(exclude).Select(n => Convert.ToInt64(Enum.Parse(type, n))).ToList();

                if (names == null)
                {
                    m_Names = Enum.GetNames(type).Except(exclude).ToList();
                }
                else
                {
                    var array = m_DynamicNames.GetValue();
                    m_Names = array == null?names.Split(',').ToList() : array.ToList();
                }

                int index;
                while ((index = m_Values.IndexOf(0)) >= 0)
                {
                    if (names == null)
                    {
                        m_Names.RemoveAt(index);
                    }
                    m_Values.RemoveAt(index);
                }

                var length = Mathf.Min(m_Names.Count, m_Values.Count);
                m_Names  = m_Names.Take(length).ToList();
                m_Values = m_Values.Take(length).ToList();
            }

            long selected = property.longValue;
            long allmask  = 0;
            foreach (var mask in m_Values)
            {
                allmask |= mask;
            }
            var id = GUIUtility.GetControlID(s_HashCode, FocusType.Passive, position);

            label    = EditorGUI.BeginProperty(position, label, property);
            position = EditorGUI.PrefixLabel(position, label);

            s_Content.tooltip = string.Empty;
            if (selected == 0)
            {
                s_Content.text = placeHolder == string.Empty ? "None" : placeHolder;
            }
            else if (showAll && selected == allmask)
            {
                s_Content.text = "All";
            }
            else
            {
                s_StringBuilder.Length = 0;
                for (int i = 0; i < m_Names.Count; i++)
                {
                    if ((selected & m_Values[i]) == m_Values[i])
                    {
                        if ((!sortCombined && showCombined) || m_Values[i].IsPowerOfTwo())
                        {
                            s_StringBuilder.Append(m_Names[i]).Append(", ");
                        }
                    }
                }
                if (sortCombined)
                {
                    for (int i = 0; i < m_Names.Count; i++)
                    {
                        if ((selected & m_Values[i]) == m_Values[i])
                        {
                            if (showCombined && !m_Values[i].IsPowerOfTwo())
                            {
                                s_StringBuilder.Append(m_Names[i]).Append(", ");
                            }
                        }
                    }
                }
                if (s_StringBuilder.Length > 0)
                {
                    s_StringBuilder.Length -= 2;
                }
                s_Content.text = s_StringBuilder.ToString();
                if (EditorStyles.popup.CalcSize(s_Content).x > position.width)
                {
                    s_Content.tooltip = s_Content.text;
                }
            }

            if (property.hasMultipleDifferentValues)
            {
                selected = 0;
            }
            if (EditorGUI.DropdownButton(position, s_Content, FocusType.Keyboard, EditorStyles.popup))
            {
                MaskPopupList.Popup(position, selected, m_Names, m_Values, sortCombined, id);
            }
            if (MaskPopupList.IsSelectionChanged(id))
            {
                property.longValue = MaskPopupList.GetLastSelectedValue();
            }
            EditorGUI.EndProperty();
#endif
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label, bool includeChildren)
        {
            if (property.propertyType != SerializedPropertyType.String)
            {
                EditorGUI.LabelField(position, label.text, "Use with String.");
                return;
            }

            if (s_Style == null)
            {
                s_Style = new GUIStyle(EditorStyles.popup);
                s_Style.normal.background = null;
            }

            if (m_Exclude == null)
            {
                m_Exclude = exclude.Split(',');
            }

            IEnumerable <Animator> animators = Enumerable.Empty <Animator>();

            if (m_Animator != null)
            {
                m_DynamicAnimator.UpdateAndCheckInitial(m_Animator, property);
                animators = m_DynamicAnimator.GetValues().Cast <Animator>();
            }
            else if (property.serializedObject.targetObject is MonoBehaviour)
            {
                animators = property.serializedObject.targetObjects.Cast <MonoBehaviour>().Select(m => m.GetComponent <Animator>());
            }
            var animatorControllers = animators.Select(a => a == null ? null : a.runtimeAnimatorController).Distinct();

            if (!animatorControllers.Any() || animatorControllers.Any(a => a == null))
            {
                EditorGUI.LabelField(position, label.text, "Load animator failed.");
                return;
            }
            else if (animatorControllers.Where(c => c != null).Count() != 1)
            {
                EditorGUI.LabelField(position, label.text, "Multiple animator is not supported.");
                return;
            }

            var animator = animators.First(a => a != null && a.runtimeAnimatorController != null);

            s_Parameters.Clear();
            foreach (var parameter in animator.parameters.Distinct())
            {
                if (parameter.type == AnimatorControllerParameterType.Float && !floatType)
                {
                    continue;
                }
                if (parameter.type == AnimatorControllerParameterType.Int && !intType)
                {
                    continue;
                }
                if (parameter.type == AnimatorControllerParameterType.Bool && !boolType)
                {
                    continue;
                }
                if (parameter.type == AnimatorControllerParameterType.Trigger && !triggerType)
                {
                    continue;
                }
                if (m_Exclude.Contains(parameter.name))
                {
                    continue;
                }
                s_Parameters.Add(parameter.name);
            }
            var parameters = s_Parameters.ToArray();

            int selected = ArrayUtility.IndexOf(parameters, property.stringValue);

            label = EditorGUI.BeginProperty(position, label, property);
            var value = EditorGUI.Popup(position, label.text, selected, parameters);

            if (value < 0 || value >= parameters.Length)
            {
                if (!property.hasMultipleDifferentValues)
                {
                    EditorGUI.LabelField(position, " ", placeHolder, s_Style);
                }
            }
            else if (value != selected)
            {
                property.stringValue = parameters[value];
            }
            EditorGUI.EndProperty();
        }