Skip to content

Commit be3cbce

Browse files
TimGameDevTimGameDev
authored andcommitted
Templates cache refactoring
1 parent b3e45fd commit be3cbce

File tree

3 files changed

+25
-77
lines changed

3 files changed

+25
-77
lines changed

UnityWeld/Binding/AbstractTemplateSelector.cs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace UnityWeld.Binding
1010
{
1111
public abstract class AbstractTemplateSelector : AbstractMemberBinding
1212
{
13-
[SerializeField] private GameObject templatesRoot;
13+
[Header("Set templates for collection")]
14+
[SerializeField] private Template[] _templates;
1415
[SerializeField] private string viewModelPropertyName = string.Empty;
1516

1617
private IDictionary<Type, Template> _availableTemplates;
@@ -40,13 +41,10 @@ public string ViewModelPropertyName
4041
set => viewModelPropertyName = value;
4142
}
4243

43-
/// <summary>
44-
/// The GameObject in the scene that is the parent object for the tenplates.
45-
/// </summary>
46-
public GameObject TemplatesRoot
44+
public Template[] Templates
4745
{
48-
get => templatesRoot;
49-
set => templatesRoot = value;
46+
get => _templates;
47+
set => _templates = value;
5048
}
5149

5250
/// <summary>
@@ -68,25 +66,23 @@ protected IDictionary<Type, Template> AvailableTemplates
6866
// Cache available templates.
6967
private void CacheTemplates()
7068
{
69+
if(_templates == null || _templates.Length == 0)
70+
{
71+
return;
72+
}
73+
7174
_availableTemplates = new Dictionary<Type, Template>();
7275

73-
var buffer = Buffer.Templates;
74-
templatesRoot.GetComponentsInChildren(true, buffer);
75-
foreach(var template in buffer)
76-
{
77-
template.gameObject.SetActive(false);
78-
var typeName = template.GetViewModelTypeName();
79-
var type = TypeResolver.TypesWithBindingAttribute.FirstOrDefault(t => t.ToString() == typeName);
80-
if(type == null)
81-
{
82-
Debug.LogError(
83-
$"Template object {template.name} references type {typeName}, but no matching type with a [Binding] attribute could be found.",
84-
template);
85-
continue;
86-
}
8776

88-
_availableTemplates.Add(type, template);
89-
}
77+
_availableTemplates = _templates
78+
.Select(template =>
79+
{
80+
var typeName = template.GetViewModelTypeName();
81+
var type = TypeResolver.TypesWithBindingAttribute
82+
.FirstOrDefault(t => t.ToString() == typeName);
83+
return (type, template);
84+
})
85+
.ToDictionary(o => o.type, o => o.template);
9086
}
9187

9288
/// <summary>

UnityWeld_Editor/CollectionBindingEditor.cs

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,26 @@ class CollectionBindingEditor : BaseBindingEditor
1111
private CollectionBinding _targetScript;
1212
private SerializedProperty _templateInitialPoolCountProperty;
1313
private SerializedProperty _itemsContainerProperty;
14+
private SerializedProperty _templatesProperty;
1415

1516
private bool _viewModelPrefabModified;
16-
private bool _templatesRootPrefabModified;
17-
private bool _templateInitialPoolCountPrefabModified;
18-
private bool _itemsContainerPrefabModified;
1917

2018
protected override void OnEnabled()
2119
{
2220
// Initialise everything
2321
_targetScript = (CollectionBinding)target;
2422
_templateInitialPoolCountProperty = serializedObject.FindProperty("_templateInitialPoolCount");
2523
_itemsContainerProperty = serializedObject.FindProperty("_itemsContainer");
24+
_templatesProperty = serializedObject.FindProperty("_templates");
2625
}
2726

2827
protected override void OnInspector()
2928
{
3029
UpdatePrefabModifiedProperties();
3130

32-
EditorStyles.label.fontStyle = _templateInitialPoolCountPrefabModified ? FontStyle.Bold : DefaultFontStyle;
3331
EditorGUILayout.PropertyField(_templateInitialPoolCountProperty);
34-
35-
EditorStyles.label.fontStyle = _itemsContainerPrefabModified ? FontStyle.Bold : DefaultFontStyle;
3632
EditorGUILayout.PropertyField(_itemsContainerProperty);
33+
EditorGUILayout.PropertyField(_templatesProperty, true);
3734

3835
EditorStyles.label.fontStyle = _viewModelPrefabModified ? FontStyle.Bold : DefaultFontStyle;
3936
ShowViewModelPropertyMenu(
@@ -43,19 +40,6 @@ protected override void OnInspector()
4340
_targetScript.ViewModelPropertyName,
4441
property => true
4542
);
46-
47-
EditorStyles.label.fontStyle = _templatesRootPrefabModified ? FontStyle.Bold : DefaultFontStyle;
48-
UpdateProperty(
49-
updatedValue => _targetScript.TemplatesRoot = updatedValue,
50-
_targetScript.TemplatesRoot,
51-
(GameObject)EditorGUILayout.ObjectField(
52-
new GUIContent("Collection templates", "Parent object for all templates to copy and bind to items in the collection."),
53-
_targetScript.TemplatesRoot,
54-
typeof(GameObject),
55-
true
56-
),
57-
"Set collection templates root"
58-
);
5943
}
6044

6145
/// <summary>
@@ -74,18 +58,6 @@ private void UpdatePrefabModifiedProperties()
7458
case "viewModelPropertyName":
7559
_viewModelPrefabModified = property.prefabOverride;
7660
break;
77-
78-
case "TemplateInitialPoolCount":
79-
_templateInitialPoolCountPrefabModified = property.prefabOverride;
80-
break;
81-
82-
case "templatesRoot":
83-
_templatesRootPrefabModified = property.prefabOverride;
84-
break;
85-
86-
case "_itemsContainer":
87-
_itemsContainerPrefabModified = property.prefabOverride;
88-
break;
8961
}
9062
}
9163
while (property.Next(false));

