Skip to content

Commit

Permalink
Fix compilation errors in old unity version
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Nov 15, 2024
1 parent b1fd5ae commit c1c5da4
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,37 @@ public object SpawnInstance(IObjectResolver resolver)
{
var prefab = prefabFinder(resolver);
var parent = destination.GetParent(resolver);

var wasActive = prefab.gameObject.activeSelf;
using var dirtyScope = new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject);

if (wasActive)
using (new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject))
{
prefab.gameObject.SetActive(false);
}

var component = parent != null
? UnityEngine.Object.Instantiate(prefab, parent)
: UnityEngine.Object.Instantiate(prefab);
if (wasActive)
{
prefab.gameObject.SetActive(false);
}

if (VContainerSettings.Instance != null && VContainerSettings.Instance.RemoveClonePostfix)
component.name = prefab.name;
var component = parent != null
? UnityEngine.Object.Instantiate(prefab, parent)
: UnityEngine.Object.Instantiate(prefab);

try
{
injector.Inject(component, resolver, customParameters);
destination.ApplyDontDestroyOnLoadIfNeeded(component);
}
finally
{
if (wasActive)
if (VContainerSettings.Instance != null && VContainerSettings.Instance.RemoveClonePostfix)
component.name = prefab.name;

try
{
prefab.gameObject.SetActive(true);
component.gameObject.SetActive(true);
injector.Inject(component, resolver, customParameters);
destination.ApplyDontDestroyOnLoadIfNeeded(component);
}
finally
{
if (wasActive)
{
prefab.gameObject.SetActive(true);
component.gameObject.SetActive(true);
}
}
return component;
}

return component;
}
}
}
35 changes: 18 additions & 17 deletions VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,24 +263,25 @@ public TScope CreateChildFromPrefab<TScope>(TScope prefab, IInstaller installer
where TScope : LifetimeScope
{
var wasActive = prefab.gameObject.activeSelf;
using var dirtyScope = new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject);

if (wasActive)
{
prefab.gameObject.SetActive(false);
}
var child = Instantiate(prefab, transform, false);
if (installer != null)
{
child.localExtraInstallers.Add(installer);
}
child.parentReference.Object = this;
if (wasActive)
using (new ObjectResolverUnityExtensions.PrefabDirtyScope(prefab.gameObject))
{
prefab.gameObject.SetActive(true);
child.gameObject.SetActive(true);
if (wasActive)
{
prefab.gameObject.SetActive(false);
}
var child = Instantiate(prefab, transform, false);
if (installer != null)
{
child.localExtraInstallers.Add(installer);
}
child.parentReference.Object = this;
if (wasActive)
{
prefab.gameObject.SetActive(true);
child.gameObject.SetActive(true);
}
return child;
}
return child;
}

public TScope CreateChildFromPrefab<TScope>(TScope prefab, Action<IContainerBuilder> installation)
Expand Down Expand Up @@ -317,7 +318,7 @@ LifetimeScope GetRuntimeParent()

if (parentReference.Object != null)
return parentReference.Object;

// Find via implementation
var implParent = FindParent();
if (implParent != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Dispose()
#endif
}
}

