Skip to content

Commit

Permalink
Merge pull request #720 from yellowisher/improve/instantiate_no_dirty
Browse files Browse the repository at this point in the history
Make Instantiate() revert dirty state of prefab
  • Loading branch information
hadashiA authored Nov 12, 2024
2 parents ec83f21 + 0f8a977 commit e427251
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public object SpawnInstance(IObjectResolver resolver)
var parent = destination.GetParent(resolver);

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

if (wasActive)
{
prefab.gameObject.SetActive(false);
Expand Down
2 changes: 2 additions & 0 deletions VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
using System;
using UnityEngine;
using VContainer.Internal;

namespace VContainer.Unity
{
public static class ObjectResolverUnityExtensions
{
public readonly struct PrefabDirtyScope : IDisposable
{
private readonly GameObject _prefab;
private readonly bool _madeDirty;

public PrefabDirtyScope(GameObject prefab)
{
_prefab = prefab;

#if UNITY_EDITOR && UNITY_2020_1_OR_NEWER
_madeDirty = prefab.activeSelf && !UnityEditor.EditorUtility.IsDirty(_prefab);
#endif
}

public void Dispose()
{
#if UNITY_EDITOR && UNITY_2020_1_OR_NEWER
if (_madeDirty)
{
UnityEditor.EditorUtility.ClearDirty(_prefab);
}
#endif
}
}

public static void InjectGameObject(this IObjectResolver resolver, GameObject gameObject)
{
void InjectGameObjectRecursive(GameObject current)
Expand Down Expand Up @@ -45,6 +71,8 @@ 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);

var instance = UnityEngine.Object.Instantiate(prefab, parent, worldPositionStays);
Expand Down Expand Up @@ -88,6 +116,8 @@ public static T Instantiate<T>(
where T : Component
{
var wasActive = prefab.gameObject.activeSelf;
using var dirtyScope = new PrefabDirtyScope(prefab.gameObject);

prefab.gameObject.SetActive(false);

var instance = UnityEngine.Object.Instantiate(prefab, position, rotation, parent);
Expand All @@ -111,6 +141,8 @@ static T Instantiate<T>(this LifetimeScope scope, T prefab, Vector3 position, Qu
where T : Component
{
var wasActive = prefab.gameObject.activeSelf;
using var dirtyScope = new PrefabDirtyScope(prefab.gameObject);

prefab.gameObject.SetActive(false);

T instance;
Expand Down Expand Up @@ -144,6 +176,8 @@ 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;
Expand Down Expand Up @@ -182,6 +216,8 @@ 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;
Expand Down Expand Up @@ -221,6 +257,8 @@ public static GameObject Instantiate(
Transform parent)
{
var wasActive = prefab.activeSelf;
using var dirtyScope = new PrefabDirtyScope(prefab);

prefab.SetActive(false);

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

0 comments on commit e427251

Please sign in to comment.