Search Unity

Upgrade Selected Material to URP Material via Script

Discussion in 'Universal Render Pipeline' started by cribin, Jun 18, 2021.

  1. cribin

    cribin

    Joined:
    Jul 11, 2018
    Posts:
    9
    Dear devs,

    I have a lot of materials that I want to convert to urp material and I would like to use scripting instead of clicking the editor menu. Is there a possibility to do so? I also tried the option Upgrade Project Material to URP Materials, but my material still remains pink.

    Best,

    Ribin
     
  2. ggzerosum

    ggzerosum

    Joined:
    Sep 26, 2017
    Posts:
    33
    Yes, you can.

    knowledge
    There are two things to remember:
    First, a Material is a Container that stores the values of the shader's variables and
    Second, Unity uses the variable's name when serializing it.

    Based on the above two facts, passing a value from one Material to another means assigning the value stored with specific variable name to a specific variable name in another Material.


    Suppose you want to assign the MainTexture value stored in Unity's default Material to a Material using your custom shader.

    Solution
    Assumptions

    Unity made signiture that store main texture as '_MainTex'.
    Your shader stored main texture as '_MyMainTex'.

    Pseudo code
    Get texture type value as 'TextureData' from Unity's material named with '_MainTex'
    Assign 'TextureData' to values named '_MyMainTex' in your target Material.

    + c# example
    Code (CSharp):
    1.         Material from; // Unity default
    2.         Material to; // your material
    3.  
    4.         Texture textureData = from.GetTexture("_MainTex");
    5.         to.SetTexture("_MyMainTex", textureData);

    Why your material remains pink?
    I guess your Material remains Pink because Unity's Shader and your Shader's Texture variable have different names. This answer may be inaccurate as I don't know your working environment
     

    Attached Files:

    Last edited: Jun 21, 2021