Example #1
0
        public override VisualElement CreateInspectorGUI()
        {
            // Update the serializedObject in case it has been changed outside the Inspector.
            serializedObject.Update();

            const string mainMarkup       = "GltfImporter";
            const string reportItemMarkup = "ReportItem";
            const string dependencyMarkup = "Dependency";
            var          root             = new VisualElement();

            var visualTree = Resources.Load(mainMarkup) as VisualTreeAsset;

            visualTree.CloneTree(root);

            var numDeps = m_AssetDependencies.arraySize;

            var maliciousTextureImporters = new List <TextureImporter>();

            var reportItemCount = m_ReportItems.arraySize;

#if !UNITY_ANIMATION
            var animRoot = root.Query <VisualElement>(name: "Animation").First();
            animRoot.SetEnabled(false);
#endif

            var reportRoot = root.Query <VisualElement>(name: "Report").First();

            if (reportItemCount > 0)
            {
                // var reportList = new List<ReportItem>
                var reportItemTree = Resources.Load(reportItemMarkup) as VisualTreeAsset;
                var reportList     = reportRoot.Query <ListView>().First();
                // reportList.bindingPath = nameof(m_ReportItems);
                reportList.makeItem = () => reportItemTree.CloneTree();
                reportList.bindItem = (element, i) => {
                    if (i >= reportItemCount || i >= m_ReportItems.arraySize)
                    {
                        element.style.display = DisplayStyle.None;
                        return;
                    }
                    var msg = element.Q <Label>("Message");

                    var item = m_ReportItems.GetArrayElementAtIndex(i);

                    var typeProp     = item.FindPropertyRelative("type");
                    var codeProp     = item.FindPropertyRelative("code");
                    var messagesProp = item.FindPropertyRelative("messages");

                    var type = (LogType)typeProp.intValue;
                    var code = (LogCode)codeProp.intValue;

                    var icon = element.Q <VisualElement>("Icon");
                    switch (type)
                    {
                    case LogType.Error:
                    case LogType.Assert:
                    case LogType.Exception:
                        icon.RemoveFromClassList("info");
                        icon.AddToClassList("error");
                        break;

                    case LogType.Warning:
                        icon.RemoveFromClassList("info");
                        icon.AddToClassList("warning");
                        break;
                    }

                    var messages       = GetStringValues(messagesProp);
                    var ritem          = new LogItem(type, code, messages);
                    var reportItemText = ritem.ToString();
                    msg.text        = reportItemText;
                    element.tooltip = reportItemText;
                };
            }
            else
            {
                reportRoot.style.display = DisplayStyle.None;
            }

            for (int i = 0; i < numDeps; i++)
            {
                var x             = m_AssetDependencies.GetArrayElementAtIndex(i);
                var assetPathProp = x.FindPropertyRelative("assetPath");

                var typeProp = x.FindPropertyRelative("type");
                var type     = (GltfAssetDependency.Type)typeProp.enumValueIndex;
                if (type == GltfAssetDependency.Type.Texture)
                {
                    var importer = AssetImporter.GetAtPath(assetPathProp.stringValue) as TextureImporter;
                    if (importer != null)
                    {
                        if (importer.textureShape != TextureImporterShape.Texture2D)
                        {
                            maliciousTextureImporters.Add(importer);
                        }
                    }
                }
            }

            if (maliciousTextureImporters.Count > 0)
            {
                var dependencyTree = Resources.Load(dependencyMarkup) as VisualTreeAsset;

                root.Query <Button>("fixall").First().clickable.clicked += () => {
                    AssetDatabase.StartAssetEditing();
                    foreach (var maliciousTextureImporter in maliciousTextureImporters)
                    {
                        FixTextureImportSettings(maliciousTextureImporter);
                    }
                    AssetDatabase.StopAssetEditing();
                    Repaint();
                };

                var foldout = root.Query <Foldout>().First();
                // var row = root.Query<VisualElement>(className: "fix-texture-row").First();
                foreach (var maliciousTextureImporter in maliciousTextureImporters)
                {
                    var row = dependencyTree.CloneTree();
                    foldout.Add(row);
                    // textureRowTree.CloneTree(foldout);
                    var path = AssetDatabase.GetAssetPath(maliciousTextureImporter);
                    row.Query <Label>().First().text = Path.GetFileName(path);
                    row.Query <Button>().First().clickable.clicked += () => {
                        FixTextureImportSettings(maliciousTextureImporter);
                        row.style.display = DisplayStyle.None;
                    };
                }
            }
            else
            {
                var depRoot = root.Query <VisualElement>("Dependencies").First();
                depRoot.style.display = DisplayStyle.None;
            }

            root.Bind(serializedObject);

            // Apply the changes so Undo/Redo is working
            serializedObject.ApplyModifiedProperties();

            root.Add(new IMGUIContainer(ApplyRevertGUI));

            return(root);
        }