Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by wolfen231 · Jul 11, 2014 at 01:42 PM · texturesautomation

Is it possible to automate material creation with new textures?

I have many textures that I need to turn in to materials. Is there a fast way to do this or script this?

For example if I could right click a single or group of textures and select from the right click menu (or use a pop up menu) "Create Material/s from Textures" that would then create materials with the same name and maybe an appendance of _mat or similar?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Bunny83 · Jul 11, 2014 at 02:09 PM

Sure, just use a script like this:

 // CreateMaterialsForTextures.cs
 // C#
 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Linq;
 
 public class CreateMaterialsForTextures : Editor
 {
     [MenuItem("Tools/CreateMaterialsForTextures")]
     static void CreateMaterials ()
     {
         try
         {
             AssetDatabase.StartAssetEditing();
             var textures = Selection.GetFiltered(typeof(Texture), SelectionMode.Assets).Cast<Texture>();
             foreach(var tex in textures)
             {
                 string path = AssetDatabase.GetAssetPath(tex);
                 path = path.Substring(0,path.LastIndexOf("."))+".mat";
                 if (AssetDatabase.LoadAssetAtPath(path,typeof(Material)) != null)
                 {
                     Debug.LogWarning("Can't create material, it already exists: " + path);
                     continue;
                 }
                 var mat = new Material(Shader.Find("Diffuse"));
                 mat.mainTexture = tex;
                 AssetDatabase.CreateAsset(mat,path);
             }
         }
         finally
         {
             AssetDatabase.StopAssetEditing();
             AssetDatabase.SaveAssets();
         }
     }
 }

Keep in mind this is an editor script and need to be placed in a folder called "editor". Once you have this script in your project and it is compiled you can select one or multiple textures in your project panel and then click "Tools->CreateMaterialsForTextures" in the menubar.

Note: if the script just was compiled the Tools menu might not be visible yet but when you just click any menuitem it should appear.

This script will create a material next to the texture with the same name. However if there is already a material with that name it does nothing.

edit
Here's the same script but turned into a wizard which allows you to select a different shader before you create the materials:

 // CreateMaterialsForTextures.cs
 // C#
 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Linq;
 
 public class CreateMaterialsForTextures : ScriptableWizard
 {
     public Shader shader;
 
     [MenuItem("Tools/CreateMaterialsForTextures")]
     static void CreateWizard ()
     {
         ScriptableWizard.DisplayWizard<CreateMaterialsForTextures>("Create Materials", "Create");
 
     }
 
     void OnEnable()
     {
         shader = Shader.Find("Diffuse");
     }
 
     void OnWizardCreate ()
     {
         try
         {
             AssetDatabase.StartAssetEditing();
             var textures = Selection.GetFiltered(typeof(Texture), SelectionMode.Assets).Cast<Texture>();
             foreach(var tex in textures)
             {
                 string path = AssetDatabase.GetAssetPath(tex);
                 path = path.Substring(0,path.LastIndexOf("."))+".mat";
                 if (AssetDatabase.LoadAssetAtPath(path,typeof(Material)) != null)
                 {
                     Debug.LogWarning("Can't create material, it already exists: " + path);
                     continue;
                 }
                 var mat = new Material(shader);
                 mat.mainTexture = tex;
                 AssetDatabase.CreateAsset(mat,path);
             }
         }
         finally
         {
             AssetDatabase.StopAssetEditing();
             AssetDatabase.SaveAssets();
         }
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 11, 2014 at 02:12 PM 0
Share

ps: I used the default "Diffuse" shader when creating the material. If you want something else, either change the script, or create a wizard.

avatar image wolfen231 · Jul 11, 2014 at 11:06 PM 0
Share

Oh that is beautiful. Exactly what I needed. I am going to study this a bit, because I originally wanted to try and script this out myself. Scripting / Coding is not my usual thing.

Thanks so much. =)

avatar image nicholascarrow · Nov 19, 2021 at 10:18 PM 1
Share

updated for universal render pipeline:

 // CreateMaterialsForTextures.cs
 // C#
 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Linq;
 
 public class CreateMaterialsForTextures : ScriptableWizard
 {
     public Shader shader;
 
     [MenuItem("Tools/CreateMaterialsForTextures")]
     static void CreateWizard()
     {
         ScriptableWizard.DisplayWizard<CreateMaterialsForTextures>("Create Materials", "Create");
 
     }
 
     void OnEnable()
     {
         shader = Shader.Find("Universal Render Pipeline/Lit");
     }
 
     void OnWizardCreate()
     {
         try
         {
             AssetDatabase.StartAssetEditing();
             var textures = Selection.GetFiltered(typeof(Texture), SelectionMode.Assets).Cast<Texture>();
             foreach (var tex in textures)
             {
                 string path = AssetDatabase.GetAssetPath(tex);
                 path = path.Substring(0, path.LastIndexOf(".")) + ".mat";
                 if (AssetDatabase.LoadAssetAtPath(path, typeof(Material)) != null)
                 {
                     Debug.LogWarning("Can't create material, it already exists: " + path);
                     continue;
                 }
                 var mat = new Material(shader);
                 mat.SetTexture("_BaseMap", tex);
                 AssetDatabase.CreateAsset(mat, path);
             }
         }
         finally
         {
             AssetDatabase.StopAssetEditing();
             AssetDatabase.SaveAssets();
         }
     }
 }
avatar image
0

Answer by kleber-swf · Jul 11, 2014 at 02:06 PM

You could create an editor script to do this using AssetDatabase.CreateAsset as shown in

http://docs.unity3d.com/ScriptReference/AssetDatabase.CreateAsset.html

(the example creates a material, btw)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wolfen231 · Jul 11, 2014 at 11:07 PM 0
Share

Thank you for replying. For some reason I couldn't even find that in the docs. =\

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to make a image flash infront of the camera 1 Answer

How can I track/debug my scripts? 1 Answer

How to convert the MouseLook(Script) to a java script. 1 Answer

Help?! Changing Textures via mouse click 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges