예제 #1
0
        void ExportShader()
        {
            var info = new ShaderTemplateConvertInfo();

            foreach (var kv in templateParser_.conditions)
            {
                var prop  = FindProperty(conditions_, kv.Key);
                var value = prop.FindPropertyRelative("value");
                info.conditions.Add(kv.Key, value.boolValue);
            }

            foreach (var kv in templateParser_.blocks)
            {
                var prop  = FindProperty(blocks_, kv.Key);
                var value = prop.FindPropertyRelative("value");
                info.blocks.Add(kv.Key, value.stringValue);
            }

            foreach (var kv in templateParser_.variables)
            {
                var prop       = FindProperty(variables_, kv.Key);
                var value      = prop.FindPropertyRelative("value");
                var constValue = ToConstVariable(kv.Key);
                if (constValue != null)
                {
                    value.stringValue = constValue;
                }
                info.variables.Add(kv.Key, value.stringValue);
            }

            var code = templateParser_.Convert(info);

            // rename if generator has a shader reference.
            if (hasShaderReference)
            {
                var shaderFilePath = AssetDatabase.GetAssetPath(shader_.objectReferenceValue);
                var shaderFileName = Path.GetFileNameWithoutExtension(shaderFilePath);
                var newFilePath    = GetShaderPath();

                if (GetShaderName() != shaderFileName)
                {
                    if (File.Exists(newFilePath))
                    {
                        throw new System.Exception(
                                  string.Format("attempted to rename {0} to {1}, but target file existed.",
                                                shaderFilePath, newFilePath));
                    }
                    AssetDatabase.RenameAsset(shaderFilePath, GetShaderName());
                }
            }

            using (var writer = new StreamWriter(GetShaderPath())) {
                writer.Write(code);
            }

            ReImport();

            if (hasShaderReference)
            {
                watcher_.Start(GetShaderPath());
            }
        }