Created
March 29, 2021 17:22
-
-
Save danielbierwirth-unity/7d67ec7f2bdb3433f37afcd4ee4f657c to your computer and use it in GitHub Desktop.
small editor util - right click create measured materials shortcut for Unity Measured Materials Library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.Reflection; | |
| using UnityEditor; | |
| using UnityEngine; | |
| namespace com.Unity.Industrial.Tools | |
| { | |
| /// <summary> | |
| /// Adds create measured material menu entry. | |
| /// Creates measured material assets on right-click create in project view. | |
| /// | |
| /// Note: Measured Materials Library is required | |
| /// </summary> | |
| public class CreateMeasuredMaterial | |
| { | |
| // Create car paint material | |
| [MenuItem("Assets/Create/MeasuredMaterial/CarPaint")] | |
| private static void CreateMeasuredMaterials_CarPaint() | |
| { | |
| CreateMaterial("CarPaint"); | |
| } | |
| // Create glass material | |
| [MenuItem("Assets/Create/MeasuredMaterial/Glass")] | |
| private static void CreateMeasuredMaterials_Glass() | |
| { | |
| CreateMaterial("Glass_Simple"); | |
| } | |
| // Create metal material | |
| [MenuItem("Assets/Create/MeasuredMaterial/Metal")] | |
| private static void CreateMeasuredMaterials_Metal() | |
| { | |
| CreateMaterial("Metal"); | |
| } | |
| // Create mirror material | |
| [MenuItem("Assets/Create/MeasuredMaterial/Mirror")] | |
| private static void CreateMeasuredMaterials_Mirror() | |
| { | |
| CreateMaterial("Metal_Simple"); | |
| } | |
| // Create plastics material | |
| [MenuItem("Assets/Create/MeasuredMaterial/Plastic_Simple")] | |
| private static void CreateMeasuredMaterials_Plastic_Simple() | |
| { | |
| CreateMaterial("Plastic_Simple"); | |
| } | |
| // Create rubber material | |
| [MenuItem("Assets/Create/MeasuredMaterial/Rubber")] | |
| private static void CreateMeasuredMaterials_Rubber() | |
| { | |
| CreateMaterial("Rubber"); | |
| } | |
| // Create wood material | |
| [MenuItem("Assets/Create/MeasuredMaterial/Wood")] | |
| private static void CreateMeasuredMaterials_Wood() | |
| { | |
| CreateMaterial("Wood"); | |
| } | |
| // Create wood coated material | |
| [MenuItem("Assets/Create/MeasuredMaterial/WoodCoated")] | |
| private static void CreateMeasuredMaterials_WoodCoated() | |
| { | |
| CreateMaterial("WoodCoated"); | |
| } | |
| /// <summary> | |
| /// Gets the the currently selected folder path in project view - right click context menu | |
| /// </summary> | |
| /// <returns>The folder path as {string}</returns> | |
| private static string GetFolderPath() | |
| { | |
| MethodInfo getActiveFolderPath = typeof(ProjectWindowUtil).GetMethod("GetActiveFolderPath", BindingFlags.Static | BindingFlags.NonPublic); | |
| return (string)getActiveFolderPath.Invoke(null, null); ; | |
| } | |
| private static void CreateMaterial(string materialName) | |
| { | |
| try | |
| { | |
| string folderPath = GetFolderPath(); | |
| var uniqueFileName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/"+ materialName +".mat"); | |
| Material material = new Material(Shader.Find("Shader Graphs/"+ materialName)); | |
| CreateMaterialAsset(material, uniqueFileName); | |
| } | |
| catch (Exception exception) | |
| { | |
| Debug.Log(exception); | |
| } | |
| } | |
| private static void CreateMaterialAsset(Material material, string uniqueFileName) | |
| { | |
| AssetDatabase.CreateAsset(material, uniqueFileName); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment