Exemplo n.º 1
0
        public bool IsAvailable(IUserDataHolder cache)
        {
            if (!myDataProvider.Project.IsUnityProject())
            {
                return(false);
            }

            var unityApi         = myDataProvider.Solution.GetComponent <UnityApi>();
            var fieldDeclaration = myDataProvider.GetSelectedElement <IFieldDeclaration>();

            if (fieldDeclaration == null || !unityApi.IsSerialisedField(fieldDeclaration.DeclaredElement))
            {
                return(false);
            }

            var existingAttribute = AttributeUtil.GetAttribute(fieldDeclaration, AttributeTypeName);

            if (existingAttribute != null && !IsRemoveActionAvailable)
            {
                return(false);
            }

            // Only for UnityObject types, not [Serialized] types
            var classDeclaration = fieldDeclaration.GetContainingTypeDeclaration();
            var classElement     = classDeclaration?.DeclaredElement;

            return(UnityApi.IsDescendantOfMonoBehaviour(classElement) ||
                   UnityApi.IsDescendantOfScriptableObject(classElement));
        }
        public string GetPresentation(ISolution solution, IDeclaredElement declaredElement, bool prefabImport)
        {
            solution.GetComponent <IShellLocks>().AssertReadAccessAllowed();
            var hierarchyContainer = solution.GetComponent <AssetDocumentHierarchyElementContainer>();

            if (UnityApi.IsDescendantOfScriptableObject((declaredElement as IField)?.Type.GetTypeElement()))
            {
                if (Reference is LocalReference localReference && localReference.LocalDocumentAnchor == 0)
                {
                    return("None");
                }

                var sourceFile = hierarchyContainer.GetSourceFile(Reference, out _);
                if (sourceFile == null)
                {
                    return("...");
                }

                var relativePath = sourceFile.DisplayName.Replace('\\', '/').RemoveStart("Assets/");

                return(relativePath);
            }

            if (Reference.LocalDocumentAnchor == 0)
            {
                return("None");
            }

            var processor = solution.GetComponent <AssetHierarchyProcessor>();
            var consumer  = new UnityScenePathGameObjectConsumer(true);
            var element   = hierarchyContainer.GetHierarchyElement(Reference, prefabImport);

            if (element == null)
            {
                return("...");
            }
            string result = "";

            if (!element.IsStripped)
            {
                processor.ProcessSceneHierarchyFromComponentToRoot(element, consumer, prefabImport);
                if (consumer.NameParts.Count == 0)
                {
                    return("...");
                }
                result += string.Join("/", consumer.NameParts);
            }

            if (element is IComponentHierarchy componentHierarchy)
            {
                result += $" ({AssetUtils.GetComponentName(solution.GetComponent<MetaFileGuidCache>(), componentHierarchy)})";
            }

            return(result);
        }
        public bool IsAvailable(IUserDataHolder cache)
        {
            var identifier = myDataProvider.GetSelectedElement <ICSharpIdentifier>();

            var classLikeDeclaration = ClassLikeDeclarationNavigator.GetByNameIdentifier(identifier);

            if (classLikeDeclaration == null)
            {
                return(false);
            }

            var existingAttribute = classLikeDeclaration.GetAttribute(KnownTypes.CreateAssetMenu);

            return(existingAttribute == null && UnityApi.IsDescendantOfScriptableObject(classLikeDeclaration.DeclaredElement));
        }
        public override RichText GetDisplayText()
        {
            var declaredElement = DeclaredElementPointer.FindDeclaredElement();

            if (declaredElement == null)
            {
                return(base.GetDisplayText());
            }

            if (UnityApi.IsDescendantOfScriptableObject(declaredElement as IClass))
            {
                return(SourceFile.GetLocation().Name);
            }

            return(base.GetDisplayText());
        }
Exemplo n.º 5
0
        public override IDeclaredElement Analyze(IDeclaration element, IHighlightingConsumer consumer, DaemonProcessKind kind)
        {
            if (!(element is IFieldDeclaration field))
            {
                return(null);
            }

            var declaredElement = field.DeclaredElement;

            if (declaredElement == null)
            {
                return(null);
            }

            bool isSerializedField = myUnityApi.IsSerialisedField(declaredElement);

            if (isSerializedField)
            {
                const string displayText = "Serializable";
                const string baseTooltip = "This field is initialized from Inspector";
                if (myUnityApi.IsDescendantOfMonoBehaviour(declaredElement.GetContainingType()))
                {
                    AddMonoBehaviourHighlighting(consumer, field, displayText, baseTooltip, kind);
                    return(declaredElement);
                }
                else if (myUnityApi.IsDescendantOfScriptableObject(declaredElement.GetContainingType()))
                {
                    AddScriptableObjectHighlighting(consumer, field, displayText, baseTooltip, kind);
                    return(declaredElement);
                }
                else if (myUnityApi.IsInjectedField(declaredElement))
                {
                    AddECSHighlighting(consumer, field, displayText, "This field is injected by Unity", kind);
                    return(declaredElement);
                }
                else
                {
                    AddSerializableHighlighting(consumer, field, displayText, "This field is serialized by Unity", kind);
                }

                return(null);
            }

            return(null);
        }
