it leader
article thumbnail










싱글턴 패털 사용법이 여러종류가 있습니다.



간단하게 싱글턴으로 만들 클래스 도입부에 싱글턴 선언을 하여 사용하는 방법



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using UnityEngine;
using System.Collections;
 
public class SingletonTest : MonoBehaviour {
 
    private static SingletonTest instance;
    private static GameObject container;
    
    public static SingletonTest GetInstance(){
 
        if(!instance){
            
            container = new GameObject();
            container.name = "SingletonTest"
            instance  = container.AddComponent(typeof(SingletonTest)) as SingletonTest;
 
        }
        
        return instance;
    }
    
    public int a;
 
 
}
 
 
//--------------------------------------------------------------------------------------------
 
 
 
using UnityEngine;
using System.Collections;
 
public class Main : MonoBehaviour {
 
    int ref;    
 
 
    void Start(){
 
    ref = SingletonTest.GetInstance().a;    
 
    }
 
}
cs




다른 방법은 템플릿화하여 싱글턴 클래스를 상속하여 사용하는 방법이 있는데


관심있으신 분들은 검색해 보길...





profile

it leader

@dev__pixui

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!