Unity singleton

Source: https://www.cnblogs.com/llstart-new0201/p/9730181.html

 

(A) The simplest of simple interest

Copy the code
public class WebRequestUtility : MonoBehaviour
{
    public static WebRequestUtility Instance;

    private void Awake()
    {
        Instance = this;
    }
}
Copy the code

This is the simplest and most effective, the most practical and no problem of simple interest mode. If there is any problem, and that is referenced in another script awake, awake Ruoguo other script execution than the first awke, it will be reported null reference. This problem is solved by setting the script execution order class. The way, try to start with a general initialization start, unless it is determined first run with awake, do not use in order to perform first and awake;

(Ii) Constructor Method

Copy the code
public class WebRequestUtility : MonoBehaviour
{
    public static WebRequestUtility Instance;

    WebRequestUtility()
    {
        Instance = this;
    }

}
Copy the code

Constructor to initialize instance executed first, before you will be awake (the official explanation is found directly, but given the script to initialize then run, it will first execute than awake, the fact, too, if someone finds a problem and then proposed to hate each other) . But encountered bug (tight encounter once), that just starts running, constructors multiple runs in 2017unity version, the cause is not found.

(C) single-mode embodiment where the DontDestroyOnLoad

Sometimes a single case can not be loaded with scenes disappear, there has been a need, therefore need not be destroyed, but will be cases when there are two single load scenario (this is in itself a problem that already have a single case, but in case scenario loaded awake again run)

Copy the code
public class WebRequestUtility : MonoBehaviour
{
    public bool bDontDestroyOnLoad = false;

    public static WebRequestUtility Instance;

    private void InitializeInstance()
    {
        if (Instance != null & Instance == this)
            return;

        if(bDontDestroyOnLoad)
        {
            if(Instance==null)
            {
                Instance = this;
                DontDestroyOnLoad(gameObject);
            }
            else
            {
                Destroy(gameObject);
            }
        }
        else
        {
            Instance = this;
        }
    }

    private void Awake()
    {
        InitializeInstance();
    }
}
Copy the code

The code reference NetworkManager assembly from the source code of unity. The logic is simple, allowing only a single embodiment, when referring to, in addition to ensure that a single embodiment has been initialized (not empty), but also to ensure that a single embodiment == this. Otherwise it is a single case was reassigned, and that is again initialized elsewhere, since the use of the Singleton pattern which is not allowed.

(Iv) Method static properties or static method

Copy the code
public class WebRequestUtility : MonoBehaviour
{
    private static WebRequestUtility instance;
    
    public static WebRequestUtility Instance
    {
        get
        {
            if(instance==null)
            {
                instance = new WebRequestUtility();
            }

            return instance;
        }
    }
}
Copy the code

This is also a good method, but not (a) (ii) is simple and generally used when other issues need to initialize when acquiring single embodiment, for most cases of single mode (a) (ii) has enough.

(Iv) single-mode chaos Example (actually begin mutual hate)

This would like to start this part of the direct hate, but who wrote this hostility has gone, or learn the good, and let me learn calm. Continued problem of some degree of unity singleton out.

1) One embodiment variant "cases of"

From the perspective of the code he really is to be a single embodiment, but after a few lines of code into multiple cases, and then after a few lines of code, may then return a "singleton" (???) from the plurality of instances, the present directly link on hate, think about or forget. You can take a look at this article https://blog.csdn.net/qq_15267341/article/details/54232854, multi-profile, multi-clear.

2) using locks (lock)

Use the lock without any problems, but unity is not recommended. .Net again in singleton must be locked, because the more consistent use of a single thread in regular conflict, etc. problems, such as deadlocks, or logic not sort out the problem. However, unity is single threaded operation, to achieve the "asynchronous" operated by coroutine, so this problem does not exist. Of course there are problems open thread, but open thread or asynchronous operation only for pure data plane operations, because the non-main thread is not possible component operation (popular talk is the operating unity custom stuff). So when asynchronous unity or open thread, basic avoid calling a single case when the main thread in the other thread (for the game level, vr ar speaking). In a very small number have to be related to treatment (usually occurs when the callback), can also update in real-time detection to resolve. Of course, if you are a single case does not involve the unity related components operate, it will not inherit mono, you can use pure C # syntax to deal with.

Guess you like

Origin www.cnblogs.com/LiTZen/p/11851116.html