-
Notifications
You must be signed in to change notification settings - Fork 82
[Need More Tests] Use RuntimeInitializeOnLoadMethod to call Init() #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
UlyssesWu
wants to merge
3
commits into
Real-Serious-Games:master
Choose a base branch
from
MonarchSolutions:feature/InitAfterSceneLoad
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using UnityEngine; | ||
using UnityWeld.Binding.Internal; | ||
|
||
namespace UnityWeld.Binding | ||
{ | ||
/// <summary> | ||
/// Bind to a boolean property on the view model and turn itself on | ||
/// or off based on its value. | ||
/// </summary> | ||
[AddComponentMenu("Unity Weld/ToggleSelf Binding")] | ||
[HelpURL("https://github.com/Real-Serious-Games/Unity-Weld")] | ||
public class ToggleSelfBinding : AbstractMemberBinding | ||
{ | ||
/// <summary> | ||
/// Type of the adapter we're using to adapt between the view model property | ||
/// and view property. | ||
/// </summary> | ||
public string ViewAdapterTypeName | ||
{ | ||
get { return viewAdapterTypeName; } | ||
set { viewAdapterTypeName = value; } | ||
} | ||
|
||
[SerializeField] | ||
private string viewAdapterTypeName; | ||
|
||
/// <summary> | ||
/// Options for adapting from the view model to the view property. | ||
/// </summary> | ||
public AdapterOptions ViewAdapterOptions | ||
{ | ||
get { return viewAdapterOptions; } | ||
set { viewAdapterOptions = value; } | ||
} | ||
|
||
[SerializeField] | ||
private AdapterOptions viewAdapterOptions; | ||
|
||
/// <summary> | ||
/// Name of the property in the view model to bind. | ||
/// </summary> | ||
public string ViewModelPropertyName | ||
{ | ||
get { return viewModelPropertyName; } | ||
set { viewModelPropertyName = value; } | ||
} | ||
|
||
[SerializeField] | ||
private string viewModelPropertyName; | ||
|
||
/// <summary> | ||
/// Watcher the view-model for changes that must be propagated to the view. | ||
/// </summary> | ||
private PropertyWatcher viewModelWatcher; | ||
|
||
/// <summary> | ||
/// Property for the propertySync to set in order to activate and deactivate all children | ||
/// </summary> | ||
public bool SelfActive | ||
{ | ||
set | ||
{ | ||
SetActive(value); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Connect() | ||
{ | ||
var viewModelEndPoint = MakeViewModelEndPoint(viewModelPropertyName, null, null); | ||
|
||
var propertySync = new PropertySync( | ||
// Source | ||
viewModelEndPoint, | ||
|
||
// Dest | ||
new PropertyEndPoint( | ||
this, | ||
"SelfActive", | ||
CreateAdapter(viewAdapterTypeName), | ||
viewAdapterOptions, | ||
"view", | ||
this | ||
), | ||
|
||
// Errors, exceptions and validation. | ||
null, // Validation not needed | ||
|
||
this | ||
); | ||
|
||
viewModelWatcher = viewModelEndPoint.Watch( | ||
() => propertySync.SyncFromSource() | ||
); | ||
|
||
// Copy the initial value over from the view-model. | ||
propertySync.SyncFromSource(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Disconnect() | ||
{ | ||
if (viewModelWatcher != null) | ||
{ | ||
viewModelWatcher.Dispose(); | ||
viewModelWatcher = null; | ||
} | ||
} | ||
|
||
private void SetActive(bool active) | ||
{ | ||
transform.gameObject.SetActive(active); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
using UnityWeld.Binding; | ||
|
||
namespace UnityWeld | ||
{ | ||
/// <summary> | ||
/// Initialize all bindings when scene loaded | ||
/// </summary> | ||
public static class WeldContext | ||
{ | ||
/// <summary> | ||
/// Automatically called before scene load | ||
/// </summary> | ||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | ||
public static void WeldContextStartup() | ||
{ | ||
SceneManager.sceneLoaded += SceneManager_SceneLoaded; | ||
} | ||
|
||
static void SceneManager_SceneLoaded(Scene scene, LoadSceneMode mode) | ||
{ | ||
foreach (var parentT in scene.GetRootGameObjects()) | ||
{ | ||
//Call Init() on all bindings | ||
foreach (var binding in parentT.GetComponentsInChildren<AbstractMemberBinding>(true)) //including inactive GameObjects | ||
{ | ||
binding.Init(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
using System; | ||
using UnityEditor; | ||
using UnityEditor.AnimatedValues; | ||
using UnityEngine; | ||
using UnityWeld.Binding; | ||
using UnityWeld.Binding.Internal; | ||
|
||
namespace UnityWeld_Editor | ||
{ | ||
[CustomEditor(typeof(ToggleSelfBinding))] | ||
public class ToggleSelfBindingEditor : BaseBindingEditor | ||
{ | ||
private ToggleSelfBinding targetScript; | ||
|
||
private AnimBool viewAdapterOptionsFade; | ||
|
||
private bool viewAdapterPrefabModified; | ||
private bool viewAdapterOptionsPrefabModified; | ||
private bool viewModelPropertyPrefabModified; | ||
|
||
private void OnEnable() | ||
{ | ||
targetScript = (ToggleSelfBinding)target; | ||
|
||
Type adapterType; | ||
|
||
viewAdapterOptionsFade = new AnimBool( | ||
ShouldShowAdapterOptions(targetScript.ViewAdapterTypeName, out adapterType) | ||
); | ||
|
||
viewAdapterOptionsFade.valueChanged.AddListener(Repaint); | ||
} | ||
|
||
private void OnDisable() | ||
{ | ||
viewAdapterOptionsFade.valueChanged.RemoveListener(Repaint); | ||
} | ||
|
||
public override void OnInspectorGUI() | ||
{ | ||
if (CannotModifyInPlayMode()) | ||
{ | ||
GUI.enabled = false; | ||
} | ||
|
||
UpdatePrefabModifiedProperties(); | ||
|
||
var defaultLabelStyle = EditorStyles.label.fontStyle; | ||
|
||
var viewPropertyType = typeof(bool); | ||
|
||
var viewAdapterTypeNames = GetAdapterTypeNames( | ||
type => TypeResolver.FindAdapterAttribute(type).OutputType == viewPropertyType | ||
); | ||
|
||
EditorStyles.label.fontStyle = viewAdapterPrefabModified | ||
? FontStyle.Bold | ||
: defaultLabelStyle; | ||
|
||
ShowAdapterMenu( | ||
new GUIContent( | ||
"View adapter", | ||
"Adapter that converts values sent from the view-model to the view." | ||
), | ||
viewAdapterTypeNames, | ||
targetScript.ViewAdapterTypeName, | ||
newValue => | ||
{ | ||
// Get rid of old adapter options if we changed the type of the adapter. | ||
if (newValue != targetScript.ViewAdapterTypeName) | ||
{ | ||
Undo.RecordObject(targetScript, "Set view adapter options"); | ||
targetScript.ViewAdapterOptions = null; | ||
} | ||
|
||
UpdateProperty( | ||
updatedValue => targetScript.ViewAdapterTypeName = updatedValue, | ||
targetScript.ViewAdapterTypeName, | ||
newValue, | ||
"Set view adapter" | ||
); | ||
} | ||
); | ||
|
||
Type adapterType; | ||
viewAdapterOptionsFade.target = ShouldShowAdapterOptions( | ||
targetScript.ViewAdapterTypeName, | ||
out adapterType | ||
); | ||
|
||
EditorStyles.label.fontStyle = viewAdapterOptionsPrefabModified | ||
? FontStyle.Bold | ||
: defaultLabelStyle; | ||
|
||
ShowAdapterOptionsMenu( | ||
"View adapter options", | ||
adapterType, | ||
options => targetScript.ViewAdapterOptions = options, | ||
targetScript.ViewAdapterOptions, | ||
viewAdapterOptionsFade.faded | ||
); | ||
|
||
EditorGUILayout.Space(); | ||
|
||
EditorStyles.label.fontStyle = viewModelPropertyPrefabModified | ||
? FontStyle.Bold | ||
: defaultLabelStyle; | ||
|
||
var adaptedViewPropertyType = AdaptTypeBackward( | ||
viewPropertyType, | ||
targetScript.ViewAdapterTypeName | ||
); | ||
ShowViewModelPropertyMenu( | ||
new GUIContent( | ||
"View-model property", | ||
"Property on the view-model to bind to." | ||
), | ||
TypeResolver.FindBindableProperties(targetScript), | ||
updatedValue => targetScript.ViewModelPropertyName = updatedValue, | ||
targetScript.ViewModelPropertyName, | ||
property => property.PropertyType == adaptedViewPropertyType | ||
); | ||
|
||
EditorStyles.label.fontStyle = defaultLabelStyle; | ||
} | ||
|
||
private void UpdatePrefabModifiedProperties() | ||
{ | ||
var property = serializedObject.GetIterator(); | ||
// Need to call Next(true) to get the first child. Once we have it, Next(false) | ||
// will iterate through the properties. | ||
property.Next(true); | ||
do | ||
{ | ||
switch (property.name) | ||
{ | ||
case "viewAdapterTypeName": | ||
viewAdapterPrefabModified = property.prefabOverride; | ||
break; | ||
|
||
case "viewAdapterOptions": | ||
viewAdapterOptionsPrefabModified = property.prefabOverride; | ||
break; | ||
|
||
case "viewModelPropertyName": | ||
viewModelPropertyPrefabModified = property.prefabOverride; | ||
break; | ||
} | ||
} | ||
while (property.Next(false)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should just delete this. We try to avoid leaving blocks of commented-out code because it can be confusing to someone else reading the code.