Guest User

Decal.cs

a guest
Mar 29th, 2021
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. [ExecuteInEditMode]
  5. public class Decal : MonoBehaviour
  6. {
  7.     public Material m_Material;
  8.     public Texture2D texture;
  9.     public Color tinting = Color.white;
  10.   //  Camera cam;
  11.     public Mesh m_CubeMesh;
  12.     MaterialPropertyBlock props;
  13.  
  14.     public void OnEnable()
  15.     {
  16.         props = new MaterialPropertyBlock();
  17.   Camera.onPreCull -= DrawWithCamera;
  18.         Camera.onPreCull += DrawWithCamera;
  19.     }
  20.  
  21. #if UNITY_EDITOR
  22.     private void DrawGizmo(bool selected)
  23.     {
  24.         var col = new Color(0.0f, 0.7f, 1f, 1.0f);
  25.         col.a = selected ? 0.3f : 0.1f;
  26.         Gizmos.color = col;
  27.         Gizmos.matrix = transform.localToWorldMatrix;
  28.         Gizmos.DrawCube(Vector3.zero, Vector3.one);
  29.         col.a = selected ? 0.5f : 0.2f;
  30.         Gizmos.color = col;
  31.         Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
  32.         Handles.matrix = transform.localToWorldMatrix;
  33.         Handles.DrawBezier(Vector3.zero, Vector3.down, Vector3.zero, Vector3.down, Color.red, null, selected ? 4f : 2f);
  34.     }
  35. #endif
  36.  
  37.  
  38. #if UNITY_EDITOR
  39.     public void OnDrawGizmos()
  40.     {
  41.         DrawGizmo(false);
  42.     }
  43.     public void OnDrawGizmosSelected()
  44.     {
  45.         DrawGizmo(true);
  46.     }
  47. #endif
  48.  
  49.  
  50.     private void OnDisable() {
  51.  
  52.         Camera.onPreCull -= DrawWithCamera;
  53.     }
  54.  
  55.     private void DrawWithCamera(Camera camera) {
  56.  
  57.         if (camera) {
  58.  
  59.             Draw(camera);
  60.         }
  61.     }
  62.  
  63.     private void Draw(Camera camera) {
  64.   // set up property block for decal texture and tinting
  65.         if (texture != null)
  66.         {
  67.             props.SetTexture("_MainTex", texture);
  68.         }
  69.         props.SetColor("_Tint", tinting);
  70.         // draw the decal on the main camera
  71.         Graphics.DrawMesh(m_CubeMesh, transform.localToWorldMatrix, m_Material, 0, camera, 0, props, false, true, false);        
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment