Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Change parameters of all materials/HDRP lit shaders located in a folder

Discussion in 'Scripting' started by DerekMcKinley, Dec 22, 2022.

  1. DerekMcKinley

    DerekMcKinley

    Joined:
    Jun 24, 2014
    Posts:
    19
    Please, would someone be so kind as to help me complete my script? It must be executed in edit mode.

    Any help would be fantastic

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class FixMats : EditorWindow
    5. {
    6.     public static Material myMaterials;
    7.  
    8.     [MenuItem("Tools/FixMats")]
    9.     static void Init()
    10.     {
    11.         //colllect all material files in especific folder
    12.         //myMaterials = (Material)AssetDatabase.LoadAssetAtPath("Assets/Materials", typeof(Material));
    13.     }
    14.  
    15.     void OnGUI()
    16.     {
    17.         if (GUILayout.Button("Fix materials!"))
    18.         {
    19.             //code for fix all materials
    20.             //example: change smothness value
    21.  
    22.         }
    23.     }
    24. }
    25.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    You may edit your post above.

    The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

    This assumes you have at least written and studied some code and have run into some kind of issue.

    If you haven't even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

    If you just want someone to do it for you, you need go to one of these places:

    https://forum.unity.com/forums/commercial-job-offering.49/

    https://forum.unity.com/forums/non-commercial-collaboration.17/

    https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons
     
  3. DerekMcKinley

    DerekMcKinley

    Joined:
    Jun 24, 2014
    Posts:
    19
    I guess now I should applaud for your dramatic performance.:D

    Writing all that took you more time than sending me at least one link with relevant information, but I guess you only come to this forum to be sarcastic.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    I am not a mind-reader. You appear to want some kind of editor script and I honestly have no idea what you want besides some properties changed, as you said,

    "Change parameters of all materials/HDRP lit chaders located in a folder"

    But if you think you have communicated clearly for total strangers to be able to help you, then by all means, leave your post as it is.
     
    DerekMcKinley likes this.
  5. DerekMcKinley

    DerekMcKinley

    Joined:
    Jun 24, 2014
    Posts:
    19
    I admit that I did not investigate enough, but I already did it myself, here is the code for future interested in the subject, the script is used to change or add values and textures to a large number of materials at the same time.

    However, I still haven't been able to modify a material to make it crystal type.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Linq;
    4. using System;
    5.  
    6. public class JCTOOLS : MonoBehaviour
    7. {
    8.     public static Material[] mats;
    9.  
    10.     public enum SurfaceType
    11.     {
    12.         Opaque,
    13.         Transparent
    14.     }
    15.  
    16.     [MenuItem("HelpTools/Load Materials")]
    17.     static void LoadMats()
    18.     {    
    19.         mats = Resources.LoadAll<Material>("Materials") as Material[];
    20.         Debug.Log(mats.Length.ToString() + " Mats Loaded");
    21.     }
    22.  
    23.     static void loadTex(Material mat, string sufix) {
    24.         string name = mat.mainTexture.name;
    25.         Texture2D Ntex = (Texture2D)Resources.Load("Textures/" + name + sufix);
    26.  
    27.         mat.SetTexture("_NormalMap", Ntex);
    28.  
    29.     }
    30.  
    31.     [MenuItem("HelpTools/Fix Materials")]
    32.     static void FixMats()
    33.     {
    34.         for (int i = 0; i < mats.Length; i++)
    35.         {
    36.             string[] sufixs = new string[] { "_N", "_NRM", "_Normal" };
    37.  
    38.             foreach (string sufix in sufixs) {
    39.                 if (mats[i].GetTexture("_NormalMap") == null)
    40.                 {
    41.                     try
    42.                     {
    43.                         loadTex(mats[i], sufix);
    44.                     }
    45.                     catch (Exception e)
    46.                     {
    47.                         print("error: cur mat do no have main texture");
    48.                     }
    49.                  
    50.                     //Debug.Log(" Texture no found, in mat " + mats[i].name);
    51.                 }
    52.             }
    53.  
    54.             mats[i].SetFloat("_Metallic", 0.0f);
    55.  
    56.             if (mats[i].name.Contains("Glass"))
    57.             {
    58.                 mats[i].SetFloat("_Smoothness", 0.85f);
    59.                 mats[i].SetFloat("_Surface", (float)SurfaceType.Transparent);
    60.                 Debug.Log("Glass material changed");
    61.             }
    62.             else
    63.             {
    64.                 mats[i].SetFloat("_Smoothness", 0.2f);
    65.             }
    66.         }
    67.         Debug.Log("Finished");
    68.     }
    69. }
    70.  
    71.  
    72. /*--------------------- HDRP Lit Shader Properties:
    73. _BaseCoIor
    74. _BaseColorMap
    75. Basecolor(Color)
    76. BaseColorMap(Texture)
    77. _BaseColorMap_Miplnfo _BaseColorMap_Miplnfo(Vector)
    78. _Metallic
    79. _Smoothness
    80. _MaskMap
    81. _MetallicRemapMin
    82. _MetallicRemapMax
    83. _Metallic (Range)
    84. Smoothness(Range)
    85. MaskMap(Texture)
    86. MetallicRemapMin(Float)
    87. MetallicRemapMax(Float)
    88. _SmoothnessRemapMin SmoothnessRemapMin(Float)
    89. _SmoothnessRemapMa) SmoothnessRemapMax(Float)
    90. AORemapMin
    91. _AORemapMax
    92. _NormalMap
    93. _NormaIMapos
    94. _NormalScale
    95. _BentNormalMap
    96. _BentNormalMapOS
    97. _HeightMap
    98. _HeightAmplitude
    99. _HeightCenter
    100. AORemapMin (Float)
    101. AORemapMax(Float)
    102. NormalMap(Texture)
    103. NormalMapOS(Texture)
    104. _NormalScale(Range)
    105. _BentNormalMap(Texture)
    106. _BentNormalMapOS(Texture)
    107. HeightMap(Texture)
    108. Height Amplitude(Float)
    109. Height Center(Range)
    110. _HeightMapParametriza Heightmap Parametrization (Float)
    111. _HeightOffset
    112. _HeightMin
    113. _HeightMax
    114. _HeightTessAmplitude
    115. _HeightTessCenter
    116. _HeightPoMAmplitude
    117. _DetailMap
    118. _DetailAlbedoScale
    119. Height Offset (Float)
    120. Heightmap Min(Float)
    121. Heightmap Max(Float)
    122. Amplitude(Float)
    123. Height Center(Range)
    124. Height Amplitude(Float)
    125. DetailMap(Texture)
    126. _DetailAlbedoScale(Range)
    127. */
    128.  
     
    Last edited: Dec 27, 2022