Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="manifestPath"></param>
        /// <param name="assetPath"></param>
        /// <param name="safetyChecks"></param>
        static void RecordAsset(string manifestPath, string path, string file, Type type, UnityEngine.Object obj, Dictionary <string, ResourceManifest> map, List <ResourceManifest> list, bool safetyChecks)
        {
            //We use this for the manifest rather than 'file' because
            //we can't determine the other one at runtime due to the use
            //of AssetDatabase. This only requires the object's name.
            string manifestName = ResourceManifest.GetManifestName(obj.name);


            //obtain the manifest that stores all resources with this name
            ResourceManifest manifest = null;

            map.TryGetValue(manifestName, out manifest);
            if (manifest == null)
            {
                try { manifest = CreateManifestAsset(manifestPath + manifestName + ".asset"); }
#pragma warning disable 0168
                catch (Exception e)
                {
                    Debug.Log("<color=red>Failed to create asset: " + manifestName + "</color>");
                    return;
                }
#pragma warning restore 0168
                if (manifest == null)
                {
                    Debug.Log("<color=red>Failed to create asset: " + manifestName + "</color>");
                    return;
                }
                map.Add(manifestName, manifest);
                list.Add(manifest);
            }

            //are we going to look for repeat paths to
            //different resources of the same type? (slow but safe)
            string fullPath = path + file;

            if (safetyChecks)
            {
                if (SafetyCheck(manifest, fullPath, type))
                {
                    manifest.AddResource(obj, fullPath);
                }
                else
                {
                    Debug.Log("<color=red>WARNING:</color>There are multiple resources of the type '" + type.Name + "' that are being compiled to the path 'Resources/" + fullPath + "'.\nThe manifest cannot determine which object this path should point to so only the first occurance has been stored.\nPlease ensure all resources of the same type and at the same directory level relative to 'Resources/' have unique names.");
                }
            }
            else
            {
                manifest.AddResource(obj, fullPath);
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        static List <ResourceManifest> BuildManifestLibrary(string manifestPath, bool safetyChecks, params Type[] supportedTypes)
        {
            List <ResourceManifest> list = new List <ResourceManifest>(10);
            Dictionary <string, ResourceManifest> map = new Dictionary <string, ResourceManifest>(10);

            //-remove all old manifests
            //-get all resources
            //-group by name
            //-assign each group to a resource manifest that is newly created
            foreach (var type in supportedTypes)
            {
                string[] guids = AssetDatabase.FindAssets("t:" + type.Name);
                for (int gi = 0; gi < guids.Length; gi++)
                {
                    string guid      = guids[gi];
                    var    obj       = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), type);
                    string assetPath = AssetDatabase.GetAssetPath(obj);

                    //check to see if it is a Unity built-in resource, also make sure it is in a 'Resources' folder.
                    if (!assetPath.StartsWith("Assets/") || assetPath.IndexOf("Resources/") == -1)
                    {
                        continue;
                    }
                    else if (type == typeof(GameObject))
                    {
                        //only support GameObject prefabs for now
                        //TODO: Add support for model prefabs
                        if (PrefabUtility.GetPrefabType(obj) != PrefabType.Prefab)
                        {
                            continue;
                        }
                    }

                    string path = AssetPathToResourcePath(assetPath);
                    string file = AssetPathToResourceName(assetPath);
                    //We use this for the manifest rather than 'file' because
                    //we can't determine the other one at runtime due to the use
                    //of AssetDatabase. This only requires the object's name.
                    string manifestName = ResourceManifest.CleanResourceObjectName(obj.name);

                    //obtain the manifest that stores all resources with this name
                    ResourceManifest manifest = null;
                    map.TryGetValue(manifestName, out manifest);
                    if (manifest == null)
                    {
                        manifest = CreateManifestAsset(manifestPath + manifestName + ".asset");
                        map.Add(manifestName, manifest);
                        list.Add(manifest);
                    }

                    //are we going to look for repeat paths to
                    //different resources of the same type? (slow but safe)
                    string fullPath = path + file;
                    if (safetyChecks)
                    {
                        if (SafetyCheck(manifest, fullPath, type))
                        {
                            manifest.AddResource(obj, fullPath);
                        }
                        else
                        {
                            Debug.Log("<color=red>WARNING:</color>There are multiple resources of the type '" + type.Name + "' that are being compiled to the path 'Resources/" + fullPath + "'. The manifest cannot determine which object this path should point to so only the first occurance has been stored. Please ensure all resources of the same type and at the same directory level relative to 'Resources/' have unique names.");
                        }
                    }
                    else
                    {
                        manifest.AddResource(obj, fullPath);
                    }
                }
            }

            PushObjectsToAssets(list);
            return(list);
        }