using System.Collections; using System.Collections.Generic; using UnityEngine; [ExecuteAlways] [RequireComponent(typeof(RectTransform))] public class Datumline : MonoBehaviour { [System.NonSerialized] RectTransform m_Rect; public RectTransform rectTransform { get { return m_Rect ?? (m_Rect = GetComponent()); } } [System.NonSerialized] Canvas m_Canva; public Canvas canvas { get { return m_Canva ?? (m_Canva = GetComponentInParent()); } } public List datumlines { get; private set; } private List horizonlines = new List(); private List verticallins = new List(); private int m_SelectDatumline = 0; public int SelectDatumline { get { return m_SelectDatumline; } } private void Awake() { datumlines = new List(); } private void OnEnable() { if (datumlines == null) { datumlines = new List(); } if (datumlines.Count == 0) { Add(); } } private void OnDisable() { } public void Add() { var _datumline = new DatumlineData(); _datumline.type = HorizonOrVertical.Vertical; _datumline.from = new Vector3(0, rectTransform.rect.height / 2, 0); _datumline.to = new Vector3(0, -rectTransform.rect.height / 2, 0); _datumline.color = Color.white; datumlines.Add(_datumline); } public DatumlineData Add(DatumlineData _line) { var _datumline = new DatumlineData(); _datumline.type = _line.type; _datumline.from = _line.from; _datumline.to = _line.to; _datumline.color = _line.color; datumlines.Add(_datumline); return _datumline; } public void ChangeVector(int _index) { var _datumline = datumlines[_index]; _datumline.type = 1 - _datumline.type; if (_datumline.type == HorizonOrVertical.Horizon) { _datumline.from = new Vector3(-rectTransform.rect.width / 2, _datumline.from.x, 0); _datumline.to = new Vector3(rectTransform.rect.width / 2, _datumline.from.x, 0); } else { _datumline.from = new Vector3(_datumline.from.y, rectTransform.rect.height / 2, 0); _datumline.to = new Vector3(_datumline.from.y, -rectTransform.rect.height / 2, 0); } } public void Remove(int _index) { if (datumlines.Count == 1) { return; } datumlines.RemoveAt(_index); } public Vector3 ToWorldValue(Vector3 _pos) { return _pos * canvas.transform.localScale.x; } public float ToScenceValue(float _value) { return _value / canvas.transform.localScale.x; } public void SetSelect(int _index) { if (_index < datumlines.Count) { m_SelectDatumline = _index; } } public List GetLines(HorizonOrVertical _type) { var _list = datumlines.FindAll((x) => { return x.type == _type; }); if (_list != null) { switch (_type) { case HorizonOrVertical.Horizon: horizonlines.Clear(); horizonlines.AddRange(_list); horizonlines.Sort(Compare); break; case HorizonOrVertical.Vertical: verticallins.Clear(); verticallins.AddRange(_list); verticallins.Sort(Compare); break; } } return _type == HorizonOrVertical.Horizon ? horizonlines : verticallins; } private int Compare(DatumlineData x, DatumlineData y) { return x.type == HorizonOrVertical.Horizon ? x.from.y.CompareTo(y.from.y) : x.from.x.CompareTo(y.from.x); } public class DatumlineData { public Vector3 from { get; set; } public Vector3 to { get; set; } public HorizonOrVertical type { get; set; } public Color color { get; set; } } public enum HorizonOrVertical { Horizon, Vertical, } }