通过silder中value值的变化来实现物体的透明度变化,其代码如下:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class AplaManager : MonoBehaviour
{
public Slider aphaSlider;//控制透明度的slider slider的value为透明度
float apha;
public GameObject[] gameObjs;
List<Renderer> renders;//保存所有物体的renderer
// Use this for initialization
void Start()
{
renders = new List<Renderer>();
apha = aphaSlider.value;
for (int i = 0; i < gameObjs.Length; i++)
{
AddRenders(gameObjs[i]);//将开始时场景中存在的物体的render添加到链表中
}
}
// Update is called once per frame
void Update()
{
if (apha != aphaSlider.value)
{
apha = aphaSlider.value;
for (int i = 0; i < renders.Count; i++)
{
//if (renders[i] == null)
//{
// renders.Remove(renders[i]);
//}
for (int j = 0; j < renders[i].materials.Length; j++)
{
Color c = renders[i].materials[j].color;
c.a = aphaSlider.value;
if (aphaSlider.value == 1)
{
if (renders[i].materials[j] != null)
{
SetMaterialRenderingMode(renders[i].materials[j], RenderingMode.Opaque);
}
}
if (aphaSlider.value < 1)
{
if (renders[i].materials[j] != null)
{
SetMaterialRenderingMode(renders[i].materials[j], RenderingMode.Fade);
}
}
if (renders[i].materials[j] != null)
{
renders[i].materials[j].color = c;
}
}
}
}
}
public void AddRenders(GameObject g)//添加需要控制透明度的物体,脚本会遍历存储所有物体和其子物体render
{
bool active = g.active;
g.SetActive(true);
if (g.GetComponent<Renderer>())
{
renders.Add(g.GetComponent<Renderer>());
}
if (g.transform.childCount != 0)
{
for (int i = 0; i < g.transform.childCount; i++)
{
AddRenders(g.transform.GetChild(i).gameObject);
}
}
g.SetActive(active);
}
public void RemoveRenders(GameObject g) //移除需要控制透明度的物体,脚本会遍历存储所有物体和其子物体render
{
bool active = g.active;
g.SetActive(true);
if (g.GetComponent<Renderer>())
{
renders.Remove(g.GetComponent<Renderer>());
}
if (g.transform.childCount != 0)
{
for (int i = 0; i < g.transform.childCount; i++)
{
RemoveRenders(g.transform.GetChild(i).gameObject);
}
}
g.SetActive(active);
}
//RenderMode Change
#region
public enum RenderingMode
{
Opaque,
Cutout,
Fade,
Transparent,
}
public static void SetMaterialRenderingMode(Material material, RenderingMode renderingMode)
{
switch (renderingMode)
{
case RenderingMode.Opaque:
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = -1;
break;
case RenderingMode.Cutout:
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.EnableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 2450;
break;
case RenderingMode.Fade:
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.EnableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
break;
case RenderingMode.Transparent:
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
break;
}
}
#endregion
}
非原创,转载自:(5条消息) 通过滑动条控制场景中物体的透明度_unity制作透明度滑动条_KeeMew的博客-CSDN博客在其基础上简单修改了一下。