Skip to content

Commit 7c48eb5

Browse files
Scope
1 parent fa4201f commit 7c48eb5

12 files changed

Lines changed: 446 additions & 33 deletions

samples/UnityApp/Assets/Scenes/2. Child Scene.unity

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ GameObject:
128128
serializedVersion: 6
129129
m_Component:
130130
- component: {fileID: 1665383480}
131+
- component: {fileID: 1665383481}
131132
m_Layer: 0
132133
m_Name: Child
133134
m_TagString: Untagged
@@ -150,6 +151,18 @@ Transform:
150151
m_Children: []
151152
m_Father: {fileID: 0}
152153
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
154+
--- !u!114 &1665383481
155+
MonoBehaviour:
156+
m_ObjectHideFlags: 0
157+
m_CorrespondingSourceObject: {fileID: 0}
158+
m_PrefabInstance: {fileID: 0}
159+
m_PrefabAsset: {fileID: 0}
160+
m_GameObject: {fileID: 1665383478}
161+
m_Enabled: 1
162+
m_EditorHideFlags: 0
163+
m_Script: {fileID: 11500000, guid: 27a6efd7f72f4a2a8d7468f9ae4cc6fc, type: 3}
164+
m_Name:
165+
m_EditorClassIdentifier: Assembly-CSharp::ModeSceneInstaller
153166
--- !u!1660057539 &9223372036854775807
154167
SceneRoots:
155168
m_ObjectHideFlags: 0
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!29 &1
4+
OcclusionCullingSettings:
5+
m_ObjectHideFlags: 0
6+
serializedVersion: 2
7+
m_OcclusionBakeSettings:
8+
smallestOccluder: 5
9+
smallestHole: 0.25
10+
backfaceThreshold: 100
11+
m_SceneGUID: 00000000000000000000000000000000
12+
m_OcclusionCullingData: {fileID: 0}
13+
--- !u!104 &2
14+
RenderSettings:
15+
m_ObjectHideFlags: 0
16+
serializedVersion: 10
17+
m_Fog: 0
18+
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
19+
m_FogMode: 3
20+
m_FogDensity: 0.01
21+
m_LinearFogStart: 0
22+
m_LinearFogEnd: 300
23+
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1}
24+
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1}
25+
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1}
26+
m_AmbientIntensity: 1
27+
m_AmbientMode: 0
28+
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1}
29+
m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0}
30+
m_HaloStrength: 0.5
31+
m_FlareStrength: 1
32+
m_FlareFadeSpeed: 3
33+
m_HaloTexture: {fileID: 0}
34+
m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0}
35+
m_DefaultReflectionMode: 0
36+
m_DefaultReflectionResolution: 128
37+
m_ReflectionBounces: 1
38+
m_ReflectionIntensity: 1
39+
m_CustomReflection: {fileID: 0}
40+
m_Sun: {fileID: 0}
41+
m_UseRadianceAmbientProbe: 0
42+
--- !u!157 &3
43+
LightmapSettings:
44+
m_ObjectHideFlags: 0
45+
serializedVersion: 13
46+
m_BakeOnSceneLoad: 0
47+
m_GISettings:
48+
serializedVersion: 2
49+
m_BounceScale: 1
50+
m_IndirectOutputScale: 1
51+
m_AlbedoBoost: 1
52+
m_EnvironmentLightingMode: 0
53+
m_EnableBakedLightmaps: 1
54+
m_EnableRealtimeLightmaps: 0
55+
m_LightmapEditorSettings:
56+
serializedVersion: 12
57+
m_Resolution: 2
58+
m_BakeResolution: 40
59+
m_AtlasSize: 1024
60+
m_AO: 0
61+
m_AOMaxDistance: 1
62+
m_CompAOExponent: 1
63+
m_CompAOExponentDirect: 0
64+
m_ExtractAmbientOcclusion: 0
65+
m_Padding: 2
66+
m_LightmapParameters: {fileID: 0}
67+
m_LightmapsBakeMode: 1
68+
m_TextureCompression: 1
69+
m_ReflectionCompression: 2
70+
m_MixedBakeMode: 2
71+
m_BakeBackend: 1
72+
m_PVRSampling: 1
73+
m_PVRDirectSampleCount: 32
74+
m_PVRSampleCount: 512
75+
m_PVRBounces: 2
76+
m_PVREnvironmentSampleCount: 256
77+
m_PVREnvironmentReferencePointCount: 2048
78+
m_PVRFilteringMode: 1
79+
m_PVRDenoiserTypeDirect: 1
80+
m_PVRDenoiserTypeIndirect: 1
81+
m_PVRDenoiserTypeAO: 1
82+
m_PVRFilterTypeDirect: 0
83+
m_PVRFilterTypeIndirect: 0
84+
m_PVRFilterTypeAO: 0
85+
m_PVREnvironmentMIS: 1
86+
m_PVRCulling: 1
87+
m_PVRFilteringGaussRadiusDirect: 1
88+
m_PVRFilteringGaussRadiusIndirect: 5
89+
m_PVRFilteringGaussRadiusAO: 2
90+
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
91+
m_PVRFilteringAtrousPositionSigmaIndirect: 2
92+
m_PVRFilteringAtrousPositionSigmaAO: 1
93+
m_ExportTrainingData: 0
94+
m_TrainingDataDestination: TrainingData
95+
m_LightProbeSampleCountMultiplier: 4
96+
m_LightingDataAsset: {fileID: 20201, guid: 0000000000000000f000000000000000, type: 0}
97+
m_LightingSettings: {fileID: 0}
98+
--- !u!196 &4
99+
NavMeshSettings:
100+
serializedVersion: 2
101+
m_ObjectHideFlags: 0
102+
m_BuildSettings:
103+
serializedVersion: 3
104+
agentTypeID: 0
105+
agentRadius: 0.5
106+
agentHeight: 2
107+
agentSlope: 45
108+
agentClimb: 0.4
109+
ledgeDropHeight: 0
110+
maxJumpAcrossDistance: 0
111+
minRegionArea: 2
112+
manualCellSize: 0
113+
cellSize: 0.16666667
114+
manualTileSize: 0
115+
tileSize: 256
116+
buildHeightMesh: 0
117+
maxJobWorkers: 0
118+
preserveTilesOutsideBounds: 0
119+
debug:
120+
m_Flags: 0
121+
m_NavMeshData: {fileID: 0}
122+
--- !u!1 &1665383478
123+
GameObject:
124+
m_ObjectHideFlags: 0
125+
m_CorrespondingSourceObject: {fileID: 0}
126+
m_PrefabInstance: {fileID: 0}
127+
m_PrefabAsset: {fileID: 0}
128+
serializedVersion: 6
129+
m_Component:
130+
- component: {fileID: 1665383480}
131+
- component: {fileID: 1665383481}
132+
m_Layer: 0
133+
m_Name: Child B
134+
m_TagString: Untagged
135+
m_Icon: {fileID: 0}
136+
m_NavMeshLayer: 0
137+
m_StaticEditorFlags: 0
138+
m_IsActive: 1
139+
--- !u!4 &1665383480
140+
Transform:
141+
m_ObjectHideFlags: 0
142+
m_CorrespondingSourceObject: {fileID: 0}
143+
m_PrefabInstance: {fileID: 0}
144+
m_PrefabAsset: {fileID: 0}
145+
m_GameObject: {fileID: 1665383478}
146+
serializedVersion: 2
147+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
148+
m_LocalPosition: {x: 0, y: 0, z: 0}
149+
m_LocalScale: {x: 1, y: 1, z: 1}
150+
m_ConstrainProportionsScale: 0
151+
m_Children: []
152+
m_Father: {fileID: 0}
153+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
154+
--- !u!114 &1665383481
155+
MonoBehaviour:
156+
m_ObjectHideFlags: 0
157+
m_CorrespondingSourceObject: {fileID: 0}
158+
m_PrefabInstance: {fileID: 0}
159+
m_PrefabAsset: {fileID: 0}
160+
m_GameObject: {fileID: 1665383478}
161+
m_Enabled: 1
162+
m_EditorHideFlags: 0
163+
m_Script: {fileID: 11500000, guid: 27a6efd7f72f4a2a8d7468f9ae4cc6fc, type: 3}
164+
m_Name:
165+
m_EditorClassIdentifier: Assembly-CSharp::ModeSceneInstaller
166+
--- !u!1660057539 &9223372036854775807
167+
SceneRoots:
168+
m_ObjectHideFlags: 0
169+
m_Roots:
170+
- {fileID: 1665383480}

