单例模式

79 阅读1分钟
#region 单例模式
    public static MonoTrail Instance;
    private void Awake()
    {
        if (Instance != null) Destroy(gameObject);
        Instance = this;
    }
#endregion

#region 单例模式
    private static Chest _instance;
    public static Chest Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = GameObject.Find("ChestPanel").GetComponent<Chest>();
            }
            return _instance;
        }
    }
#endregion
    
    
    //使用
    if (Input.GetKeyDown(KeyCode.T))
        {
            Knapsack tempKnapsack = Knapsack.Instance;
            tempKnapsack.DisplaySwitch();
        }