当前位置:网站首页>Change the material of unity model as a whole
Change the material of unity model as a whole
2022-04-23 20:10:00 【m_ dlwlrma】
using System;
using System.Collections.Generic;
using UnityEngine;
public class ModelTransparent : MonoBehaviour
{
/// <summary>
/// Used to save the original of the model Material
/// </summary>
[HideInInspector] public List<MaterialList> Material_List = new List<MaterialList>();
/// <summary>
/// Transparent material
/// </summary>
public Material MaterialTransparent;
private void Awake()
{
RecordOriginalMaterials();
}
/// <summary>
/// Record the original material of the model
/// </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>
/// Model transparency
/// </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>
/// Restore the original material of the model
/// </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://yzsam.com/2022/04/202204232008560976.html
边栏推荐
- Redis distributed lock
- 高效的串口循环Buffer接收处理思路及代码2
- 记录:调用mapper报空指针;<foreach>不去重的用法;
- Devops integration - environment variables and building tools of Jenkins service
- 还在用 ListView?使用 AnimatedList 让列表元素动起来
- Scrum Patterns之理解各种团队模式
- LPC1768 关于延时Delay时间与不同等级的优化对比
- MFC获取本机IP(网络通讯时用得多)
- kibana 报错 server is not ready yet 可能的原因
- R language uses timeroc package to calculate the multi time AUC value of survival data under competitive risk, uses Cox model and adds covariates, and R language uses the plotauccurve function of time
猜你喜欢
Mfcc: Mel frequency cepstrum coefficient calculation of perceived frequency and actual frequency conversion
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (VII)
深度分析数据恢复原理——那些数据可以恢复那些不可以数据恢复软件
LeetCode异或运算
Leetcode XOR operation
【webrtc】Add x264 encoder for CEF/Chromium
指针数组与数组指针的区分
MFCC: Mel频率倒谱系数计算感知频率和实际频率转换
Distinction between pointer array and array pointer
php参考手册String(7.2千字)
随机推荐
nc基础用法
How about CICC fortune? Is it safe to open an account
[H264] hevc H264 parsing and frame rate setting of the old version of libvlc
WordPress插件:WP-China-Yes解决国内访问官网慢的方法
Mysql database - basic operation of database and table (II)
Design of warehouse management database system
Is the wechat CICC wealth high-end zone safe? How to open an account for securities
Mysql database - single table query (II)
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (VII)
Unity 模型整体更改材质
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(五)
Local call feign interface message 404
NC basic usage
Kubernetes entry to mastery - bare metal loadbalance 80 443 port exposure precautions
Remote code execution in Win 11 using wpad / PAC and JScript
C6748 software simulation and hardware test - with detailed FFT hardware measurement time
PHP reference manual string (7.2000 words)
记录:调用mapper报空指针;<foreach>不去重的用法;
MySQL数据库 - 数据库和表的基本操作(二)
一个简单的(基于redisson的)分布式同步工具类封装