HeaderDraw / HeaderLeftDrawAllow you to manually draw items on the component headers
Parameters:
string groupBy = null: group the header items virtically by this name. Ifnull, it will not share space with anyone.
Signature:
The method must have this signaure:
HeaderUsed FuncName(HeaderArea headerArea)
SaintsField.ComponentHeader.HeaderArea has the following fields:
/// <summary> /// Rect.y for drwaing /// </summary> public readonly float Y; /// <summary> /// Rect.height for drawing /// </summary> public readonly float Height; /// <summary> /// the x value where the title (component name) started /// </summary> public readonly float TitleStartX; /// <summary> /// the x value where the title (component name) ended /// </summary> public readonly float TitleEndX; /// <summary> /// the x value where the empty space start. You may want to start draw here /// </summary> public readonly float SpaceStartX; /// <summary> /// the x value where the empty space ends. When drawing from right, you need to backward drawing starts here /// </summary> public readonly float SpaceEndX; /// <summary> /// The x drawing position. It's recommend to use this as your start drawing point, as SaintsField will /// change this value accordingly everytime an item is drawn. /// </summary> public readonly float GroupStartX; /// <summary> /// When using `GroupBy`, you can see the vertical rect which already used by others in this group /// </summary> public readonly IReadOnlyList<Rect> GroupUsedRect; public float TitleWidth => TitleEndX - TitleStartX; public float SpaceWidth => SpaceEndX - SpaceStartX; /// <summary> /// A quick way to make a rect /// </summary> /// <param name="x">where to start</param> /// <param name="width">width of the rect</param> /// <returns>rect space you want to draw</returns> public Rect MakeXWidthRect(float x, float width) => new Rect(x, Y, width, Height);
After you draw your item, use return new HeaderUsed(useRect); to tell the space you've used.
A simple example of progress bar
using SaintsField; using SaintsField.ComponentHeader; #if UNITY_EDITOR [HeaderDraw] private HeaderUsed HeaderDrawRight1G1(HeaderArea headerArea) { // this is drawing from right to left, so we need to backward the rect space Rect useRect = new Rect(headerArea.MakeXWidthRect(headerArea.GroupStartX - 100, 100)) { y = headerArea.Y + 2, height = headerArea.Height - 4, }; Rect progressRect = new Rect(useRect) { width = range1 * useRect.width, }; EditorGUI.DrawRect(useRect, Color.gray); EditorGUI.DrawRect(progressRect, Color.red); return new HeaderUsed(useRect); } #endif [Range(0f, 1f)] public float range1;
A more complex example:
using SaintsField; using SaintsField.ComponentHeader; #if UNITY_EDITOR private bool _started; [HeaderGhostButton("<icon=play.png/>")] private IEnumerator BeforeBotton() { _started = true; while (_started) { range1 = (range1 + 0.01f) % 1; range2 = (range2 + 0.03f) % 1; range3 = (range3 + 0.02f) % 1; yield return null; } } [HeaderDraw("group1")] private HeaderUsed HeaderDrawRight1G1(HeaderArea headerArea) { Rect useRect = new Rect(headerArea.MakeXWidthRect(headerArea.GroupStartX - 40, 40)) { height = headerArea.Height / 3, }; Rect progressRect = new Rect(useRect) { width = range1 * useRect.width, }; EditorGUI.DrawRect(useRect, Color.gray); EditorGUI.DrawRect(progressRect, Color.red); return new HeaderUsed(useRect); } [HeaderDraw("group1")] private HeaderUsed HeaderDrawRight1G2(HeaderArea headerArea) { Rect useRect = new Rect(headerArea.MakeXWidthRect(headerArea.GroupStartX - 40, 40)) { y = headerArea.Y + headerArea.Height / 3, height = headerArea.Height / 3, }; Rect progressRect = new Rect(useRect) { width = range2 * useRect.width, }; EditorGUI.DrawRect(useRect, Color.gray); EditorGUI.DrawRect(progressRect, Color.yellow); return new HeaderUsed(useRect); } [HeaderDraw("group1")] private HeaderUsed HeaderDrawRight1G3(HeaderArea headerArea) { Rect useRect = new Rect(headerArea.MakeXWidthRect(headerArea.GroupStartX - 40, 40)) { y = headerArea.Y + headerArea.Height / 3 * 2, height = headerArea.Height / 3, }; Rect progressRect = new Rect(useRect) { width = range3 * useRect.width, }; EditorGUI.DrawRect(useRect, Color.gray); EditorGUI.DrawRect(progressRect, Color.cyan); return new HeaderUsed(useRect); } [HeaderGhostButton("<icon=pause.png/>")] private void AfterBotton() { _started = false; } #endif [Range(0f, 1f)] public float range1; [Range(0f, 1f)] public float range2; [Range(0f, 1f)] public float range3;