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