using System; using ProCore.Decals; using UnityEngine; [ExecuteInEditMode] [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] public class qd_Decal : MonoBehaviour { [SerializeField] [HideInInspector] private Texture2D _texture; [HideInInspector] [SerializeField] private Rect _rect; [SerializeField] [HideInInspector] private float _scale; public Texture2D texture { get { return _texture; } } private void Awake() { Verify(); } public void SetScale(float scale) { _scale = scale; } public void SetTexture(Texture2D tex) { _texture = tex; } public void SetUVRect(Rect r) { _rect = r; Vector2[] uv = new Vector2[4] { new Vector2(_rect.x + _rect.width, _rect.y), new Vector2(_rect.x, _rect.y), new Vector2(_rect.x + _rect.width, _rect.y + _rect.height), new Vector2(_rect.x, _rect.y + _rect.height) }; GetComponent().sharedMesh.uv = uv; } public void FreezeTransform() { Vector3 localScale = base.transform.localScale; Mesh sharedMesh = base.transform.GetComponent().sharedMesh; Vector3[] vertices = sharedMesh.vertices; for (int i = 0; i < vertices.Length; i++) { vertices[i] = Vector3.Scale(vertices[i], localScale); } sharedMesh.vertices = vertices; base.transform.localScale = Vector3.one; } public void Verify() { Material material = GetComponent().sharedMaterial; if (material == null) { GameObject[] array = qdUtil.FindDecalsWithTexture(_texture); array = Array.FindAll(array, (GameObject x) => x.GetComponent().sharedMaterial != null); if (array == null || array.Length < 1) { material = new Material(Shader.Find("Transparent/Diffuse")); material.mainTexture = _texture; } else { material = array[0].GetComponent().sharedMaterial; } GetComponent().sharedMaterial = material; } if (GetComponent().sharedMesh == null) { GetComponent().sharedMesh = qd_Mesh.DecalMesh("DecalMesh" + GetInstanceID(), material, _rect, _scale); } } }