当前位置:网站首页>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
边栏推荐
- 微信中金财富高端专区安全吗,证券如何开户呢
- Speex Wiener filter and rewriting of hypergeometric distribution
- A simple (redisson based) distributed synchronization tool class encapsulation
- Kubernetes entry to mastery - bare metal loadbalance 80 443 port exposure precautions
- 高效的串口循环Buffer接收处理思路及代码2
- Unity创建超写实三维场景的一般步骤
- Why is the hexadecimal printf output of C language sometimes with 0xff and sometimes not
- VeraCrypt文件硬盘加密使用教程
- Go three ways to copy files
- Openharmony open source developer growth plan, looking for new open source forces that change the world!
猜你喜欢
指针数组与数组指针的区分
Distinction between pointer array and array pointer
MySQL lock
STM32 Basics
Compact CUDA tutorial - CUDA driver API
php参考手册String(7.2千字)
网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
Building googlenet neural network based on pytorch for flower recognition
山大网安靶场实验平台项目-个人记录(五)
Openharmony open source developer growth plan, looking for new open source forces that change the world!
随机推荐
Software College of Shandong University Project Training - Innovation Training - network security shooting range experimental platform (8)
STM32 Basics
nc基础用法1
Speex维纳滤波与超几何分布的改写
AQS learning
Class loading mechanism
Introduction to electron tutorial 3 - process communication
Record: call mapper to report null pointer Foreach > the usage of not removing repetition;
SRS deployment
【webrtc】Add x264 encoder for CEF/Chromium
NC basic usage
Design of library management database system
SRS 的部署
精简CUDA教程——CUDA Driver API
nc基础用法
How about Bohai futures. Is it safe to open futures accounts?
Building googlenet neural network based on pytorch for flower recognition
Class loading process of JVM
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码