Is there any way to create and load a shader source file at runtime?

I am trying to create a shader at runtime based on a pre-existing template saved as a text file called “AnimationTextureTemplate.txt”, and modify it by inserting a user-specified function f(x,y,t), which it uses to draw an animated pattern. In older versions of Unity there used to be a way to create a new Material at runtime using the Material(string) constructor, but this is now obsolete. In Unity 5 the Material constructor requires that you load a pre-existing shader asset. I attempted to do this using the code below. Unfortunately, Unity apparently does not recognize shader code that is created after the program is initialized (?) because the Resource.Load command returns null the first time the program is run. When I run the program a second time though (after Test.shader already exists in the project from the first time) it is able to find and load it with no problems. Is there any way to get the shader to load on the first run? Or alternatively, can anyone think of another method I could use to accomplish the same task?

using UnityEngine;
using System.IO;

public class AnimationTexture : MonoBehaviour {

    public string function;

	void Start () {
        string shaderSourceCode = File.ReadAllText(Application.dataPath + "/Resources/RuntimeShaders/AnimationTextureTemplate.txt");
        string shaderName = "unlit/Test";
        shaderSourceCode = shaderSourceCode.Replace("[FUNCTION]", function);
        shaderSourceCode = shaderSourceCode.Replace("[SHADER_NAME]", shaderName);

        string fileName = Application.dataPath + "/Resources/RuntimeShaders/Test.shader";
        if (File.Exists(fileName))
        {
            Debug.Log(fileName + " already exists.");
        }
        else {
            StreamWriter sr = File.CreateText(fileName);
            sr.Write(shaderSourceCode);
            sr.Close();
        }
        Shader shader = Shader.Find(shaderName);
        if (shader != null)
        {
            Material material = new Material(shader);
            transform.GetComponent<Renderer>().material = material;
        }
        else
            Debug.LogWarning("Could not create material from " + shaderName);
    }
}

Solved it. I was just missing one crucial step - I needed to call UnityEditor.AssetDatabase.ImportAsset(fileName) after creating the shader file and before using Shader.Find(shaderName). Once I added this one line everything works perfectly.