samples/UnityApp/Assets/Scenes/3. Child Scene B.unity.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
using Pure.DI;
21
using UnityEngine;
32

4-
public partial class ChildComposition
5-
{
6-
void Setup() => DI.Setup()
7-
.DependsOn(nameof(RootComposition), SetupContextKind.Argument, "parent")
8-
.Root<Child>("child");
9-
}
10-
113
public class Child
124
{
13-
private IClockService _clockService;
5+
private readonly IClockService _clockService;
146

157
public Child(IClockService clockService)
168
{
@@ -19,6 +11,7 @@ public Child(IClockService clockService)
1911

2012
public void Initialize()
2113
{
22-
Debug.Log("Child: " + _clockService.GetHashCode());
14+
// Debug.Log("Child: " + _clockService.GetHashCode());
15+
Debug.Log($"Child instance: {GetHashCode()}, ClockService(singleton): {_clockService.GetHashCode()}");
2316
}
24-
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Pure.DI;
2+
using UnityEngine;
3+
4+
public partial class GameComposition
5+
{
6+
static void Setup() => DI.Setup()
7+
.Arg<IClockConfig>("clockConfig")
8+
.Bind<IClockService>().As(Lifetime.Singleton).To<ClockService>()
9+
.Bind().As(Lifetime.Scoped).To<Child>()
10+
.Root<Root>("Root")
11+
.Root<Child>("Child")
12+
.Builders<MonoBehaviour>();
13+
}

samples/UnityApp/Assets/Scripts/Example/GameComposition.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Pure.DI;
2+
using UnityEngine;
3+
4+
public sealed class ModeSceneInstaller : MonoBehaviour
5+
{
6+
private GameComposition _scope = null!;
7+
8+
[Dependency]
9+
public IClockService ClockService { private get; set; } = null!;
10+
11+
void Awake()
12+
{
13+
if (RootComposition.Instance == null)
14+
{
15+
Debug.LogError("RootComposition instance is missing. Cannot create mode scope.");
16+
return;
17+
}
18+
19+
_scope = RootComposition.Instance.CreateScope();
20+
_scope.BuildUp(this);
21+
22+
Debug.Log("Mode installer: " + ClockService.GetHashCode());
23+
_scope.Child.Initialize();
24+
}
25+
26+
void OnDestroy()
27+
{
28+
if (_scope != null)
29+
{
30+
_scope.Dispose();
31+
_scope = null!;
32+
}
33+
}
34+
}

samples/UnityApp/Assets/Scripts/Example/ModeSceneInstaller.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.EventSystems;
4+
using UnityEngine.UI;
5+
6+
public static class ModeSwitchUi
7+
{
8+
private const string UiRootName = "Mode Switch UI";
9+
10+
public static void Ensure(RootComposition rootComposition)
11+
{
12+
if (rootComposition == null)
13+
{
14+
return;
15+
}
16+
17+
var existing = GameObject.Find(UiRootName);
18+
if (existing != null)
19+
{
20+
return;
21+
}
22+
23+
var uiRoot = new GameObject(UiRootName);
24+
UnityEngine.Object.DontDestroyOnLoad(uiRoot);
25+
26+
var canvas = uiRoot.AddComponent<Canvas>();
27+
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
28+
29+
uiRoot.AddComponent<CanvasScaler>();
30+
uiRoot.AddComponent<GraphicRaycaster>();
31+
32+
EnsureEventSystem();
33+
34+
CreateButton(
35+
uiRoot.transform,
36+
label: "Mode 1",
37+
position: new Vector2(110f, -30f),
38+
onClick: rootComposition.SwitchToFirstMode);
39+
40+
CreateButton(
41+
uiRoot.transform,
42+
label: "Mode 2",
43+
position: new Vector2(110f, -80f),
44+
onClick: rootComposition.SwitchToSecondMode);
45+
}
46+
47+
private static void EnsureEventSystem()
48+
{
49+
if (UnityEngine.Object.FindFirstObjectByType<EventSystem>() != null)
50+
{
51+
return;
52+
}
53+
54+
var eventSystemObject = new GameObject("EventSystem");
55+
UnityEngine.Object.DontDestroyOnLoad(eventSystemObject);
56+
eventSystemObject.AddComponent<EventSystem>();
57+
eventSystemObject.AddComponent<StandaloneInputModule>();
58+
}
59+
60+
private static void CreateButton(Transform parent, string label, Vector2 position, Action onClick)
61+
{
62+
var buttonObject = new GameObject($"{label} Button");
63+
buttonObject.transform.SetParent(parent, false);
64+
65+
var rectTransform = buttonObject.AddComponent<RectTransform>();
66+
rectTransform.sizeDelta = new Vector2(180f, 40f);
67+
rectTransform.anchorMin = new Vector2(0f, 1f);
68+
rectTransform.anchorMax = new Vector2(0f, 1f);
69+
rectTransform.pivot = new Vector2(0.5f, 0.5f);
70+
rectTransform.anchoredPosition = position;
71+
72+
var image = buttonObject.AddComponent<Image>();
73+
image.color = new Color(0.18f, 0.18f, 0.18f, 0.9f);
74+
75+
var button = buttonObject.AddComponent<Button>();
76+
button.onClick.AddListener(() => onClick());
77+
78+
var textObject = new GameObject("Label");
79+
textObject.transform.SetParent(buttonObject.transform, false);
80+
81+
var textRect = textObject.AddComponent<RectTransform>();
82+
textRect.anchorMin = Vector2.zero;
83+
textRect.anchorMax = Vector2.one;
84+
textRect.offsetMin = Vector2.zero;
85+
textRect.offsetMax = Vector2.zero;
86+
87+
var text = textObject.AddComponent<Text>();
88+
text.text = label;
89+
text.alignment = TextAnchor.MiddleCenter;
90+
text.color = Color.white;
91+
text.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
92+
text.fontSize = 18;
93+
}
94+
}

samples/UnityApp/Assets/Scripts/Example/ModeSwitchUi.cs.meta

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

0 commit comments

Comments
 (0)