bool VisitNull <TContainer, TValue>(InspectorContext inspectorContext, IProperty <TContainer> property, ref TValue value,
                                            PropertyPath path)
        {
            var element = new NullElement <TValue>(inspectorContext.Root, property, path);

            inspectorContext.Parent.contentContainer.Add(element);
            return(true);
        }
Exemple #2
0
        public VisitStatus Visit <TContainer, TValue>(Property <TContainer, TValue> property, ref TContainer container,
                                                      ref TValue value)
        {
            if (!RuntimeTypeInfoCache <TValue> .CanBeNull || null != value)
            {
                return(VisitStatus.Unhandled);
            }

            if (typeof(UnityEngine.Object).IsAssignableFrom(typeof(TValue)))
            {
                return(VisitStatus.Unhandled);
            }

            if (!property.IsReadOnly && property.HasAttribute <CreateInstanceOnInspectionAttribute>() && !(property is ICollectionElementProperty))
            {
                var attribute = property.GetAttribute <CreateInstanceOnInspectionAttribute>();
                if (null == attribute.Type)
                {
                    if (TypeConstruction.CanBeConstructed <TValue>())
                    {
                        value = TypeConstruction.Construct <TValue>();
                        property.SetValue(ref container, value);
                        return(VisitStatus.Unhandled);
                    }

                    Debug.LogWarning(PropertyChecks.GetNotConstructableWarningMessage(typeof(TValue)));
                }
                else
                {
                    var isAssignable    = typeof(TValue).IsAssignableFrom(attribute.Type);
                    var isConstructable = TypeConstruction.GetAllConstructableTypes(typeof(TValue))
                                          .Contains(attribute.Type);
                    if (isAssignable && isConstructable)
                    {
                        value = TypeConstruction.Construct <TValue>(attribute.Type);
                        property.SetValue(ref container, value);
                        return(VisitStatus.Unhandled);
                    }

                    Debug.LogWarning(isAssignable
                        ? PropertyChecks.GetNotConstructableWarningMessage(attribute.Type)
                        : PropertyChecks.GetNotAssignableWarningMessage(attribute.Type, typeof(TValue)));
                }
            }

            Visitor.AddToPath(property);
            try
            {
                var path    = Visitor.GetCurrentPath();
                var element = new NullElement <TValue>(VisitorContext.Root, property, path);
                VisitorContext.Parent.contentContainer.Add(element);
            }
            finally
            {
                Visitor.RemoveFromPath(property);
            }
            return(VisitStatus.Stop);
        }