λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Unity

λ©”μ†Œλ“œ μ •μ˜ μ‹œ κ³ λ €ν•  점

by γ…Ž-γ…Ž 2022. 12. 28.

μ•„λž˜μ²˜λŸΌ μ½”λ“œλ₯Ό λ§Œλ“€μ—ˆλ‹€.

public class SwappableTile : Tile
{    
    private Vector3 mousePosition;

    private void GetMousePosition()
    {
        mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        mousePosition.z = 0;
    }
}

κ³ λ €ν•΄ λ³Ό 사항이 μ—¬λŸΏ μžˆλ‹€.

 

1. 순수 ν•¨μˆ˜μ΄κ±°λ‚˜ μž¬μ‚¬μš©λ˜λŠ” 경우 λ³„λ„μ˜ λ©”μ†Œλ“œλ‘œ 뢄리할 κ°€μΉ˜κ°€ μžˆλ‹€.

- 순수 ν•¨μˆ˜: 같은 인자 값을 λ°›μ•˜μ„ λ•Œ μ–Έμ œλ‚˜ 같은 λ°˜ν™˜ 값을 λŒλ €μ£ΌλŠ” ν•¨μˆ˜

- 반면 순수 ν•¨μˆ˜κ°€ μ•„λ‹Œ ν•¨μˆ˜, 예λ₯Ό λ“€λ©΄ λΆ„λ¦¬ν•œ λ©”μ†Œλ“œ λ‚΄μ˜ λ³€μˆ˜λ“€μ„ μ‚¬μš©ν•˜μ§€ μ•Šκ³  클래슀의 멀버 λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” 경우 ꡳ이 λ³„λ„μ˜ λ©”μ†Œλ“œλ‘œ λΆ„λ¦¬ν•˜μ§€ μ•ŠλŠ” 것이 λ‚«λ‹€.(의미 μ—†λŠ” λ©”μ†Œλ“œ λΆ„λ¦¬λŠ” μ°¨ν›„ μ½”λ“œ μΆ”μ λ§Œ μ–΄λ ΅κ²Œ ν•œλ‹€.)

- μœ„μ˜ GetMousePosition은 λ©”μ†Œλ“œ λ°”κΉ₯의 클래슀 멀버 λ³€μˆ˜μ— μ ‘κ·Όν•˜μ—¬ μ‚¬μš©ν•˜κ³  μžˆμœΌλ―€λ‘œ 순수 ν•¨μˆ˜κ°€ μ•„λ‹ˆλ‹€.

 

 

ν•˜μ§€λ§Œ 사싀 SwappableTile 클래슀 μ•ˆμ—μ„œ mousePositon을 μ–»μ–΄μ˜€λŠ” 뢀뢄이 두 ꡰ데 μžˆλ‹€. κ·Έλ ‡λ‹€λ©΄ 차라리 μ € 2쀄 짜리 μ½”λ“œλ₯Ό 두 번 μž…λ ₯ν•˜λ”λΌλ„ 별도 λ©”μ†Œλ“œλ‘œ λΆ„λ¦¬ν•˜λŠ” 것이 λ‚˜μ„κΉŒ?

그것은 μ•„λ‹ˆλ‹€. κ·Έλ ‡μ§€λ§Œ...

 

 

2. "Get~~", "Set~~"은 관둀적인 μ˜λ―Έκ°€ μžˆμœΌλ―€λ‘œ μ£Όμ˜ν•΄μ•Ό ν•œλ‹€.

- ν†΅μƒμ μœΌλ‘œ Get~~은 μ–΄λ–€ 값을 returnν•  κ²ƒμ²˜λŸΌ 보인닀.

- 그리고 Set~~은 μ–΄λ–€ μž…λ ₯ νŒŒλΌλ―Έν„°λ₯Ό λ°›μ•„μ„œ λ³€μˆ˜μ— 값을 섀정해쀄 κ²ƒμ²˜λŸΌ 보인닀.

- μœ„μ˜ GetMousePosition은 사싀 mousePosition을 μ„€μ •ν•˜κ³  μžˆμœΌλ―€λ‘œ Set에 더 가깝닀. κ·ΈλŸ¬λ‚˜ 관둀적인 Set λ©”μ†Œλ“œμ˜ κ·œμΉ™μ„ μœ„ν•΄ μž…λ ₯ νŒŒλΌλ―Έν„°λ₯Ό μΆ”κ°€λ‘œ λ°›μ•„μ•Ό ν•œλ‹€λ©΄...

public class SwappableTile : Tile
{    
    private Vector3 mousePosition;

    private void Update()
    {
        // μ΄λ ‡κ²Œ μ‚¬μš©
        SetMousePosition(Input.mousePosition);
    }

    private void SetMousePosition(Vector3 mouseScreenPosition)
    {
        mousePosition = Camera.main.ScreenToWorldPoint(mouseScreenPosition);
        mousePosition.z = 0;
    }
}

 

 

κ·Έλž˜λ„ 사싀 λͺ…λ£Œν•˜μ§€ μ•Šλ‹€. κ·Έλž˜μ„œ...

 

 

3. λ©”μ†Œλ“œμ˜ λ‚΄μš©μ„ μ΄λ¦„μœΌλ‘œ μœ μΆ” κ°€λŠ₯ν•˜κ²Œ ν•œλ‹€.

- λ©”μ†Œλ“œλ₯Ό μ‚΄νŽ΄λ³΄μ§€ μ•Šκ³  μ΄λ¦„λ§ŒμœΌλ‘œ 무슨 μž‘μ—…μ΄ μˆ˜ν–‰λ˜λŠ”μ§€ μœ μΆ”ν•  수 μžˆλ„λ‘ λ§Œλ“œλŠ” 것이 κ°€μž₯ μ’‹λ‹€.

public class SwappableTile : Tile
{
    private Vector3 mousePosition;

    private void Update()
    {
        //μ΄λ ‡κ²Œ μ‚¬μš©
        mousePosition = GetMouseWorldPosition();
    }

    private Vector3 GetMouseWorldPosition()
    {
        Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        worldPosition.z = 0;

        return worldPosition;
    }
}

 

 

이제 λ©”μ†Œλ“œλ„ λΆ„λ¦¬ν•˜κ³ , Get의 관둀적인 κ·œμΉ™(return)을 μ§€ν‚€λ©΄μ„œ 무슨 λ©”μ†Œλ“œμΈμ§€ μœ μΆ” κ°€λŠ₯ν•˜κ²Œ λ˜μ—ˆλ‹€.

 

 

 

 

 

μ°Έμ‘°

https://jeong-pro.tistory.com/23

 

순수 ν•¨μˆ˜λž€? (ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μ˜ 뿌리, ν•¨μˆ˜μ˜ λΆ€μˆ˜νš¨κ³Όλ₯Ό μ—†μ•€λ‹€)

ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ° ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ° : λΆ€μˆ˜ 효과λ₯Ό μ—†μ• κ³  순수 ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄ λͺ¨λ“ˆν™” μˆ˜μ€€μ„ λ†’μ΄λŠ” ν”„λ‘œκ·Έλž˜λ° νŒ¨λŸ¬λ‹€μž„* λΆ€μˆ˜ 효과 = μ™ΈλΆ€μ˜ μƒνƒœλ₯Ό λ³€κ²½ν•˜λŠ” 것 λ˜λŠ” ν•¨μˆ˜λ‘œ λ“€μ–΄μ˜¨ 인자

jeong-pro.tistory.com

 

λŒ“κΈ€