当前位置:网站首页>Unity 模型整体更改材质
Unity 模型整体更改材质
2022-04-23 20:09:00 【m_dlwlrma】
using System;
using System.Collections.Generic;
using UnityEngine;
public class ModelTransparent : MonoBehaviour
{
/// <summary>
/// 用于保存模型原来得Material
/// </summary>
[HideInInspector] public List<MaterialList> Material_List = new List<MaterialList>();
/// <summary>
/// 透明材质
/// </summary>
public Material MaterialTransparent;
private void Awake()
{
RecordOriginalMaterials();
}
/// <summary>
/// 记录模型原本得材质
/// </summary>
void RecordOriginalMaterials()
{
MeshRenderer[] rendererArray = GetComponentsInChildren<MeshRenderer>();
if (rendererArray.Length != 0)
{
Material_List.Clear();
for (int i = 0; i < rendererArray.Length; i++)
{
int temp = i;
Material[] materialArray = rendererArray[temp].materials;
MaterialList materialList = new MaterialList();
if (materialArray.Length != 0)
{
for (int j = 0; j < materialArray.Length; j++)
{
int tempNumber = j;
materialList.materialData.Add(materialArray[tempNumber]);
}
Material_List.Add(materialList);
}
}
}
}
/// <summary>
/// 模型透明
/// </summary>
public void ChangeModelForTransparent()
{
MeshRenderer[] rendererArray = GetComponentsInChildren<MeshRenderer>();
if (rendererArray.Length != 0)
{
for (int i = 0; i < rendererArray.Length; i++)
{
int temp = i;
Material[] ma = rendererArray[temp].materials;
if (ma.Length != 0)
{
for (int j = 0; j < ma.Length; j++)
{
ma[j] = MaterialTransparent;
}
}
rendererArray[temp].materials = ma;
}
}
}
/// <summary>
/// 模型恢复原来的材质
/// </summary>
public void ChangeModelMaterialForOriginal()
{
MeshRenderer[] rendererArray = GetComponentsInChildren<MeshRenderer>();
if (rendererArray.Length != 0)
{
for (int i = 0; i < rendererArray.Length; i++)
{
int temp = i;
if (i < Material_List.Count)
{
rendererArray[temp].materials = Material_List[temp].materialData.ToArray();
}
}
}
}
}
[Serializable]
public class MaterialList
{
public List<Material> materialData = new List<Material>();
public MaterialList()
{
}
public MaterialList(List<Material> materials)
{
materialData = materials;
}
}
Test:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
private ModelTransparent _modelTransparent;
public GameObject TestGameObject;
public List<Button> btns;
void Start()
{
_modelTransparent = TestGameObject.GetComponent<ModelTransparent>();
btns[0].onClick.AddListener(delegate {
_modelTransparent.ChangeModelForTransparent(); });
btns[1].onClick.AddListener(delegate {
_modelTransparent.ChangeModelMaterialForOriginal(); });
}
}
版权声明
本文为[m_dlwlrma]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42862278/article/details/124348714
边栏推荐
- Redis的安装(CentOS7命令行安装)
- Building googlenet neural network based on pytorch for flower recognition
- VeraCrypt文件硬盘加密使用教程
- 对普通bean进行Autowired字段注入
- SRS 的部署
- Class loading mechanism
- R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行BoxCox变换处理(将非正态分布数据列转换为正态分布数据、不可以处理负数)、设置method参数为BoxCox
- 山大网安靶场实验平台项目—个人记录(四)
- Why is the hexadecimal printf output of C language sometimes with 0xff and sometimes not
- 【webrtc】Add x264 encoder for CEF/Chromium
猜你喜欢

【webrtc】Add x264 encoder for CEF/Chromium

STM32 Basics

深度分析数据恢复原理——那些数据可以恢复那些不可以数据恢复软件

Openharmony open source developer growth plan, looking for new open source forces that change the world!

Kubernetes entry to mastery - bare metal loadbalance 80 443 port exposure precautions

【数值预测案例】(3) LSTM 时间序列电量预测,附Tensorflow完整代码

LeetCode动态规划训练营(1~5天)

MySQL 进阶 锁 -- MySQL锁概述、MySQL锁的分类:全局锁(数据备份)、表级锁(表共享读锁、表独占写锁、元数据锁、意向锁)、行级锁(行锁、间隙锁、临键锁)

Grafana 分享带可变参数的链接

Mfcc: Mel frequency cepstrum coefficient calculation of perceived frequency and actual frequency conversion
随机推荐
图书管理数据库系统设计
nc基础用法1
Go modules daily use
如何做产品创新?——产品创新方法论探索一
【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码
Devops integration - environment variables and building tools of Jenkins service
Kubernetes入门到精通-在 Kubernetes 上安装 OpenELB
Mysql database - single table query (I)
数据库查询 - 选课系统
MySQL 进阶 锁 -- MySQL锁概述、MySQL锁的分类:全局锁(数据备份)、表级锁(表共享读锁、表独占写锁、元数据锁、意向锁)、行级锁(行锁、间隙锁、临键锁)
Mysql database - connection query
Design of library management database system
nc基础用法3
How about Bohai futures. Is it safe to open futures accounts?
[numerical prediction case] (3) LSTM time series electricity quantity prediction, with tensorflow complete code attached
微信中金财富高端专区安全吗,证券如何开户呢
Speex Wiener filter and rewriting of hypergeometric distribution
VeraCrypt文件硬盘加密使用教程
Kubernetes entry to mastery - bare metal loadbalance 80 443 port exposure precautions
MySQL数据库 - 连接查询