Exemplo n.º 1
0
 /// <summary>
 /// Assigns the property values from a specified member.
 /// </summary>
 protected virtual void SetValue(UnityMemberBase value)
 {
     if (value != null)
     {
         componentProperty.stringValue = value.component;
         nameProperty.stringValue      = value.name;
         UnityMemberDrawerHelper.SerializeParameterTypes(parameterTypesProperty, value.parameterTypes);
     }
     else
     {
         componentProperty.stringValue    = null;
         nameProperty.stringValue         = null;
         parameterTypesProperty.arraySize = 0;
     }
 }
Exemplo n.º 2
0
        /// <inheritdoc />
        protected override void RenderMemberControl(Rect position)
        {
            // Other Targets
            // Some Unity Objects, like Assets, are not supported by the drawer.
            // Just display an error message to let the user change their target.

            if (targetType == UnityObjectType.Other)
            {
                EditorGUI.HelpBox(position, "Unsupported Unity Object type.", MessageType.None);
                return;
            }

            // Display a list of all available reflected members in a popup.

            UnityMemberBase value = GetValue();

            DropdownOption <UnityMemberBase> selectedOption = null;

            if (value != null)
            {
                if (targetType == UnityObjectType.GameObject)
                {
                    string label;

                    if (value.component == null)
                    {
                        label = string.Format("GameObject.{0}", value.name);
                    }
                    else
                    {
                        label = string.Format("{0}.{1}", value.component, value.name);
                    }

                    // There seems to be no way of differentiating between null parameter types
                    // (fields, properties and implicitly typed methods) and zero parameter types
                    // because Unity's serialized property array cannot be assigned to null, only
                    // given an array size of zero.

                    // TODO: The solution would be to use a single comma-separated
                    // string instead of an array of strings, which we could differentiate manually.
                    if (value.parameterTypes != null && value.parameterTypes.Length > 0)
                    {
                        string parameterString = string.Join(", ", value.parameterTypes.Select(t => t.PrettyName()).ToArray());

                        label += string.Format(" ({0})", parameterString);
                    }

                    selectedOption = new DropdownOption <UnityMemberBase>(value, label);
                }
                else if (targetType == UnityObjectType.ScriptableObject)
                {
                    selectedOption = new DropdownOption <UnityMemberBase>(value, value.name);
                }
            }

            bool enabled = targetType != UnityObjectType.None;

            if (!enabled)
            {
                EditorGUI.BeginDisabledGroup(true);
            }

            EditorGUI.BeginChangeCheck();

            EditorGUI.showMixedValue = nameProperty.hasMultipleDifferentValues;

            List <DropdownOption <UnityMemberBase> > filteredMembers = new List <DropdownOption <UnityMemberBase> >();
            var allMembers = GetAllMemberOptions();

            if (filter.InspectorReturnTypes != null && filter.InspectorReturnTypes.Length > 0 && filter.InspectorReturnTypes.Where(s => (!string.IsNullOrEmpty(s))).Count() > 0)
            {
                for (int i = 0; i < allMembers.Count; i++)
                {
                    if (allMembers[i].label.ContainsOneOf(filter.InspectorReturnTypes))
                    {
                        filteredMembers.Add(allMembers[i]);
                    }
                }
            }
            else
            {
                filteredMembers = allMembers;
            }

            value = DropdownGUI <UnityMemberBase> .PopupSingle
                    (
                position,
                filteredMembers,
                selectedOption,
                new DropdownOption <UnityMemberBase>(null, string.Format("Nothing"))
                    );

            EditorGUI.showMixedValue = false;

            if (EditorGUI.EndChangeCheck())
            {
                SetValue(value);
            }

            if (!enabled)
            {
                EditorGUI.EndDisabledGroup();
            }
        }