Use the unity program material proceduralmaterial

Source: Internet
Author: User

Some Results use the material of the substance, but the attributes on the material cannot be called using the usual material method. proceduralmaterial is required to call the method,

Using unityengine; using system. collections; public class testproceduralmaterial: monobehaviour {public gameobject testcube; Public texture2d Tex; Public proceduralmaterial materialitem; void start () {proceduralmaterial substance = object. instantiate (materialitem) as proceduralmaterial; // load a program material testcube. renderer. sharedmaterial = substance; testcube. renderer. sharedmaterial. settexturescale ("_ maintex ", New vector2 (3, 3); // change UV. This is the normal material method proceduralpropertydescription [] inputs = substance. getproceduralpropertydescriptions (); foreach (proceduralpropertydescription input in inputs) // obtain the attributes of the program material. If you do not know the attributes, you can use this to obtain all the attribute names and types {debug. log ("input type =" + input. type + "input name =" + input. name);} substance. setproceduralboolean ("battens", true); // you can specify boolsubstance. setproceduralfloat ("battens_amount_x", 6f); // set fl Oatsubstance. setproceduralcolor ("wood_color", new color (0.8f, 0.5f, 0.2f); // sets the color of substance. setproceduraltexture ("input", Tex); // sets the texture substance. rebuildtextures (); // all modifications to the application are required .} // Update is called once per framevoid Update (){}}

Of course, there are also setproceduralenum and setproceduralvector. Set Properties as needed.

This problem occurs during loading.


Invalid unknown program material, but the value has been modified, and the image is assigned. For some reason, in proceduralmaterial and Renderer. this problem occurs when the attribute of material is changed together with the normal material,

I set the object in this way only when the same material has different parameters on different objects. instantiate (materialitem) as proceduralmaterial; // load a program material, so that the attributes of the main material ball are not modified, you can use the same substance on different objects to adjust the value to achieve different effects. If I do not use this sentence, the object. instantiate, it will be okay to load, and the display is normal, but only one material ball corresponds to one object. When other objects use this material ball again, when you modify the parameters, the main material ball is changed, and the object changes to the parameter effect you last modified.


This will show that this will only affect the display of interface parameters, without affecting the actual effect, but it is a bit uncomfortable to look at the invalid program material invalid procedural material, and I do not know whether my value is correct, if you want it, I have modified the parameters through the test. Which of the following heroes knows,

Finally, let's talk about it. renderer. sharedmaterial and Renderer. the difference between material and material is that we recommend that you use the former to share the material, but it will cause problems during local development and will affect the root material. In this way, you can modify the material attributes in your project, however, you can use the platform to determine whether to use the latter in a local project and publish the platform to use the former, because each call to the latter will produce a new material, which consumes too much memory.

Using unityengine; using system. collections; public class testproceduralmaterial: monobehaviour {public gameobject testcube; Public texture2d Tex; public material materialitem; void start () {testcube. renderer. sharedmaterial = materialitem; testcube. renderer. sharedmaterial. maintexturescale = new vector2 (3, 3); // change UV, which is the normal material method proceduralmaterial substance = testcube. renderer. sharedmaterial as proceduralmat Erial; // obtain the program material proceduralpropertydescription [] inputs = substance of the object. getproceduralpropertydescriptions (); foreach (proceduralpropertydescription input in inputs) // obtain the attributes of the program material. If you do not know the attributes, you can use this to obtain all the attribute names and types {debug. log ("input type =" + input. type + "input name =" + input. name);} substance. setproceduralboolean ("battens", true); // you can specify boolsubstance. setproceduralfloat ("battens_amount_x", 6f); // set floatsubstance. set Proceduralcolor ("wood_color", new color (0.8f, 0.5f, 0.2f); // sets the color of substance. setproceduraltexture ("input", Tex); // sets the texture substance. rebuildtextures (); // all modifications to the application are required .} // Update is called once per framevoid Update (){}}

There is no problem with such a program, but the root material is used for modification.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.