コード例 #1
0
        private bool CanAutoPopulate(SerializedProperty property, AutoPopulateAttribute autoPopulate)
        {
            if (autoPopulate == null)
            {
                return(false);
            }
            if (property.isArray)
            {
                return(false); // Disable array support for now,
                // Unity doesn't call the drawer for the array, it calls it for the elements in the array :(
            }
            if (property.propertyPath.EndsWith("]"))
            {
                return(false); // as above, we use the ] char as a sign that Unity is trying to edit an array element
            }

            if (property.propertyType == SerializedPropertyType.ObjectReference)
            {
                return(true);
            }

            if (AutoPopulateUtils.IsInterfaceReference(property, fieldInfo.FieldType))
            {
                return(true);
            }

            return(false);
        }
コード例 #2
0
        private void ForceRepopulate(object data)
        {
            SerializedProperty property = data as SerializedProperty;

            AutoPopulateAttribute autoPopulate = attribute as AutoPopulateAttribute;

            if ((property != null) && (autoPopulate != null))
            {
                property.serializedObject.Update();
                AutoPopulateUtils.PopulateValue(fieldInfo.FieldType, property, autoPopulate);
                property.serializedObject.ApplyModifiedProperties();
            }
        }
コード例 #3
0
        private string GetTooltip(SerializedProperty property, AutoPopulateAttribute autoPopulate)
        {
            Type searchType;

            if (property.isArray)
            {
                searchType = AutoPopulateUtils.GetSearchType(fieldInfo.FieldType.GetElementType(), autoPopulate);
            }
            else
            {
                searchType = AutoPopulateUtils.GetSearchType(fieldInfo.FieldType, autoPopulate);
            }
            string relativeTo = "";

            if (autoPopulate.FindRelativeTo != null)
            {
                string componentName = autoPopulate.FindRelativeTo.Name;
                string componentArticle;
                if (IsVowel(componentName[0]))
                {
                    componentArticle = "an";
                }
                else
                {
                    componentArticle = "a";
                }

                relativeTo = string.Format(" on the first parent with {0} {1} component", componentArticle, autoPopulate.FindRelativeTo.Name);
            }
            switch (autoPopulate.Find)
            {
            case AutoPopulateAttribute.FindBehaviour.Self:
                return(string.Format("Auto-populated by calling gameObject.GetComponent({0}){1}", searchType.Name, relativeTo));

            case AutoPopulateAttribute.FindBehaviour.Children:
                return(string.Format("Auto-populated by calling gameObject.GetComponentInChildren({0}){1}", searchType.Name, relativeTo));

            case AutoPopulateAttribute.FindBehaviour.Parent:
                return(string.Format("Auto-populated by calling gameObject.GetComponentInParent({0}){1}", searchType.Name, relativeTo));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #4
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            AutoPopulateAttribute autoPopulate = attribute as AutoPopulateAttribute;

            if (CanAutoPopulate(property, autoPopulate))
            {
                bool wasEnabled = GUI.enabled;
                GUI.enabled = GetEditable(property, autoPopulate);

                bool populated = false;

                if (AutoPopulateUtils.NeedsPopulating(property, fieldInfo.FieldType))
                {
                    populated = AutoPopulateUtils.PopulateValue(fieldInfo.FieldType, property, autoPopulate);
                }
                else
                {
                    populated = true;
                }

                GUIContent tweakedLabel = new GUIContent(label.text, GetIcon(autoPopulate, populated), GetTooltip(property, autoPopulate));

                if (AutoPopulateUtils.IsInterfaceReference(property, fieldInfo.FieldType))
                {
                    InterfaceReferenceDrawer.PropertyField(position, property, fieldInfo, tweakedLabel);
                }
                else
                {
                    EditorGUI.PropertyField(position, property, tweakedLabel);
                }

                GUI.enabled = wasEnabled;
                HandlePopup(position, property, autoPopulate);
            }
            else
            {
                GUIContent tweakedLabel = new GUIContent(label.text, ErrorIcon, GetErrorTooltip());
                EditorGUI.PropertyField(position, property, tweakedLabel);
            }
        }