public static void InjectGameObject(this IObjectResolver resolver, GameObject gameObject)
{
void InjectGameObjectRecursive(GameObject current)
Expand Down Expand Up @@ -71,25 +71,26 @@ public static T Instantiate<T>(this IObjectResolver resolver, T prefab, Transfor
where T : Component
{
var wasActive = prefab.gameObject.activeSelf;
using var dirtyScope = new PrefabDirtyScope(prefab.gameObject);

prefab.gameObject.SetActive(false);
using (new PrefabDirtyScope(prefab.gameObject))
{
prefab.gameObject.SetActive(false);

var instance = UnityEngine.Object.Instantiate(prefab, parent, worldPositionStays);
var instance = UnityEngine.Object.Instantiate(prefab, parent, worldPositionStays);

SetName(instance, prefab);
SetName(instance, prefab);

try
{
resolver.InjectGameObject(instance.gameObject);
}
finally
{
prefab.gameObject.SetActive(wasActive);
instance.gameObject.SetActive(wasActive);
}
try
{
resolver.InjectGameObject(instance.gameObject);
}
finally
{
prefab.gameObject.SetActive(wasActive);
instance.gameObject.SetActive(wasActive);
}

return instance;
return instance;
}
}

public static T Instantiate<T>(
Expand All @@ -116,25 +117,25 @@ public static T Instantiate<T>(
where T : Component
{
var wasActive = prefab.gameObject.activeSelf;
using var dirtyScope = new PrefabDirtyScope(prefab.gameObject);

prefab.gameObject.SetActive(false);
using (new PrefabDirtyScope(prefab.gameObject))
{
prefab.gameObject.SetActive(false);

var instance = UnityEngine.Object.Instantiate(prefab, position, rotation, parent);
var instance = UnityEngine.Object.Instantiate(prefab, position, rotation, parent);

SetName(instance, prefab);
SetName(instance, prefab);

try
{
resolver.InjectGameObject(instance.gameObject);
}
finally
{
prefab.gameObject.SetActive(wasActive);
instance.gameObject.SetActive(wasActive);
try
{
resolver.InjectGameObject(instance.gameObject);
}
finally
{
prefab.gameObject.SetActive(wasActive);
instance.gameObject.SetActive(wasActive);
}
return instance;
}

return instance;
}

static T Instantiate<T>(this LifetimeScope scope, T prefab, Vector3 position, Quaternion rotation)
Expand Down Expand Up @@ -176,36 +177,36 @@ static T Instantiate<T>(this LifetimeScope scope, T prefab, Vector3 position, Qu
static GameObject Instantiate(this LifetimeScope scope, GameObject prefab, Vector3 position, Quaternion rotation)
{
var wasActive = prefab.activeSelf;
using var dirtyScope = new PrefabDirtyScope(prefab);

prefab.SetActive(false);

GameObject instance;
if (scope.IsRoot)
using (new PrefabDirtyScope(prefab))
{
instance = UnityEngine.Object.Instantiate(prefab, position, rotation);
UnityEngine.Object.DontDestroyOnLoad(instance);
}
else
{
// Into the same scene as LifetimeScope
instance = UnityEngine.Object.Instantiate(prefab, position, rotation, scope.transform);
instance.transform.SetParent(null);
}
prefab.SetActive(false);

SetName(instance, prefab);
GameObject instance;
if (scope.IsRoot)
{
instance = UnityEngine.Object.Instantiate(prefab, position, rotation);
UnityEngine.Object.DontDestroyOnLoad(instance);
}
else
{
// Into the same scene as LifetimeScope
instance = UnityEngine.Object.Instantiate(prefab, position, rotation, scope.transform);
instance.transform.SetParent(null);
}

try
{
scope.Container.InjectGameObject(instance);
}
finally
{
prefab.SetActive(wasActive);
instance.SetActive(wasActive);
}
SetName(instance, prefab);

return instance;
try
{
scope.Container.InjectGameObject(instance);
}
finally
{
prefab.SetActive(wasActive);
instance.SetActive(wasActive);
}
return instance;
}
}

public static GameObject Instantiate(this IObjectResolver resolver, GameObject prefab)
Expand All @@ -216,23 +217,24 @@ public static GameObject Instantiate(this IObjectResolver resolver, GameObject p
public static GameObject Instantiate(this IObjectResolver resolver, GameObject prefab, Transform parent, bool worldPositionStays = false)
{
var wasActive = prefab.activeSelf;
using var dirtyScope = new PrefabDirtyScope(prefab);

prefab.SetActive(false);

GameObject instance = null;
try
using (new PrefabDirtyScope(prefab))
{
instance = UnityEngine.Object.Instantiate(prefab, parent, worldPositionStays);
SetName(instance, prefab);
resolver.InjectGameObject(instance);
}
finally
{
prefab.SetActive(wasActive);
instance?.SetActive(wasActive);
prefab.SetActive(false);

GameObject instance = null;
try
{
instance = UnityEngine.Object.Instantiate(prefab, parent, worldPositionStays);
SetName(instance, prefab);
resolver.InjectGameObject(instance);
}
finally
{
prefab.SetActive(wasActive);
instance?.SetActive(wasActive);
}
return instance;
}
return instance;
}

public static GameObject Instantiate(
Expand All @@ -257,25 +259,26 @@ public static GameObject Instantiate(
Transform parent)
{
var wasActive = prefab.activeSelf;
using var dirtyScope = new PrefabDirtyScope(prefab);
using (new PrefabDirtyScope(prefab))
{
prefab.SetActive(false);

prefab.SetActive(false);
var instance = UnityEngine.Object.Instantiate(prefab, position, rotation, parent);

var instance = UnityEngine.Object.Instantiate(prefab, position, rotation, parent);
SetName(instance, prefab);

SetName(instance, prefab);
try
{
resolver.InjectGameObject(instance);
}
finally
{
prefab.SetActive(wasActive);
instance.SetActive(wasActive);
}

try
{
resolver.InjectGameObject(instance);
}
finally
{
prefab.SetActive(wasActive);
instance.SetActive(wasActive);
return instance;
}

return instance;
}

static void SetName(UnityEngine.Object instance, UnityEngine.Object prefab)
Expand Down

0 comments on commit c1c5da4

Please sign in to comment.