| using UnityEngine; | 
| using System.Collections; | 
|   | 
|   | 
| [ExecuteAlways] | 
| [RequireComponent(typeof(Camera))] | 
| //提供一个后处理的基类,主要功能在于直接通过Inspector面板拖入shader,生成shader对应的材质 | 
| public class PostEffectsBaseEx : MonoBehaviour | 
| { | 
|   | 
|     //Inspector面板上直接拖入 | 
|     public Shader shader = null; | 
|     private Material _material = null; | 
|     public Material material | 
|     { | 
|         get | 
|         { | 
|             if (_material == null) | 
|                 _material = GenerateMaterial(shader); | 
|             return _material; | 
|         } | 
|     } | 
|   | 
|     //根据shader创建用于屏幕特效的材质 | 
|     protected Material GenerateMaterial(Shader shader) | 
|     { | 
|         if (shader == null) | 
|             return null; | 
|         //需要判断shader是否支持 | 
|         if (shader.isSupported == false) | 
|             return null; | 
|         Material material = new Material(shader); | 
|         material.hideFlags = HideFlags.DontSave; | 
|         if (material) | 
|             return material; | 
|         return null; | 
|     } | 
|   | 
|     private void OnDisable() | 
|     { | 
|         // remove material | 
|         if (_material) | 
|         { | 
| #if UNITY_EDITOR | 
|             DestroyImmediate(_material); | 
| #else | 
|             Destroy(_material); | 
| #endif | 
|             _material = null; | 
|         } | 
|     } | 
| } |