Unity里获取Material里所有的Texture
来源:互联网 发布:excel两列数据找相同 编辑:程序博客网 时间:2024/05/29 10:13
背景:通过Material获取Texture的接口有:Material.mainTexture, Material.GetTexture(string propertyName); 但没有一个接口可以直接获取Material的所有textures.
解决方法一:
利用资源依赖,EditorUtility. CollectDependencies()。
static string [] GetMaterialTexturePaths( Material _mat ) { List<string > results = new List <string>(); Object[] roots = new Object[] { _mat }; Object[] dependObjs = EditorUtility. CollectDependencies(roots ); foreach (Object dependObj in dependObjs ) { if (dependObj .GetType() == typeof(Texture2D )) { string texpath = AssetDatabase.GetAssetPath (dependObj. GetInstanceID()); results.Add (texpath); } } return results .ToArray(); }
解决方法二:
利用序列化,得到Shader的Property,从而得到Shader里Texture相关的PropertyName。 Unity4.1版本以上,ShaderUtil已经提供相应接口。
static string[] GetCertainMaterialTexturePaths(Material _mat) { List<string > results = new List<string >(); Shader shader = _mat.shader; for (int i = 0; i < ShaderUtil.GetPropertyCount(shader); ++i) { if (ShaderUtil .GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType .TexEnv) { string propertyName = ShaderUtil .GetPropertyName(shader, i); Texture tex = _mat.GetTexture(propertyName); string texPath = AssetDatabase .GetAssetPath(tex.GetInstanceID()); results.Add(texPath); } } return results.ToArray(); }
对于ShaderUtil不提供对外接口的低版本,还有方法,参考http://answers.unity3d.com/questions/179255/a-way-to-iterateenumerate-shader-properties.html
using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using UnityEditor;using UnityEngine;using System.IO;using System.Collections; public static class ShaderUtilInterface{ public static Dictionary<string, MethodInfo> methods = new Dictionary<string, MethodInfo>(); static ShaderUtilInterface() { var asm = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a=>a.GetTypes().Any(t=>t.Name == "ShaderUtil")); if(asm != null) { var tp = asm.GetTypes().FirstOrDefault(t=>t.Name == "ShaderUtil"); foreach(var method in tp.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) { methods[method.Name] = method; } } } public static List<Texture> GetTextures(this Material shader) { var list = new List<Texture>(); var count = shader.GetPropertyCount(); for(var i = 0; i < count; i++) { if(shader.GetPropertyType(i)==4) { list.Add((Texture)shader.GetProperty(i)); } } return list; } public static int GetPropertyCount(this Material shader) { return Call<int>("GetPropertyCount", shader.shader); } public static int GetPropertyType(this Material shader, int index) { return Call<int>("GetPropertyType", shader.shader, index); } public static string GetPropertyName(this Material shader, int index) { return Call<string>("GetPropertyName", shader.shader, index); } public static void SetProperty(this Material material, int index, object value) { var name = material.GetPropertyName(index); var type = material.GetPropertyType(index); switch(type) { case 0: material.SetColor(name, (Color)value); break; case 1: material.SetVector(name, (Vector4) value); break; case 2: material.SetFloat(name, (float)value); break; case 3: material.SetFloat(name, (float)value); break; case 4: material.SetTexture(name, (Texture) value); break; } } public static object GetProperty(this Material material, int index) { var name = material.GetPropertyName(index); var type = material.GetPropertyType(index); switch(type) { case 0: return material.GetColor(name); case 1: return material.GetVector(name); case 2: case 3: return material.GetFloat(name); case 4: return material.GetTexture(name); } return null; } public static T Call<T>(string name, params object[] parameters) { return (T)methods[name].Invoke(null, parameters); } }
0 0
- Unity里获取Material里所有的Texture
- Unity里获取Material里所有的Texture
- Unity工程里图片的RGB和Alpha通道的分离,以及显示所有带有Alpha通道贴图的Material
- Unity工程里图片的RGB和Alpha通道的分离,以及显示所有带有Alpha通道贴图的Material
- 在table里动态获取tr里的所有数据
- 获取文件夹里的所有文件名称
- 获取浏览器里所有的cookie
- js 获取时间段里所有的月份
- 移除Unity工程里所有图片的Alpha通道
- 移除Unity工程里所有图片的Alpha通道
- 获取JSONObject里所有key
- Unity 获取UI中不同容器里的相对坐标
- 获取页面里所有复选框选中的值
- (一)如何获取Inbox里的所有消息
- 获取android手机里的所有应用,点击并响应
- 获取android手机里的所有应用,点击并响应
- 动态获取表单里的所有文本域和值
- 获取一个目录里的所有文件夹和文件
- WebForm 错误页
- 操作系统面试t
- 安装Oracle 11g数据库服务器
- Oracle性能分析9:重建索引
- hdu2473——Junk-Mail Filter(并查集+虚父节点的使用)
- Unity里获取Material里所有的Texture
- test
- 使用selector修改TextView中字体的颜色
- 【笔试题集合】字符串问题
- 计算机视觉资源汇总 - Part VII(研究组和个人主页)
- 进程间通信几种方式
- 堆栈与函数调用
- 【产品经理】产品经理使用Axure工具经验谈
- 启程