Search Unity

A simple tool to apply multiple model material import settings

Discussion in 'Editor & General Support' started by CausticLasagne, Nov 26, 2020.

  1. CausticLasagne

    CausticLasagne

    Joined:
    Oct 2, 2015
    Posts:
    26
    ** I've posted an update to this script in a new thread! View it Here! **

    This tool allows you to search and remap multiple models in the project view at one time.
    To use it, select 'Caustic/Change Material Import Settings'.
    Setup a model the way you want and press 'Copy Settings!'.
    Select other models and press 'Apply Settings'.
    Editor Window, place it in your 'Scripts/Editor' folder.
    If it was helpful, I also make other games and software. Contributions will go to my next in-the-works Stealth/Action title.
    https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=QNBCJGGNLJ2LN&item_name=Thanks+for+supporting+me!&currency_code=AUD

    Screenshot 2020-11-26 222143.png Screenshot 2020-11-27 022620.png

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. /// <summary>
    6. /// ice cream for breakfast
    7. /// tacos for lunch
    8. /// burritoes for dinner
    9. /// pizza for dessert
    10. /// life is short eat dessert first
    11. /// </summary>
    12. ///
    13.  
    14. public class ChangeMaterialImportSettings : EditorWindow
    15. {
    16.     [MenuItem("Caustic/Change Material Import Settings")]
    17.     public static void InspectSelectionMenuSelected()
    18.     {
    19.         ChangeMaterialImportSettings win = new ChangeMaterialImportSettings();
    20.         win.position = new Rect(10, 10, 400, 400);
    21.         win.Show(true);
    22.     }
    23.  
    24.     private Rect guiRect = new Rect(0, 0, 400, 400);
    25.     ModelImporterMaterialName materialNameOption;
    26.     ModelImporterMaterialSearch materialSearchOption;
    27.  
    28.     public ChangeMaterialImportSettings() { }
    29.  
    30.     public void OnSelectionChange()
    31.     {
    32.         this.Repaint();
    33.     }
    34.  
    35.     public void OnGUI()
    36.     {
    37.         GUI.skin = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector);
    38.  
    39.         GUILayout.BeginArea(guiRect);
    40.         GUILayout.BeginVertical();
    41.  
    42.         DrawCurrentValues();
    43.  
    44.         GUILayout.Space(10F);
    45.  
    46.         GUILayout.BeginHorizontal();
    47.         if (GUILayout.Button("Close"))
    48.         {
    49.             this.Close();
    50.         }
    51.         if (GUILayout.Button("Copy Settings!"))
    52.         {
    53.             CopySettings();
    54.         }
    55.         if (GUILayout.Button("Apply!"))
    56.         {
    57.             HandleButtonClickApply();
    58.         }
    59.         GUILayout.EndHorizontal();
    60.  
    61.         GUILayout.Space(15F);
    62.  
    63.         DrawCurrentSelection();
    64.  
    65.         GUILayout.EndVertical();
    66.         GUILayout.FlexibleSpace();
    67.         GUILayout.EndArea();
    68.     }
    69.  
    70.     private void HandleButtonClickApply()
    71.     {
    72.         ModelImporter mdl = null;
    73.         try
    74.         {
    75.             if (Selection.objects != null)
    76.             {
    77.                 foreach (Object obj in Selection.objects)
    78.                 {
    79.                     if (obj is GameObject)
    80.                     {
    81.                         string pathName = AssetDatabase.GetAssetPath(obj);
    82.                         try
    83.                         {
    84.                             mdl = ModelImporter.GetAtPath(pathName) as ModelImporter;
    85.                         }
    86.                         catch(System.Exception ea)
    87.                         {
    88.                             Debug.LogError(AssetDatabase.GetAssetPath(obj) + " could not be converted to a model.");
    89.                             continue;
    90.                         }
    91.                         mdl?.SearchAndRemapMaterials(materialNameOption, materialSearchOption);
    92.                         mdl?.SaveAndReimport();
    93.                     }
    94.                     else
    95.                     {
    96.                         Debug.LogError(AssetDatabase.GetAssetPath(obj) + " is not a model.");
    97.                     }
    98.                 }
    99.                 Debug.LogError("Searched " + Selection.objects.Length + " models.");
    100.             }
    101.         }
    102.         catch(System.Exception ea)
    103.         {
    104.             Debug.LogError("There was a problem processing the operation.\n" + ea);
    105.         }
    106.     }
    107.  
    108.     private void CopySettings()
    109.     {
    110.         ModelImporter mdl = null;
    111.         try
    112.         {
    113.             if (Selection.objects != null)
    114.             {
    115.                 foreach (Object obj in Selection.objects)
    116.                 {
    117.                     if (obj is GameObject)
    118.                     {
    119.                         string pathName = AssetDatabase.GetAssetPath(obj);
    120.                         try
    121.                         {
    122.                             mdl = ModelImporter.GetAtPath(pathName) as ModelImporter;
    123.                             materialSearchOption = mdl.materialSearch;
    124.                             materialNameOption = mdl.materialName;
    125.                         }
    126.                         catch (System.Exception ea)
    127.                         {
    128.                             Debug.LogError(AssetDatabase.GetAssetPath(obj) + " model could not be read.");
    129.                             continue;
    130.                         }
    131.                     }
    132.                     else
    133.                     {
    134.                         Debug.LogError(AssetDatabase.GetAssetPath(obj) + " is not a model.");
    135.                     }
    136.  
    137.                     Debug.LogError("Got Settings from " + AssetDatabase.GetAssetPath(obj));
    138.                     break;
    139.                 }
    140.             }
    141.         }
    142.         catch (System.Exception ea)
    143.         {
    144.             Debug.LogError("There was a problem processing the operation.\n" + ea);
    145.         }
    146.     }
    147.  
    148.     private void DrawCurrentValues()
    149.     {
    150.         EditorGUILayout.LabelField("Automatically search and remap materials on a model.");
    151.         EditorGUILayout.LabelField("Malfunction if anything other than models from the project view is selected.");
    152.         EditorGUILayout.LabelField("Select the first model to copy its settings.");
    153.         EditorGUILayout.Space();
    154.         EditorGUILayout.LabelField("Material Search: " + materialSearchOption);
    155.         EditorGUILayout.LabelField("Material Search: " + materialNameOption);
    156.     }
    157.  
    158.     private void DrawCurrentSelection()
    159.     {
    160.         if (Selection.objects != null)
    161.         {
    162.             GUILayout.Label("Current selection:");
    163.             foreach (Object obj in Selection.objects)
    164.             {
    165.                 GUILayout.Label(string.Format("{0}: {1}", obj.GetType().Name, obj.name));
    166.             }
    167.         }
    168.     }
    169.  
    170.  
    171. }
     

    Attached Files:

    Last edited: Jun 23, 2021
    oAzuehT, h1ggs, DPunK and 2 others like this.