zoukankan      html  css  js  c++  java
  • 一个批量更改模型材质、颜色、贴图的通用方法

    直接上代码:

        /// <summary>
        /// 更改模型材质
        /// </summary>
        /// <typeparam name="T">要更改的类型如【Color,Texture,Material】</typeparam>
        /// <param name="models">Transform数组</param>
        /// <param name="t"></param>
        private void UpdateModelMaterial<T>(Transform[] models,T t) where T:new()
        {
            if (!typeof(T).Equals(typeof(Color)) &&!typeof(T).Equals(typeof(Texture2D)) && !typeof(T).Equals(typeof(Texture)) && !typeof(T).Equals(typeof(Material))&& !typeof(T).Equals(typeof(Shader)))
                Debug.LogError("T的类型必须为Color,Texture,Material,Shader中的一个!");
            //模型材质更改
            foreach (var modelItem in models)
            {
                if (modelItem.renderer == null)
                    continue;
                for (int i = 0; i < modelItem.renderer.materials.Length; i++)
                {
                    if (typeof(T).Equals(typeof(Color)))
                        modelItem.renderer.materials[i].color = (Color)(object)t;
                    else if (typeof(T).Equals(typeof(Texture2D)))
                        modelItem.renderer.materials[i].mainTexture = (Texture2D)(object)t;
                    else if (typeof(T).Equals(typeof(Texture)))
                        modelItem.renderer.materials[i].mainTexture = (Texture)(object)t;
                    else if (typeof(T).Equals(typeof(Material)))
                        modelItem.renderer.materials[i] = (Material)(object)t;
                    else if (typeof(T).Equals(typeof(Shader)))
                        modelItem.renderer.materials[i].shader = (Shader)(object)t;
                }
            }
        }

    由于C#泛型约束不能使用or、||等多个类,只能控制台输出错误了。

  • 相关阅读:
    榨干PHP性能的使用细节
    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
    Oracle 表锁处理总结
    Windows安装NodeJS
    RedHed5.8 重装yum
    linux Redhat5.8 升级 openSLL 无法升级成功,解决办法
    idea安装详情
    Linux升级OpenSSH 和 OpenSSL 详细步骤
    Oracle dmp文件 exp导出,imp导入
    redis哨兵模式增加密码认证
  • 原文地址:https://www.cnblogs.com/townsend/p/4167849.html
Copyright © 2011-2022 走看看