示例#1
0
    //--------------------------------------------------------------------------------------------------
    // IO

    //Save .shader file
    private static Shader SaveShader(TCP2_Config config, Shader existingShader, string sourceCode, bool overwritePrompt, bool modifiedPrompt)
    {
        if (string.IsNullOrEmpty(config.Filename))
        {
            Debug.LogError("[TCP2 Shader Generator] Can't save Shader: filename is null or empty!");
            return(null);
        }

        //Save file
        var outputPath = OutputPath;
        var filename   = config.Filename;

        //Get existing shader exact path
        if (existingShader != null)
        {
            outputPath = GetExistingShaderPath(config, existingShader);

            /*
             * if(config.Filename.Contains("/"))
             * {
             *      filename = config.Filename.Substring(config.Filename.LastIndexOf('/')+1);
             * }
             */
        }

        var systemPath = Application.dataPath + outputPath;

        if (!Directory.Exists(systemPath))
        {
            Directory.CreateDirectory(systemPath);
        }

        var fullPath  = systemPath + filename + ".shader";
        var overwrite = true;

        if (overwritePrompt && File.Exists(fullPath))
        {
            overwrite = EditorUtility.DisplayDialog("TCP2 : Shader Generation", "The following shader already exists:\n\n" + fullPath + "\n\nOverwrite?", "Yes", "No");
        }

        if (modifiedPrompt)
        {
            overwrite = EditorUtility.DisplayDialog("TCP2 : Shader Generation", "The following shader seems to have been modified externally or manually:\n\n" + fullPath + "\n\nOverwrite anyway?", "Yes", "No");
        }

        if (overwrite)
        {
            var directory = Path.GetDirectoryName(fullPath);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            //Write file to disk
            File.WriteAllText(fullPath, sourceCode, Encoding.UTF8);
            AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);

            //Import (to compile shader)
            var assetPath = fullPath.Replace(Application.dataPath, "Assets");

            var shader = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Shader)) as Shader;
            if (SelectGeneratedShader)
            {
                Selection.objects = new Object[] { shader };
            }

            //Set ShaderImporter userData
            var shaderImporter = ShaderImporter.GetAtPath(assetPath) as ShaderImporter;
            if (shaderImporter != null)
            {
                //Get file hash to verify if it has been manually altered afterwards
                var shaderHash = GetShaderContentHash(shaderImporter);

                //Use hash if available, else use timestamp
                var customDataList = new List <string>();
                customDataList.Add(!string.IsNullOrEmpty(shaderHash) ? shaderHash : shaderImporter.assetTimeStamp.ToString());
                customDataList.Add(config.GetShaderTargetCustomData());
                var configTypeCustomData = config.GetConfigTypeCustomData();
                if (configTypeCustomData != null)
                {
                    customDataList.Add(configTypeCustomData);
                }
                customDataList.Add(config.GetConfigFileCustomData());

                var userData = config.ToUserData(customDataList.ToArray());
                shaderImporter.userData = userData;

                //Needed to save userData in .meta file
                AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.Default);
            }
            else
            {
                Debug.LogWarning("[TCP2 Shader Generator] Couldn't find ShaderImporter.\nMetadatas will be missing from the shader file.");
            }

            return(shader);
        }

        return(null);
    }