Ejemplo n.º 1
0
        /// <summary>
        /// Helper method that attempts to store the object as a string to a runtime
        /// resource that can be loaded with <see cref="Resources.Load()"/>. Only
        /// types that are supported by the resource manifest system will be considered.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="info"></param>
        /// <param name="context"></param>
        /// <returns><c>true</c> if the object was added to the SerializationInfo as a rsource string, <c>false</c> otherwise.</returns>
        protected static bool SerializeAsResource(object obj, SerializationInfo info, StreamingContext context)
        {
            if (obj == null)
            {
                return(false);
            }
            var sc = context.Context as XmlSerializer.SerializeContext;


            //let's see if this is a unity resource that can be stored as just a string
            Type objType = obj.GetType();

            for (int i = 0; i < Constants.ResourceTypes.Length; i++)
            {
                if (objType == Constants.ResourceTypes[i])
                {
                    UnityEngine.Object uo = obj as UnityEngine.Object;

                    //before we check the manifests, see if this object exists in the BuiltinResources list
                    int uId = SerializerBase.UnityResources.Resources.IndexOf(uo);
                    if (uId >= 0)
                    {
                        //we have a unity, builtin resource, we need to serialize a slightly different way.
                        sc.Element.SetAttribute("BuiltinId", uId.ToString());
                    }

                    //try to serialize this as a runtime
                    //resource path if it exists in a manifest
                    string manifestFile = "Manifests/" + ResourceManifest.CleanResourceObjectName(uo.name);
                    var    manifest     = Resources.Load(manifestFile, typeof(ResourceManifest)) as ResourceManifest;
                    if (manifest != null)
                    {
                        string path = manifest.GetPathToResource(uo);
                        if (!string.IsNullOrEmpty(path))
                        {
                            //Make it an attribute rather than a node.
                            //That way we can identify if it was serialized
                            //in-place or not during deserialization.
                            sc.Element.SetAttribute("ResourcePath", path);
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 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);
        }