Advertisement
Guest User

Decal.cs

a guest
Aug 21st, 2023
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 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.  
  9.     [Header("PropertyBlock Settings")]
  10.     public Texture2D texture;
  11.     public string textureName = "_MainTex";
  12.  
  13.  
  14.     [ColorUsage(true, true)]
  15.     public Color color = Color.white;
  16.     public string colorName = "_Tint";
  17.  
  18.     // mesh to draw with
  19.     Mesh m_CubeMesh;
  20.     // extra settings
  21.     MaterialPropertyBlock props;
  22.     // render settings
  23.     RenderParams m_renderParams;
  24.  
  25.     void SetPropertyBlockSettings()
  26.     {
  27.         if (props == null)
  28.         {
  29.             props = new MaterialPropertyBlock();
  30.         }
  31.         if (texture)
  32.         {
  33.             props.SetTexture(textureName, texture);
  34.         }
  35.         props.SetColor(colorName, color);
  36.  
  37.     }
  38.  
  39.     private void OnValidate()
  40.     {
  41.         SetPropertyBlockSettings();
  42.     }
  43.     public void OnEnable()
  44.     {
  45.         SetPropertyBlockSettings();
  46.         m_CubeMesh = Resources.GetBuiltinResource<Mesh>("Cube.fbx");
  47.         m_renderParams = new(m_Material) { matProps = props, receiveShadows = false };
  48.     }
  49.  
  50. #if UNITY_EDITOR
  51.     private void DrawGizmo(bool selected)
  52.     {
  53.         // draw a cube dizmo and a line in the direction of the decal
  54.         var col = new Color(0.0f, 0.7f, 1f, 1.0f);
  55.         col.a = selected ? 0.3f : 0.1f;
  56.         Gizmos.color = col;
  57.         Gizmos.matrix = transform.localToWorldMatrix;
  58.         Gizmos.DrawCube(Vector3.zero, Vector3.one);
  59.         col.a = selected ? 0.5f : 0.05f;
  60.         Gizmos.color = col;
  61.         Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
  62.         Handles.matrix = transform.localToWorldMatrix;
  63.         Handles.DrawBezier(Vector3.zero, Vector3.down, Vector3.zero, Vector3.down, Color.red, null, selected ? 4f : 2f);
  64.     }
  65. #endif
  66.  
  67.  
  68. #if UNITY_EDITOR
  69.     public void OnDrawGizmos()
  70.     {
  71.         DrawGizmo(false);
  72.     }
  73.     public void OnDrawGizmosSelected()
  74.     {
  75.         DrawGizmo(true);
  76.     }
  77. #endif
  78.  
  79.     void Update()
  80.     {
  81.         // draw the cube using the render parameters
  82.         Graphics.RenderMesh(m_renderParams, m_CubeMesh, 0, transform.localToWorldMatrix);
  83.     }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement