Example #1
0
        private void GenerateTreeElements(AuditProfile profile, TreeViewItem root)
        {
            List <string> associatedAssets = GetFilteredAssets(profile);

            // early out if there are no affected assets
            if (associatedAssets.Count == 0)
            {
                Debug.Log("No matching assets found for " + profile.name);
                return;
            }

            string        activePath   = "Assets";
            AssetViewItem assetsFolder = new AssetViewItem(activePath.GetHashCode(), 0, activePath, true);

            if (assetsFolder.children == null)
            {
                assetsFolder.children = new List <TreeViewItem>();
            }
            root.AddChild(assetsFolder);

            Dictionary <int, AssetViewItem> items = new Dictionary <int, AssetViewItem>
            {
                { activePath.GetHashCode(), assetsFolder }
            };

            foreach (var assetPath in associatedAssets)
            {
                // split the path to generate folder structure
                string path    = assetPath.Substring(7);
                var    strings = path.Split(new[] { '/' }, StringSplitOptions.None);
                activePath = "Assets";

                AssetViewItem active = assetsFolder;

                // for each module
                List <IConformObject> conformData = profile.GetConformData(assetPath);
                bool result = true;
                for (int i = 0; i < conformData.Count; ++i)
                {
                    if (conformData[i].Conforms == false)
                    {
                        result = false;
                        break;
                    }
                }

                if (!result && assetsFolder.conforms)
                {
                    assetsFolder.conforms = false;
                }

                AssetImporter    assetImporter   = AssetImporter.GetAtPath(assetPath);
                SerializedObject assetImporterSO = new SerializedObject(assetImporter);

                // the first entries have lower depth
                for (int i = 0; i < strings.Length; i++)
                {
                    activePath += "/" + strings[i];
                    int id = activePath.GetHashCode();

                    if (i == strings.Length - 1)
                    {
                        AssetViewItem item = new AssetViewItem(id, i + 1, strings[i], result)
                        {
                            icon        = AssetDatabase.GetCachedIcon(assetPath) as Texture2D,
                            path        = activePath,
                            conformData = conformData,
                            assetObject = assetImporterSO,
                            isAsset     = true
                        };
                        active.AddChild(item);
                        active = item;
                        if (items.ContainsKey(id))
                        {
                            Debug.LogError("id already in for " + activePath);
                        }
                        items.Add(id, item);
                    }
                    else
                    {
                        AssetViewItem item;
                        if (!items.TryGetValue(id, out item))
                        {
                            item = new AssetViewItem(id, i + 1, strings[i], result)
                            {
                                path = activePath,
                                icon = AssetDatabase.GetCachedIcon(activePath) as Texture2D
                            };
                            active.AddChild(item);
                            items.Add(id, item);
                        }
                        else if (result == false)
                        {
                            if (item.conforms)
                            {
                                item.conforms = false;
                            }
                        }

                        active = item;
                    }
                }
            }
        }