• LittleTool之批量修改材质


    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    
    public class ChangeMaterial : EditorWindow
    {        static string path = "Assets/_Materials/";
            static string shaderName="Custom/CurvedWorld";
            
            static string tempName1="_Curvature";//材质球参数,需要手动修改
            static string tempName2="_Axis";
            static float temp1=1;
            static float temp2=1;
    
            public static  string  [] postfix =
            {
                    ".png",".jpg",".tga","psd"
            };
    
            [MenuItem ("Custom/ChangeMaterial")]     
            public static void Change()
            {
                    if (Selection.activeGameObject != null) {
    
                            foreach (GameObject go in Selection.gameObjects) {
                                    Renderer render = go.GetComponentInChildren<Renderer> ();
                                    if (render != null) {                                      
                                            
                                            Texture texture = GetTexture (go.name);//根据对象名获取图片
                                            if (texture != null) {
                                                    Material newMat = new Material (Shader.Find(shaderName));
    
                                                    newMat.SetFloat(tempName1,temp1);
                                                    newMat.SetFloat (tempName2,temp2);
    
                                                    AssetDatabase.CreateAsset (newMat, path + go.name+".mat");
                                                    render.sharedMaterial=newMat;
                                                    render.sharedMaterial.mainTexture = texture;
                                                    Debug.Log ("成功!");
                                            } else {
                                                    Debug.Log ("失败!");
                                            }
                                    }
                            }
                    }
            } 
            static Texture GetTexture(string name)
            {
                    foreach(string str in postfix)
                    {
                            Texture texture = AssetDatabase.LoadAssetAtPath("Assets/_Textures/" + name+str,typeof(Texture)) as Texture;
                            if(texture != null)
                                    return texture;
                    }
                    return null;                                            
            }
    }

    注意:

      1.将要用到的模型都制作为预制体!!!

      2.手动修改参数

      

  • 相关阅读:
    Longest Valid Parentheses
    Gas Station
    Multiply Strings
    LeetCode:Merge Sorted Array
    LeetCode:Single Number II
    LeetCode: Single Number
    LeetCode:3Sum
    LeetCode:Binary Tree Preorder Traversal
    LeetCode: Best Time to Buy and Sell Stock III
    LeetCode: Best Time to Buy and Sell Stock II
  • 原文地址:https://www.cnblogs.com/chimo523/p/5201489.html
Copyright © 2020-2023  润新知