Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Editor/DomainReloadHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,27 @@ private static void OnRuntimeLoad()
if (valueToAssign != null) {
value = Convert.ChangeType(valueToAssign, fieldType);
if (value == null)
Debug.LogWarning("Unable to assign value of type {valueToAssign.GetType()} to field {field.Name} of type {fieldType}.");
Debug.LogWarning($"Unable to assign value of type {valueToAssign.GetType()} to field {field.Name} of type {fieldType}.");
}

// If assignNewTypeInstance is set, create a new instance of this type and assign it to the field
if (assignNewTypeInstance)
value = Activator.CreateInstance(fieldType);
if (assignNewTypeInstance){
value = Activator.CreateInstance(fieldType, reloadAttribute.arguments);
}

try {
field.SetValue(null, value);
clearedValues++;
}
catch {
if (valueToAssign == null)
Debug.LogWarning("Unable to clear field {field.Name}.");
Debug.LogWarning($"Unable to clear field {field.Name}.");
else
Debug.LogWarning("Unable to assign field {field.Name}.");
Debug.LogWarning($"Unable to assign field {field.Name}.");
}

} else {
Debug.LogWarning("Inapplicable field {field.Name} to clear; must be static and non-generic.");
Debug.LogWarning($"Inapplicable field {field.Name} to clear; must be static and non-generic.");
}

}
Expand Down
7 changes: 5 additions & 2 deletions Runtime/DomainReloadAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class ClearOnReloadAttribute : Attribute
{
public readonly object valueToAssign;
public readonly bool assignNewTypeInstance;
public readonly object[] arguments;

/// <summary>
/// Marks field, property or event to be cleared on reload.
Expand All @@ -29,10 +30,12 @@ public ClearOnReloadAttribute(object valueToAssign)
/// Marks field of property to be cleared or re-initialized on reload.
/// </summary>
/// <param name="assignNewTypeInstance">If true, field/property will be assigned a newly created object of its type on reload. Has no effect on events.</param>
public ClearOnReloadAttribute(bool assignNewTypeInstance = false)
/// <param name="arguments">Pass arguments to the constructor of the new type instance.</param>
public ClearOnReloadAttribute(bool assignNewTypeInstance = false, params object[] arguments)
{
this.valueToAssign = null;
this.assignNewTypeInstance = assignNewTypeInstance;
this.arguments = arguments;
}
}

Expand All @@ -43,4 +46,4 @@ public class ExecuteOnReloadAttribute : Attribute
/// Marks method to be executed on reload.
/// </summary>
public ExecuteOnReloadAttribute() {}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.jsteinhauer.unitydomainreloadhelper",
"version": "1.0.0",
"version": "1.0.2",
"displayName": "Unity Domain Reload Helper",
"description": "A couple of attributes that help disabling Domain Reloading in Unity easier.",
"unity": "2019.3",
Expand Down