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 /
avatar image
0
Question by unit_nick · Sep 19, 2017 at 07:27 AM · shadermaterialrendererdynamic

Is there any way to create a dynamic material when no materials have been compiled?

In the process of creating a dynamic scene I discovered that it would not render the materials when built.

From what I can gather unless you actually compile the script with an object that has a material the resources required to build a material will not be included.

Is there any way (that I haven't been able to discover after quite a bit of searching) to dynamically create a material that will render in a build?

I seek to build everything from scripts and an empty game object. No other assets, resources, etc.

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

2 Replies

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

Answer by unit_nick · Sep 22, 2017 at 05:04 AM

I have found the solution.

  1. Create a scripted shader. This can be done automatically by selecting Create > Shader in your Project folder in the editor.

  2. You then need to create a new material with this shader. I do it by creating an object field in the inspector. public Shader shader; which you can drop your shader script into. Then create your material Material material = new Material(shader);

  3. And finally you attach this material to your objects gameObject.GetComponent<Renderer>().material = material;

Comment
Add comment · Show 3 · 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 unit_nick · Sep 22, 2017 at 05:21 AM 0
Share

You might say this breaks my requirements for no other assets but because you can write the shader it meets my requirements.

avatar image fafase unit_nick · Oct 09, 2017 at 05:41 AM 0
Share

How's that differ from the answer I gave?

avatar image unit_nick fafase · Oct 09, 2017 at 06:06 AM 0
Share

I seek to build everything from scripts and an empty game object. No other assets, resources, etc.

Because I fail to see how you have addressed the above

just make sure you have all the needed assets first.

avatar image
1

Answer by fafase · Sep 19, 2017 at 07:34 AM

You need a reference to the shader. Unity only compiles assets that are used or in the Resources/StreamingAssets folders.

If you need to create a material at runtime, just make sure you have all the needed assets first.

 public class MaterialFactory:MonoBehaviour
 {
        [SerializeField] private Shader shader;
        public Material CreateMaterial()
        {
             return new Material(shader);
        }
 }

Click the circle icon on the shader slot, it will open up the inspector, you can now assign a shader and it will be shipped with the build if you place the script on a GameObject in a scene.

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 unit_nick · Sep 19, 2017 at 07:42 AM 0
Share

Including assets means it is no longer dynamic. I want everything to be driven from a script on an empty game object. I have updated my OP to reflect this. Thanks for your response.

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

117 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

Related Questions

Changing two different objects renderer colour 1 Answer

Rendering an entire object as transparent 1 Answer

How to set pre-render properties to UGUI Image? 0 Answers

Cannot change material on build 1 Answer

Using 5.5 selection highlighting from editor in game 0 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