UnityWeld_Editor/TemplateBindingEditor.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ class TemplateBindingEditor : BaseBindingEditor
1111
private TemplateBinding targetScript;
1212

1313
private bool viewModelPrefabModified;
14-
private bool templatesRootPrefabModified;
14+
private SerializedProperty _templatesProperty;
1515

1616
protected override void OnEnabled()
1717
{
1818
targetScript = (TemplateBinding)target;
19+
_templatesProperty = serializedObject.FindProperty("_templates");
1920
}
2021

2122
protected override void OnInspector()
@@ -37,24 +38,7 @@ protected override void OnInspector()
3738
property => true
3839
);
3940

40-
EditorStyles.label.fontStyle = templatesRootPrefabModified
41-
? FontStyle.Bold
42-
: DefaultFontStyle;
43-
44-
UpdateProperty(
45-
updatedValue => targetScript.TemplatesRoot = updatedValue,
46-
targetScript.TemplatesRoot,
47-
(GameObject)EditorGUILayout.ObjectField(
48-
new GUIContent(
49-
"Templates root object",
50-
"Parent object to the objects we want to use as templates."
51-
),
52-
targetScript.TemplatesRoot,
53-
typeof(GameObject),
54-
true
55-
),
56-
"Set template binding root object"
57-
);
41+
EditorGUILayout.PropertyField(_templatesProperty, true);
5842
}
5943

6044
/// <summary>
@@ -74,10 +58,6 @@ private void UpdatePrefabModifiedProperties()
7458
case "viewModelPropertyName":
7559
viewModelPrefabModified = property.prefabOverride;
7660
break;
77-
78-
case "templatesRoot":
79-
templatesRootPrefabModified = property.prefabOverride;
80-
break;
8161
}
8262
}
8363
while (property.Next(false));

0 commit comments

Comments
 (0)