hadashiA / VContainer

The extra fast, minimum code size, GC-free DI (Dependency Injection) library running on Unity Game Engine.
https://vcontainer.hadashikick.jp
MIT License
1.81k stars 157 forks source link

IObjectResolver.Instantiate() makes prefab dirty. #662

Open yellowisher opened 2 months ago

yellowisher commented 2 months ago

If I instantiate prefab with IObjectResolver.Instantiate(), the prefab show up in my VCS without any changes. Yes, when I stage them, they are gone but its pretty annoying.

The reason is IObjectResolver.Instantiate() directly disable the prefab itself then Instantiate it. (maybe for calling Inject before Awake?) Zenject Instantiate a prefab as a child of inactive GameObject rather than disable prefab itself. Link

IaroslavGusiev commented 2 months ago
public static GameObject InstantiateAndInject([NotNull] this IObjectResolver resolver, [NotNull] GameObject prefab, Transform parent = null)
        {
            if (prefab == null)
                throw new NullReferenceException(nameof(prefab));

            bool prefabWasActive = prefab.activeSelf;
            prefab.SetActive(false);
            GameObject instance = resolver.Instantiate(prefab, parent);
            prefab.SetActive(prefabWasActive);
            instance.SetActive(prefabWasActive);
            return instance;
        }