Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ReticentDaikaiju · May 14, 2017 at 01:52 AM · materialshadersgraphicsruntimeload asset

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);
     }
 }



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ReticentDaikaiju · May 14, 2017 at 07:19 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andywatts · Apr 07, 2020 at 02:55 PM 0
Share

No UnityEditor in a build tho, right? Any other way to load precompiled shaders at runtime?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shadows passing through objects, why? 4 Answers

Best way to render hundreds of textures for flooring in an in-game editor? 0 Answers

Shader displaying with weird colors 1 Answer

Accessing decal's material's parameters in animation window. 0 Answers

Shader graph has incorrect position (VERSION 10.5) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges