Skip to content
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

IObjectResolver.Instantiate() makes prefab dirty. #662

Open
yellowisher opened this issue Apr 28, 2024 · 1 comment
Open

IObjectResolver.Instantiate() makes prefab dirty. #662

yellowisher opened this issue Apr 28, 2024 · 1 comment

Comments

@yellowisher
Copy link

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
Copy link

IaroslavGusiev commented May 11, 2024

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;
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants