源文件:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ModelTransparencyHelper : MonoBehaviour
{
/*
/// <summary>
/// 从不透明到透明的过渡时间
/// </summary>
public float TransitonTime=0.3f;*/
/// <summary>
/// 透明的程度
/// </summary>
public float TransparenceValue = 0.01f;
/// <summary>
/// 原材质球组的主颜色值
/// </summary>
private List<Color> m_colors=new List<Color>();
/// <summary>
/// 原材质球组的引用
/// </summary>
private List<Material> m_Materials=new List<Material>();
void Start () {
try
{
if (m_Materials==null|| m_colors == null)
{
m_Materials=new List<Material>();
m_colors=new List<Color>();
}
if (GetComponent<MeshRenderer>() != null)
{
m_Materials = GetComponent<MeshRenderer>().materials.ToList();
for (int i = 0; i < m_Materials.Count; i++)
{
m_colors.Add(m_Materials[i].color);
}
}
//测试使用
ToTransparence();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
/// <summary>
/// 从不透明到透明
/// </summary>
/// <returns></returns>
public bool ToTransparence()
{
try
{
if (m_Materials.Count<1)
{
return false;
}
for (int i = 0; i < m_Materials.Count; i++)
{
m_Materials[i].SetFloat("_Mode", 3);
m_Materials[i].SetInt("_SrcBlend",(int)UnityEngine.Rendering.BlendMode.One);
m_Materials[i].SetInt("_DstBlend",(int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m_Materials[i].SetInt("_ZWrite",0);
m_Materials[i].DisableKeyword("_ALPHATEST_ON");
m_Materials[i].DisableKeyword("_ALPHABLEND_ON");
m_Materials[i].EnableKeyword("_ALPHAPREMULTIPLY_ON");
m_Materials[i].renderQueue = 500;
m_Materials[i].color = new Color(m_colors[i].r,m_colors[i].g,m_colors[i].b,TransparenceValue);
}
print("try ToTransparence!");
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
/// <summary>
/// 在物体半透明时控制物体透明度
/// </summary>
public bool ControlTran(float t)
{
try
{
if (m_Materials.Count < 1)
{
return false;
}
if (t > 0.05 && t < 0.1)
{
}
else
{
for (int i = 0; i < m_Materials.Count; i++)
{
m_Materials[i].SetFloat("_Mode", 3);
m_Materials[i].SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
m_Materials[i].SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m_Materials[i].SetInt("_ZWrite", 0);
m_Materials[i].DisableKeyword("_ALPHATEST_ON");
m_Materials[i].DisableKeyword("_ALPHABLEND_ON");
m_Materials[i].EnableKeyword("_ALPHAPREMULTIPLY_ON");
m_Materials[i].renderQueue = 500;
m_Materials[i].color = new Color(m_colors[i].r, m_colors[i].g, m_colors[i].b, t);
}
}
// print("try ToTransparence!");
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
/// <summary>
/// 从透明到不透明
/// </summary>
/// <returns></returns>
public bool ToOpaque()
{
try
{
if (m_Materials.Count < 1)
{
return false;
}
for (int i = 0; i < m_Materials.Count; i++)
{
m_Materials[i].SetFloat("_Mode", 0);
m_Materials[i].SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
m_Materials[i].SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
m_Materials[i].SetInt("_ZWrite", 1);
m_Materials[i].DisableKeyword("_ALPHATEST_ON");
m_Materials[i].DisableKeyword("_ALPHABLEND_ON");
m_Materials[i].EnableKeyword("_ALPHAPREMULTIPLY_ON");
m_Materials[i].renderQueue = -1;
m_Materials[i].color = new Color(m_colors[i].r, m_colors[i].g, m_colors[i].b, m_colors[i].a);
}
print("try ToOpaque!");
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
private void Update()
{
//if (Input.GetMouseButtonDown(0))
//{
// ToTransparence();
//}
}
}
using System;
using System.Collectio