Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by archat01 · Jul 17, 2021 at 02:03 PM · shadertexturebuild

Seeking help to load unlit/texture dynamically in Unity 3D using C# and substitute of Shader.Find,Seeking help to load unlit/texture dynamically in Unity 3D using C#, substitute of Shader.Find

While developing an application recently I found a typical problem and seeking your help to resort into a solution. Previously I used to load unlit transparent textures dynamically with the following code:

         fileData = File.ReadAllBytes(filePath);
         Texture2D tex = new Texture2D(2, 2);
         tex.LoadImage(fileData);
         GetComponent<Renderer>().material.mainTexture = tex;

         rend = gameObject.GetComponent<Renderer>();
         Shader shader = Shader.Find("Unlit/Texture");
         rend.material.shader = shader;

Recently I found that Shader.Find("Unlit/Texture") doesn't work in case of Windows build, specifically when I'm running the exe with my Unity Editor closed. I'm trying to find a substitute to this without any luck so far. I know in the Unity official documentation they have mentioned about the unreliability of Shader.Find, but no substitute to that is mentioned anywhere.

Will eagerly wait for a reply.

Thanks in advance for your time.,While developing an application recently I found a typical problem and seeking your help to resort into a solution. Previously I used to load unlit textures dynamically with the following code:

         fileData = File.ReadAllBytes(filePath);
         Texture2D tex = new Texture2D(2, 2);
         tex.LoadImage(fileData);
         GetComponent<Renderer>().material.mainTexture = tex;

         rend = gameObject.GetComponent<Renderer>();
         Shader shader = Shader.Find("Unlit/Texture");
         rend.material.shader = shader;

Recently I found that Shader.Find("Unlit/Texture") doesn't work in case of Windows build, specifically when I'm running the exe with my Unity Editor closed. I'm trying to find a substitute to this without any luck so far. I know in the Unity official documentation they have mentioned about the unreliability of Shader.Find, but no substitute to that is mentioned anywhere.

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
0

Answer by BastianUrbach · Jul 17, 2021 at 03:58 PM

The problem is that Unity automatically detects which shaders (and other assets) are used in the application and only includes those in the build. In the UnityEditor, everything works fine but in the build, the shader does no longer exist. There are a couple different ways around that. Just pick whichever is the most convenient for you.

  1. Instead of finding the shader dynamically, make a public field of type Shader and assign the shader there via the inspector, then use that field instead of Shader.Find.

  2. Add it to the list of always included shaders (Edit > Project Settings > Graphics > Always Included Shaders)

  3. Place it in a folder called Resources

  4. Use it somewhere in a scene that's part of the build. Doesn't have to be visible, you can just put it on a dummy GameObject and disable it. It's also sufficient to assign it to any public field of a MonoBehaviour, it doesn't need to be on a renderer. The only thing that matters is that Unity thinks you might need it.

Comment
Add comment · 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

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

218 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 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

Reflective surface/texture bug in build only (works in play mode in editor) 1 Answer

Distribute terrain in zones 3 Answers

Animated Texture based on Movement 0 Answers

Unity - Simple Water 3 Answers

How can I activate a shader at specific times? 2 Answers


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