Exemplo n.º 6
0
        private UnityPresentationType GetUnityPresentationType(IType type)
        {
            if (UnityApi.IsDescendantOfUnityEvent(type.GetTypeElement()))
            {
                return(UnityPresentationType.UnityEvent);
            }

            if (UnityApi.IsDescendantOfScriptableObject(type.GetTypeElement()))
            {
                return(UnityPresentationType.ScriptableObject);
            }
            if (type.IsBool())
            {
                return(UnityPresentationType.Bool);
            }
            if (type.IsEnumType())
            {
                return(UnityPresentationType.Enum);
            }
            if (type.IsString())
            {
                return(UnityPresentationType.String);
            }

            if (type.IsSimplePredefined())
            {
                return(UnityPresentationType.OtherSimple);
            }

            if (type.IsValueType())
            {
                return(UnityPresentationType.ValueType);
            }

            if (IsSerializedViaFileId(type))
            {
                return(UnityPresentationType.FileId);
            }

            return(UnityPresentationType.Other);
        }
Exemplo n.º 7
0
        public override bool AddDeclarationHighlighting(IDeclaration node, IHighlightingConsumer consumer, DaemonProcessKind kind)
        {
            if (!(node is IClassLikeDeclaration element))
            {
                return(false);
            }

            var typeElement = element.DeclaredElement;

            if (typeElement != null)
            {
                if (UnityApi.IsDescendantOfMonoBehaviour(typeElement))
                {
                    AddMonoBehaviourHiglighting(consumer, element, "Script", "Unity script", kind);
                }
                else if (UnityApi.IsDescendantOf(KnownTypes.Editor, typeElement) ||
                         UnityApi.IsDescendantOf(KnownTypes.EditorWindow, typeElement))
                {
                    AddEditorHiglighting(consumer, element, "Editor", "Custom Unity Editor", kind);
                }
                else if (UnityApi.IsDescendantOfScriptableObject(typeElement))
                {
                    AddMonoBehaviourHiglighting(consumer, element, "Scriptable object", "Scriptable Object", kind);
                }
                else if (myUnityApi.IsUnityType(typeElement))
                {
                    AddUnityTypeHighlighting(consumer, element, "Unity type", "Custom Unity type", kind);
                }
                else if (myUnityApi.IsUnityECSType(typeElement))
                {
                    AddUnityECSHighlighting(consumer, element, "Unity ECS", "Unity entity component system object",
                                            kind);
                }

                return(true);
            }

            return(false);
        }
Exemplo n.º 8
0
        public IDeclaredElement Analyze(IDeclaration element, IHighlightingConsumer consumer, DaemonProcessKind kind)
        {
            if (!(element is IFieldDeclaration field))
            {
                return(null);
            }

            var declaredElement = field.DeclaredElement;

            if (declaredElement == null)
            {
                return(null);
            }

            bool isSerializedField = myUnityApi.IsSerialisedField(declaredElement);

            if (isSerializedField && (
                    myUnityApi.IsDescendantOfMonoBehaviour(declaredElement.GetContainingType()) ||
                    myUnityApi.IsDescendantOfScriptableObject(declaredElement.GetContainingType())
                    ) ||
                myUnityApi.IsInjectedField(declaredElement))
            {
                myImplicitUsageHighlightingContributor.AddUnityImplicitFieldUsage(consumer, field,
                                                                                  "This field is initialised by Unity", "Set by Unity", kind);

                return(declaredElement);
            }

            if (isSerializedField && declaredElement.GetAttributeInstances(false)
                .All(t => !t.GetClrName().Equals(KnownTypes.SerializeField)))
            {
                myImplicitUsageHighlightingContributor.AddUnityImplicitFieldUsage(consumer, field,
                                                                                  "This field is serialized by Unity", "Serializable", kind);
                return(declaredElement);
            }

            return(null);
        }
 private bool IsRelatedToScriptableObject() => UnityApi.IsDescendantOfScriptableObject(DeclaredElementPointer.FindDeclaredElement() as IClass);
Exemplo n.º 10
0
 private bool IsRelatedToScriptableObject() => UnityApi.IsDescendantOfScriptableObject((DeclaredElementPointer.FindDeclaredElement() as IField)?.Type.GetTypeElement());