场景内主角和相机之间物体 透明化

2015-9-29 雨辰 Unity3D

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class MyCamera : MonoBehaviour
{
    ////本次碰撞到的物体
    private List<Renderer> colliderObject;
 
    //得到主人公
    public GameObject Target;
    void Start()
    {
        colliderObject = new List<Renderer>();
 
    }
    void Update()
    {
        //绘制射线为红色。方便调试
        Debug.DrawLine(Target.transform.position, transform.position, Color.red);
        RaycastHit[] hit;
 
        hit = Physics.RaycastAll(Target.transform.position, transform.position);
        if (hit.Length > 0)
        {
 
            for (int i = 0; i < hit.Length; i++)
            {
                //Debug.LogWarning("有碰撞" + hit[i].collider.gameObject.name);
                Renderer obj = hit[i].collider.gameObject.GetComponent<Renderer>();
                colliderObject.Add(obj);
                SetMaterialsColor(obj, 0.5f);
            }
            
 
        }//还原
        else
        {
 
            for (int i = 0; i < colliderObject.Count; i++)
            {
                Renderer obj = colliderObject[i];
                SetMaterialsColor(obj, 1f);
            }
        }
 
    }
    /// <summary>
    /// 修改遮挡物体所有材质
    /// </summary>
    /// <param name="_renderer">材质</param>
    /// <param name="Transpa">透明度</param>
    private void SetMaterialsColor(Renderer _renderer, float Transpa)
    {
        //换shader或者修改材质
 
        //获取当前物体材质球数量
        int materialsNumber = _renderer.sharedMaterials.Length;
        for (int i = 0; i < materialsNumber; i++)
        {
            
            //获取当前材质球颜色
            Color color = _renderer.materials[i].color;
 
            //设置透明度  0-1;  0 = 完全透明
            color.a = Transpa;
 
            //置当前材质球颜色
            _renderer.materials[i].SetColor("_Color", color);
        }
 
    }
}

标签: Unity3D-Shader Unity3D

发表评论:

雨辰 joyimp|@2011-2018 京ICP备16030765号