当前位置:网站首页>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
边栏推荐
- R语言ggplot2可视化分面图(facet_wrap)、使用lineheight参数自定义设置分面图标签栏(灰色标签栏)的高度
- 记录:调用mapper报空指针;<foreach>不去重的用法;
- STM32 Basics
- 如何在BNB鏈上創建BEP-20通證
- SRS 的部署
- Main differences between go and PHP
- [numerical prediction case] (3) LSTM time series electricity quantity prediction, with tensorflow complete code attached
- Record: call mapper to report null pointer Foreach > the usage of not removing repetition;
- Video understanding
- Kubernetes入门到精通-裸机LoadBalence 80 443 端口暴露注意事项
猜你喜欢

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

MFCC: Mel频率倒谱系数计算感知频率和实际频率转换

CVPR 2022 | QueryDet:使用级联稀疏query加速高分辨率下的小目标检测

C6748 software simulation and hardware test - with detailed FFT hardware measurement time

Comment créer un pass BEP - 20 sur la chaîne BNB

山大网安靶场实验平台项目-个人记录(五)

Unity创建超写实三维场景的一般步骤

Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (6)

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

MySQL advanced lock - overview of MySQL locks and classification of MySQL locks: global lock (data backup), table level lock (table shared read lock, table exclusive write lock, metadata lock and inte
随机推荐
Mysql database - single table query (I)
VeraCrypt文件硬盘加密使用教程
Kubernetes introduction to mastery - ktconnect (full name: kubernetes toolkit connect) is a small tool based on kubernetes environment to improve the efficiency of local test joint debugging.
Redis的安装(CentOS7命令行安装)
一个简单的(基于redisson的)分布式同步工具类封装
山大网安靶场实验平台项目—个人记录(四)
MySQL数据库 - 单表查询(二)
Deep learning -- Summary of Feature Engineering
【数值预测案例】(3) LSTM 时间序列电量预测,附Tensorflow完整代码
@MapperScan与@Mapper
Design of warehouse management database system
Go recursively loops through folders
Remote code execution in Win 11 using wpad / PAC and JScript 1
MFC obtains local IP (used more in network communication)
Inject Autowired fields into ordinary beans
filebeat、logstash配置安装
Distinction between pointer array and array pointer
如何做产品创新?——产品创新方法论探索一
@Mapperscan and @ mapper
Class loading process of JVM