Skip to content
This repository was archived by the owner on Jan 4, 2022. It is now read-only.

Commit ddfae41

Browse files
authored
Merge pull request #52 from InitialPrefabs/develop
Develop - Disabling (0.2.0)
2 parents 3e0c32c + ffb8c8c commit ddfae41

38 files changed

+715
-247
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Change Log
22

3+
## 0.2.0
4+
### Added
5+
* Added `ChildrenActiveMetadata` component to store the state of the visibility of the entity
6+
* Added the `EnableRenderingTag` to mark entities as newly rendering
7+
* Added the `ToggleVisibilitySystem` to manage the state of the entities' visibility
8+
* Added a `ReactiveSubgroupToggleSystem` to clean up entities when they are disabled/enabled.
9+
10+
### Changed
11+
* Changed the `UpdateLocalMeshDataSystem` to react to the state of the entity. See the wiki page about
12+
[closing](Wiki/Close.md) for more details.
13+
* Changed the `CanvasConversionSystem` to populate and add the `ChildrenActiveMetadataSystem`.
14+
* Changed the `BuildImageVertexDataSystem` and `BuildTextVertexDataSystem` to include disabled entities.
15+
* Changed the `RenderCommand` to be a managed `IComponentData` instead.
16+
317
## 0.1.3
418
### Fixed
519
* Fixed issue regarding render texture cameras

Controls/Authoring/ButtonTypeAuthoring.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using UnityEngine;
33

