首页 技术 正文
技术 2022年11月15日
0 收藏 387 点赞 3,287 浏览 1656 个字

猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!

原文地址: http://blog.csdn.net/cocos2der/article/details/46854411

项目中,有时候导入一些资源时候,需要对应创建材质球,如果每次自己动手创建,还是挺麻烦的,下面是如何导入资源时候自动创建材质球。

using UnityEngine;using System.Collections;using System.Collections.Generic;using UnityEditor;using System.IO;public class BuildMaterial : UnityEditor.AssetModificationProcessor{    //生成出的Material的路径    private static string MaterialsPath = "Assets/Resources/Skin/";    // 创建菜单按钮,手工调用创建材质    [MenuItem ("HETools/BuildMaterials")]    static void CreateMateral ()    {        Object[] selectObject = Selection.objects;        List<string> path = new List<string> ();        foreach (Object obj in selectObject) {            path.Add (AssetDatabase.GetAssetPath (obj));        }        foreach (string p in path) {            CreateOneMateral (p);        }        System.GC.Collect ();    }    // 监控assets资源添加,发现指定目录ThemeTile有新增加的texture,就自动生成材质    public static void OnWillCreateAsset (string path)    {        int index = path.LastIndexOf (".");        string file = path.Substring (index);        string[] pathArr = path.Split ('/');        if (pathArr [pathArr.Length - 3] != "ThemeTile")            return;        CreateOneMateral (path);        System.GC.Collect ();    }    // 创建材质球    static void CreateOneMateral (string p)    {        p = p.Replace (".meta", "");        Debug.Log ("CreateOneMateral from path: " + p);        int pos = p.LastIndexOf ('/');        if (pos == -1)            return;        string[] strArr = p.Split ('/');        string themeIDStr = strArr [strArr.Length - 2];        Texture textur = (Texture)AssetDatabase.LoadAssetAtPath (p, typeof(Texture)) as Texture;        string name = strArr [strArr.Length - 1];        int y = name.IndexOf ('.');        name = name.Substring (0, y);        Material mater = new Material (Shader.Find ("Mobile/VertexLit"));        mater.mainTexture = textur;        AssetDatabase.CreateAsset (mater, MaterialsPath + themeIDStr + "/" + name + ".mat");    }}

注意,上面代码中我是规定了只有指定的目录添加texture才会自动生成材质,所以使用时候,请自行修改下。

这里发现了个问题:

导入贴图时候,自动创建出来的材质球丢失了纹理图,而采用菜单按钮点击创建出来的正常。问题还没有解决,有哪位朋友知道解决办法可以告诉我下。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919