Ejemplo n.º 1
0
        /// <summary>
        /// Deserialize the shader's attributes from UserData in the shader's importer.
        /// If none exists, it returns a new AttributeLayoutContaine instance.
        /// </summary>
        /// <param name="shader"></param>
        /// <returns></returns>
        internal static AttributeLayoutContainer LoadShaderMetaData(Shader shader)
        {
            if (shader == null)
            {
                throw new ArgumentNullException("shader");
            }

            string        path     = AssetDatabase.GetAssetPath(shader);
            AssetImporter importer = AssetImporter.GetAtPath(path);

            AttributeLayoutContainer data = AttributeLayoutContainer.Create(shader, null);

            JsonUtility.FromJsonOverwrite(importer.userData, data);
            return(data);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves the metadata of the shader passed in parameters, can overwrite if necessary
        /// </summary>
        /// <param name="shader">Shader associated with the metadata</param>
        /// <param name="attributes">Metadata to write</param>
        /// <param name="overwrite">Will overwrite if already existing file</param>
        /// <param name="logErrors">Log errors or not</param>
        /// <returns></returns>
        internal static string SaveMeshAttributesData(Shader shader, AttributeLayout[] attributes, bool overwrite = false, bool logErrors = true)
        {
            if (shader == null || attributes == null)
            {
                if (logErrors)
                {
                    Debug.LogError("Cannot save null attributes for shader.");
                }

                return(null);
            }

            string path             = FindPolybrushMetaDataForShader(shader);
            string shader_path      = AssetDatabase.GetAssetPath(shader);
            string shader_directory = Path.GetDirectoryName(shader_path);
            string shader_filename  = Path.GetFileNameWithoutExtension(path);

            // metadata didn't exist before
            if (string.IsNullOrEmpty(path))
            {
                if (string.IsNullOrEmpty(shader_path))
                {
                    // how!?
                    path = EditorUtility.SaveFilePanelInProject(
                        "Save Polybrush Shader Attributes",
                        shader_filename,
                        SHADER_ATTRIB_FILE_EXTENSION,
                        "Please enter a file name to save Polybrush shader metadata to.");

                    if (string.IsNullOrEmpty(path))
                    {
                        Debug.LogWarning(string.Format("Could not save Polybrush shader metadata.  Please try again, possibly with a different file name or folder path."));
                        return(null);
                    }
                }
                else
                {
                    shader_filename = Path.GetFileNameWithoutExtension(shader_path);
                    path            = string.Format("{0}/{1}.{2}", shader_directory, shader_filename, SHADER_ATTRIB_FILE_EXTENSION);
                }
            }

            if (!overwrite && File.Exists(path))
            {
                // @todo
                Debug.LogWarning("shader metadata exists. calling function refuses to overwrite and lazy developer didn't add a save dialog here.");
                return(null);
            }

            try
            {
                AttributeLayoutContainer container = AttributeLayoutContainer.Create(shader, attributes);
                string json = JsonUtility.ToJson(container, true);
                File.WriteAllText(path, json);

                //note: convert it here to be able to load it using AssetDatabase functions
                shader_filename = Path.GetFileNameWithoutExtension(shader_path);
                path            = string.Format("{0}/{1}.{2}", shader_directory, shader_filename, SHADER_ATTRIB_FILE_EXTENSION);
                //-------

                return(path);
            }
            catch (System.Exception e)
            {
                if (logErrors)
                {
                    Debug.LogError("Failed saving Polybrush Shader MetaData\n" + e.ToString());
                }
                return(path);
            }
        }