[Unity] μ½”λ“œ λ‚΄ νž™ λ©”λͺ¨λ¦¬ μ΅œμ ν™”

2023. 6. 3. 21:29Β·πŸ“ Game/✏ Unity

μ½”λ“œ λ‚΄ νž™ λ©”λͺ¨λ¦¬ μ΅œμ ν™”

ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“€ λ•Œ νž™ λ©”λͺ¨λ¦¬ μ˜μ—­μ€ ν”„λ‘œκ·Έλž˜λ¨Έκ°€ 항상 μ‹ κ²½ 써야 λ˜λŠ” μ€‘μš”ν•œ λ©”λͺ¨λ¦¬ μ˜μ—­μ΄λ‹€.  

μœ λ‹ˆν‹°μ—μ„œ νž™ λ³€μˆ˜κ°€ 생성될 λ•Œ μ—¬μœ  λ©”λͺ¨λ¦¬κ°€ μΆ©λΆ„ν•˜μ§€ μ•Šλ‹€λ©΄ GC을 μˆ˜ν–‰ν•œλ‹€.
ν•˜μ§€λ§Œ GC은 κ½€λ‚˜ 무거운 μž‘μ—…μ΄κΈ° λ•Œλ¬Έμ— 이 μˆœκ°„ ν”„λ ˆμž„ λ“œλ‘­μ΄ λ°œμƒν•œλ‹€.


이λ₯Ό κ°œμ„ ν•˜κΈ° μœ„ν•΄ μ΅œκ·Όμ—λŠ” Inscremental GC κΈ°λŠ₯이 μ—…λ°μ΄νŠΈλ˜κΈ΄ ν•˜μ˜€μ§€λ§Œ,

κ·Έλž˜λ„ GC의 λŒ€μƒμ΄ λ˜λŠ” νž™ 할당을 μ΅œλŒ€ν•œ λ§Œλ“€μ§€ μ•ŠλŠ” 것이 μ’‹λ‹€.

 

1) 자주 μ‚¬μš©λ˜λŠ” μ°Έμ‘° λ³€μˆ˜λŠ” μΊμ‹±ν•˜κΈ°

private Button btn;

void Start()
{
    btn = GetComponent<Button>();
}

public void MyMethod()
{
    //Button btn = GetComponent<Button>(); // ν•¨μˆ˜ ν˜ΈμΆœμ‹œ λ§ˆλ‹€ νž™ λ©”λͺ¨λ¦¬λ₯Ό ν• λ‹Ήν•œλ‹€.
    SomeMethod(btn);
}

 

2) μ»¬λ ‰μ…˜μ„ μƒˆλ‘œ λ§Œλ“€ λ•Œ λ°œμƒν•˜λŠ” κ°€λΉ„μ§€

private List<int> myList = new List<int>();

public void Update()
{
    //List<int> myList = new List<int>(); // λ§€ ν”„λ ˆμž„ μƒˆ μ»¬λ ‰μ…˜μ„ μƒμ„±ν•˜κ³  μžˆλ‹€.
    myList.Clear(); // μ»¬λ ‰μ…˜μ„ Clear()둜 λ‚΄μš©μ„ λΉ„μ›Œμ„œ μ‚¬μš©ν•˜λ©΄ κ°€λΉ„μ§€λ₯Ό 쀄일 수 μžˆλ‹€.
    SomeMethod(myList);
}

 

3) λ¬Έμžμ—΄ μ—°κ²° μ‹œ λ°œμƒν•˜λŠ” κ°€λΉ„μ§€

2023.03.18 - [πŸ“ Language/✏ C#] - [C#] string 객체 μ‚¬μš©μ—μ„œ ν”νžˆ ν•˜λŠ” μ‹€μˆ˜

string name = "Mark";
string str = "My Name : ";
str += name;

 

4) Unity ν•¨μˆ˜ ν˜ΈμΆœμ—μ„œ λ°œμƒν•˜λŠ” κ°€λΉ„μ§€

public void MyMethod()
{
    Vector3[] normals = myMesh.normals;
    for (int i = 0; i < normals.Length; i++)
    {
        //Vector3 normal = myMesh.normals[i]; //배열을 λ°˜ν™˜ν•˜λŠ” Unity ν•¨μˆ˜μ— μ•‘μ„ΈμŠ€ ν•  λ•Œλ§ˆλ‹€ μƒˆ 배열이 μƒμ„±λ˜μ–΄ λ°˜ν™˜λœ κ°’μœΌλ‘œ μ „λ‹¬λœλ‹€.
        Vector3 normal = normals[i];
    }
}
string playerTag = "Player";

void OnTriggerEnter(Collider other)
{
    //if(other.gameObject.tag == "Player") // GameObject.tagμ΄λ‚˜ GameObject.name μ ‘κ·Ό μ‹œμ—λ„ 맀번 λ¬Έμžμ—΄μ„ μƒμ„±ν•΄μ„œ λ°˜ν™˜ν•œλ‹€.
    if(other.gameObject.CompareTag(playerTag))
    {
        //...
    }
}

 

5) μ½”λ£¨ν‹΄μ—μ„œ λ°œμƒν•˜λŠ” κ°€λΉ„μ§€

WaitForSeconds delay = new WaitForSeconds(1f);

While(true)
{
    //yield return 0; // 0인 int κ°’ λ³€μˆ˜κ°€ boxing되기 λ•Œλ¬Έμ— κ°€λΉ„μ§€λ₯Ό λ°œμƒμ‹œν‚¨λ‹€.
    //yield return new WaitForSeconds(1f); // 맀번 μƒˆλ‘œμš΄ 객체가 μƒμ„±λœλ‹€.
    yield return delay;
}

 

https://mentum.tistory.com/538

 

μœ λ‹ˆν‹° μ΅œμ ν™” : μ½”λ“œ λ‚΄ νž™ λ©”λͺ¨λ¦¬ μ΅œμ ν™”

νž™ λ©”λͺ¨λ¦¬ μ˜μ—­ ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“€ λ•Œ νž™ λ©”λͺ¨λ¦¬ μ˜μ—­μ€ ν”„λ‘œκ·Έλž˜λ¨Έκ°€ 항상 μ‹ κ²½ 써야 λ˜λŠ” μ€‘μš”ν•œ λ©”λͺ¨λ¦¬ μ˜μ—­μ΄λ‹€. μœ λ‹ˆν‹°μ—μ„œ νž™ λ³€μˆ˜κ°€ 생성될 λ•Œ μ—¬μœ  λ©”λͺ¨λ¦¬κ°€ μΆ©λΆ„ν•˜μ§€ μ•Šλ‹€λ©΄ κ°€λΉ„μ§€ 컬렉

mentum.tistory.com

μ €μž‘μžν‘œμ‹œ (μƒˆμ°½μ—΄λ¦Ό)
'πŸ“ Game/✏ Unity' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • [Unity] null 체크
  • [Unity] Mathf.Lerp() ν•¨μˆ˜
  • [Unity] ν™œμš© κ°€λŠ₯ν•œ AI μ½”λ”© μ„œλΉ„μŠ€ 2편
  • [Unity] ν™œμš© κ°€λŠ₯ν•œ AI μ½”λ”© μ„œλΉ„μŠ€ 1편
Blxxming
Blxxming
CS 지식과 κ³΅λΆ€ν•˜λ‹€ 배운 것, κ²½ν—˜ν•œ 것 등을 κΈ°λ‘ν•˜λŠ” λΈ”λ‘œκ·Έμž…λ‹ˆλ‹€.
  • Blxxming
    πŸ’‘λ²ˆλœ©πŸ’‘
    Blxxming
  • 전체
    였늘
    μ–΄μ œ
  • 곡지사항

    • Tech Interview
    • πŸ“š Tech (246)
      • πŸ“ Computer Science (96)
        • ✏ OS (12)
        • ✏ Network & Web (10)
        • ✏ Database (11)
        • ✏ Data Structure (6)
        • ✏ Algorithm (40)
        • ✏ Design Pattern (9)
        • ✏ Cloud Computing (3)
        • ✏ (5)
      • πŸ“ Language (73)
        • ✏ Language (6)
        • ✏ C & C++ (11)
        • ✏ C# (19)
        • ✏ JAVA (37)
      • πŸ“ Game (43)
        • ✏ Computer Graphics (2)
        • ✏ Unity (14)
        • ✏ Unreal (26)
        • ✏ (1)
      • πŸ“ Book (34)
        • ✏ Effective (3)
        • ✏ Game Server (16)
        • ✏ Clean Code (14)
        • ✏ (1)
  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.0
Blxxming
[Unity] μ½”λ“œ λ‚΄ νž™ λ©”λͺ¨λ¦¬ μ΅œμ ν™”
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”