Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

[ad_1]

I’ve many situations of a category that instantiates prefabs that solely exist within the editor. When this occurs, an occasion is invoked to alert subscribers that issues are being instantiated:

utilizing UnityEngine;
utilizing System;

public class GameObjectCreator : MonoBehaviour 
{
    [SerializeField]
    personal GameObject _prefab;

    public occasion Motion<GameObject> OnCreated;

    public void Create()
    {
        var clone = Instantiate(_prefab);
        OnCreated?.Invoke(clone);
    }
}

I’m pairing this class with a couple of helper lessons which have completely different duties for establishing these clones; these helper lessons additionally exist solely inside the editor. I’m utilizing OnValidate to subscribe to the OnCreated occasion, which permits all the things to work inside Unity, however issues break in an precise construct.

public class CreationHelper : MonoBehaviour
{
    [SerializeField]
    personal GameObjectCreator _creator;

    personal void DoSomeStuff(GameObject gameObject) 
    {
        // ...
    }

    personal void OnValidate() 
    {
        _creator.OnCreated += DoSomeStuff; // That is misplaced at runtime
    }
}

I do know I can merely use a supervisor for added setup inside a given scene, however am questioning if there’s a approach to serialize occasion subscriptions so I can proceed with this present construction.

I don’t imagine UnityEvents will work as a result of they require concrete references to issues within the scene, and I’m instantiating an unknown variety of prefabs at runtime.

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *