-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBetterContentFitter.cs
More file actions
79 lines (68 loc) · 1.73 KB
/
BetterContentFitter.cs
File metadata and controls
79 lines (68 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using MEC;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class BetterContentFitter : ContentSizeFitter, ILayoutChild
{
public Action OnChange { get; set; }
[System.NonSerialized] private RectTransform m_Rect;
private RectTransform rectTransform
{
get
{
if (m_Rect == null)
m_Rect = GetComponent<RectTransform>();
return m_Rect;
}
}
protected override void OnEnable()
{
base.OnEnable();
SetDirty();
if (!Application.isPlaying) return;
var listLayout = GetComponentsInChildren<ILayoutChild>().ToList();
listLayout.Remove(this);
foreach (var item in listLayout)
{
item.OnChange += UpdateSelf;
}
}
protected override void OnDisable()
{
base.OnDisable();
if (!Application.isPlaying) return;
var listLayout = GetComponentsInChildren<ILayoutChild>().ToList();
listLayout.Remove(this);
foreach (var item in listLayout)
{
item.OnChange -= UpdateSelf;
}
}
public void UpdateSelf()
{
Timing.RunCoroutine(LateDirty());
}
public override void SetLayoutHorizontal()
{
base.SetLayoutHorizontal();
OnChange?.Invoke();
}
public override void SetLayoutVertical()
{
base.SetLayoutVertical();
OnChange?.Invoke();
}
IEnumerator<float> LateDirty()
{
if(!Application.isPlaying) yield break;
yield return Timing.WaitForOneFrame;
SetDirty();
}
}
public interface ILayoutChild
{
public System.Action OnChange { get; set; }
}