using UnityEngine; using UnityEngine.UI; public class LineImage : RawImage { public enum LineDirection { Vertical, Horizontal } public LineDirection lineDirection = LineDirection.Vertical; protected override void Awake() { base.Awake(); Texture2D tex = new Texture2D(3, 3, TextureFormat.RGBA32, false); Color c0 = new Color(0.0f, 0.0f, 0.0f, 0.0f); Color c1 = new Color(1.0f, 1.0f, 1.0f, 1.0f); tex.SetPixel(0, 0, c1); tex.SetPixel(1, 0, c1); tex.SetPixel(2, 0, c1); tex.SetPixel(0, 1, c1); tex.SetPixel(1, 1, c1); tex.SetPixel(2, 1, c1); tex.SetPixel(0, 2, c1); tex.SetPixel(1, 2, c1); tex.SetPixel(2, 2, c1); tex.wrapMode = TextureWrapMode.Repeat; tex.filterMode = FilterMode.Point; tex.Apply(); this.texture = tex; } protected override void Start() { base.Start(); RecomputeSize(); } protected override void OnRectTransformDimensionsChange() { RecomputeSize(); base.OnRectTransformDimensionsChange(); } void RecomputeSize() { #if UNITY_EDITOR if (!UnityEditor.EditorApplication.isPlaying) return; #endif float scaleFactor = 1.0f / transform.lossyScale.x; //Debug.Log($"SCREEN SIZE CHANGED - scaleFactor={scaleFactor} "); Vector2 sd = rectTransform.sizeDelta; if (lineDirection == LineDirection.Horizontal) { sd.y = scaleFactor; } else { sd.x = scaleFactor; } rectTransform.sizeDelta = sd; } }