Unity 싱글톤 모드 사용

**

Unity 싱글톤 패턴

**
싱글톤 모드: 클래스를 설계할 때 전체 프로그램 동안 하나의 인스턴스 개체만 존재하도록 해야 합니다.

using UnityEngine;
Public abstract class SingLeton<T>:MonoBehaviour where T :MonoBehaviour
{
    
    
     private static T _instance;
     public static T Instance
     {
    
    
           get
            {
    
    
                returen _instance;
            }
     }

     protected virtual void Awake()
     {
    
    
           _instance = this as T;
     }
     
     //或者
     //protected abstract void Awake();
}

다른 스크립트의 클래스에서 참조됨

//实现抽象类
Public class EnemyEavm:SingLeto<EnemyEavm>

//或者
protected override void Awake()
{
    
    
   throw new system.NotImpCementedException();
}

다른 스크립트는 이 함수를 사용하여 이 스크립트의 메서드를 호출할 수 있습니다.

脚本名.Instance.方法

추천

출처blog.csdn.net/qq_45598937/article/details/126145244