44
namespace UGUIDots.Controls.Authoring {
5+
56
public class ButtonTypeAuthoring : MonoBehaviour, IConvertGameObjectToEntity {
67

78
public ClickType Type = ClickType.ReleaseUp;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Unity.Entities;
2+
using UnityEngine;
3+
4+
namespace UGUIDots.Controls.Authoring {
5+
6+
public enum ButtonVisibilityType {
7+
Close,
8+
Show,
9+
Toggle
10+
}
11+
12+
public class CloseButtonAuthoring : MonoBehaviour, IConvertGameObjectToEntity {
13+
14+
public ButtonVisibilityType Type = ButtonVisibilityType.Toggle;
15+
public GameObject[] Targets;
16+
17+
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) {
18+
#if UNITY_EDITOR
19+
if (Targets.Length == 0) {
20+
throw new System.InvalidOperationException("Cannot send a message to a GameObject that is null!");
21+
}
22+
#endif
23+
24+
// Create the message which will try to close something
25+
var msg = dstManager.CreateEntity();
26+
27+
switch (Type) {
28+
case ButtonVisibilityType.Toggle:
29+
dstManager.AddComponentData(msg, new ToggleButtonType { });
30+
break;
31+
case ButtonVisibilityType.Show:
32+
dstManager.AddComponentData(msg, new ShowButtonType { });
33+
break;
34+
case ButtonVisibilityType.Close:
35+
dstManager.AddComponentData(msg, new CloseButtonType { });
36+
break;
37+
default:
38+
break;
39+
}
40+
41+
var buffer = dstManager.AddBuffer<CloseTarget>(msg);
42+
43+
foreach (var target in Targets) {
44+
buffer.Add(new CloseTarget { Value = conversionSystem.GetPrimaryEntity(target) });
45+
}
46+
47+
dstManager.AddComponentData(entity, new ButtonMessageFramePayload { Value = msg });
48+
49+
#if UNITY_EDITOR
50+
dstManager.SetName(msg, "Close Target Msg");
51+
#endif
52+
}
53+
}
54+
}

Core/Systems/ReactiveDisableHierarchySystem.cs.meta renamed to Controls/Authoring/CloseButtonAuthoring.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Controls/Components/ButtonControlComponents.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55

66
namespace UGUIDots.Controls {
77

8+
/// <summary>
9+
/// The entity which is targetted to be disabled.
10+
/// </summary>
11+
public struct CloseTarget : IBufferElementData {
12+
public Entity Value;
13+
}
14+
815
/// <summary>
916
/// The type of the button behaviour.
1017
/// </summary>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using UGUIDots.Render;
2+
using UGUIDots.Transforms;
3+
using Unity.Burst;
4+
using Unity.Collections;
5+
using Unity.Collections.LowLevel.Unsafe;
6+
using Unity.Entities;
7+
using Unity.Transforms;
8+
9+
namespace UGUIDots.Controls.Messaging.Systems {
10+
11+
[UpdateInGroup(typeof(MessagingUpdateGroup))]
12+
public class ToggleVisibilitySystem : SystemBase {
13+
14+
[BurstCompile]
15+
private struct ToggleJob {
16+
17+
public EntityCommandBuffer CmdBuffer;
18+
19+
[ReadOnly]
20+
public ComponentDataFromEntity<Disabled> Disabled;
21+
22+
[ReadOnly]
23+
public ComponentDataFromEntity<Parent> Parents;
24+
25+
[ReadOnly]
26+
public BufferFromEntity<Child> Children;
27+
28+
[ReadOnly]
29+
public ComponentDataFromEntity<ShowButtonType> ShowButtons;
30+
31+
[ReadOnly]
32+
public ComponentDataFromEntity<ToggleButtonType> ToggleButtons;
33+
34+
[ReadOnly]
35+
public ComponentDataFromEntity<CloseButtonType> CloseButtons;
36+
37+
[ReadOnly]
38+
public BufferFromEntity<LocalVertexData> LocalVertices;
39+
40+
public ComponentDataFromEntity<ChildrenActiveMetadata> Metadata;
41+
42+
public void Execute(Entity msgEntity, DynamicBuffer<CloseTarget> b0) {
43+
var buffer = b0.AsNativeArray();
44+
45+
for (int i = 0; i < buffer.Length; i++) {
46+
var targetEntity = buffer[i].Value;
47+
48+
// Check the metadata of the entity and update its state
49+
var root = HierarchyUtils.GetRoot(targetEntity, Parents);
50+
var activeStates = Metadata[root];
51+
52+
if (activeStates.Value.TryGetValue(targetEntity, out bool isActive)) {
53+
isActive = !isActive;
54+
activeStates.Value[targetEntity] = isActive;
55+
}
56+
57+
if (isActive && Disabled.Exists(targetEntity) &&
58+
(ShowButtons.Exists(msgEntity) || ToggleButtons.Exists(msgEntity))) {
59+
60+
CmdBuffer.RemoveComponent<Disabled>(targetEntity);
61+
CmdBuffer.AddComponent<EnableRenderingTag>(targetEntity);
62+
63+
if (LocalVertices.Exists(targetEntity)) {
64+
CmdBuffer.AddComponent<UpdateVertexColorTag>(targetEntity);
65+
}
66+
RecurseChildrenAndEnable(targetEntity, ref activeStates.Value);
67+
}
68+
69+
if (!isActive && (CloseButtons.Exists(msgEntity) || ToggleButtons.Exists(msgEntity))) {
70+
CmdBuffer.AddComponent<Disabled>(targetEntity);
71+
72+
if (LocalVertices.Exists(targetEntity)) {
73+
CmdBuffer.AddComponent<UpdateVertexColorTag>(targetEntity);
74+
}
75+
76+
RecurseChildrenAndDisabled(targetEntity);
77+
}
78+
}
79+
}
80+
81+
82+
private void RecurseChildrenAndDisabled(Entity entity) {
83+
if (!Children.Exists(entity)) {
84+
return;
85+
}
86+
87+
var children = Children[entity].AsNativeArray();
88+
89+
for (int i = 0; i < children.Length; i++) {
90+
var child = children[i].Value;
91+
92+
CmdBuffer.AddComponent<Disabled>(child);
93+
CmdBuffer.AddComponent<UpdateVertexColorTag>(child);
94+
RecurseChildrenAndDisabled(child);
95+
}
96+
}
97+
98+
private void RecurseChildrenAndEnable(Entity entity, ref UnsafeHashMap<Entity, bool> metadata) {
99+
if (!Children.Exists(entity)) {
100+
return;
101+
}
102+
103+
var children = Children[entity].AsNativeArray();
104+
105+
for (int i = 0; i < children.Length; i++) {
106+
var child = children[i].Value;
107+
108+
CmdBuffer.AddComponent<EnableRenderingTag>(child);
109+
CmdBuffer.AddComponent<UpdateVertexColorTag>(child);
110+
111+
metadata.TryGetValue(child, out bool isActive);
112+
113+
if (isActive) {
114+
CmdBuffer.RemoveComponent<Disabled>(child);
115+
}
116+
117+
RecurseChildrenAndEnable(child, ref metadata);
118+
}
119+
}
120+
}
121+
122+
private EntityCommandBufferSystem cmdBufferSystem;
123+
124+
protected override void OnCreate() {
125+
cmdBufferSystem = World.GetOrCreateSystem<BeginPresentationEntityCommandBufferSystem>();
126+
}
127+
128+
protected override void OnUpdate() {
129+
var job = new ToggleJob {
130+
CmdBuffer = cmdBufferSystem.CreateCommandBuffer(),
131+
Parents = GetComponentDataFromEntity<Parent>(true),
132+
Metadata = GetComponentDataFromEntity<ChildrenActiveMetadata>(false),
133+
Children = GetBufferFromEntity<Child>(true),
134+
Disabled = GetComponentDataFromEntity<Disabled>(true),
135+
ShowButtons = GetComponentDataFromEntity<ShowButtonType>(true),
136+
CloseButtons = GetComponentDataFromEntity<CloseButtonType>(true),
137+
ToggleButtons = GetComponentDataFromEntity<ToggleButtonType>(true),
138+
LocalVertices = GetBufferFromEntity<LocalVertexData>(true)
139+
};
140+
141+
Dependency = Entities.WithAll<ButtonMessageRequest>().
142+
WithAny<ShowButtonType, CloseButtonType, ToggleButtonType>().
143+
ForEach((Entity entity, DynamicBuffer<CloseTarget> b0) => {
144+
145+
job.Execute(entity, b0);
146+
}).Schedule(Dependency);
147+
148+
cmdBufferSystem.AddJobHandleForProducer(Dependency);
149+
}
150+
}
151+
}

Controls/Systems/Messaging/ToggleVisibilitySystem.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Controls/Systems/Mobile/MobileMouseCollisionSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected unsafe override void OnUpdate() {
1919
}).WithNativeDisableUnsafePtrRestriction(touches).WithNativeDisableUnsafePtrRestriction(size).Run();
2020

2121
Entities.WithNativeDisableUnsafePtrRestriction(touches).WithNativeDisableUnsafePtrRestriction(size).
22-
WithNone<ButtonDisabledTag>().
22+
WithNone<NonInteractableButtontag>().
2323
ForEach((ref ClickState c0, ref ButtonVisual c1, in Dimensions c2, in LocalToWorld c3, in ButtonClickType c4) => {
2424
var aabb = new AABB {
2525
Center = c3.Position,

Controls/Systems/Standalone/StandaloneMouseCollisionSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected override void OnUpdate() {
3131
Pressed = new bool2(clickDown, clickUp)
3232
};
3333

34-
Dependency = Entities.WithNone<ButtonDisabledTag>().ForEach((ref ClickState c0, ref ButtonVisual c1,
34+
Entities.WithNone<NonInteractableButtontag>().ForEach((ref ClickState c0, ref ButtonVisual c1,
3535
in Dimensions c2, in LocalToWorld c3, in ButtonClickType c4) => {
3636

3737
var aabb = new AABB {
@@ -58,7 +58,7 @@ protected override void OnUpdate() {
5858
} else {
5959
c1.Value = ButtonVisualState.None;
6060
}
61-
}).ScheduleParallel(Dependency);
61+
}).Run();
6262
}
6363
}
6464
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Unity.Entities;
1+
using System;
2+
using Unity.Entities;
23
using UnityEngine;
34

45
namespace UGUIDots.Render.Authoring {
@@ -8,9 +9,11 @@ public class RenderCommandAuthoring : MonoBehaviour, IConvertGameObjectToEntity
89
public OrthographicRenderFeature RenderFeature;
910

1011
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) {
11-
dstManager.AddSharedComponentData(entity, new RenderCommand {
12-
RenderFeature = RenderFeature
13-
});
12+
if (ReferenceEquals(RenderFeature, null)) {
13+
throw new ArgumentException("The RenderFeature cannot be null!");
14+
}
15+
16+
dstManager.AddComponentData(entity, new RenderCommand { RenderFeature = RenderFeature });
1417
}
1518
}
16-
}
19+
}

0 commit comments

Comments
 (0)