public interface IItem{

void Use(GameObject target);

}


์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ  useํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ์–ด.



public class Ammo Pack : MonoBehavior, IItem{

public int ammo =30;


public void Use(GameObject target){

Debug.Log("ํƒ„์•Œ์ด ์ฆ๊ฐ€ํ–ˆ๋‹ค");

}

}


public class healthpack : MonoBehavior, IItem{

public float health = 50;


public void Use(GameObject target){

Debug.Log("์ฒด๋ ฅ์ด ์ฆ๊ฐ€ํ–ˆ๋‹ค");

}

}


์ด๋ ‡๊ฒŒ ๊ฐ ํด๋ž˜์Šค์— IItem์„ ์ƒ์†ํ•ด์ฃผ๋Š”๊ฑฐ ๊นŒ์ง€๋Š” ์ดํ•ด๊ฐ€ ๊ฐ€.ย 



void OntriggerEnter(Collider other){

IItem item = other.GetComponent<IItem>();

if(item != null){

item.use();

}

}


์ด๋ถ€๋ถ„์ด ์ดํ•ด๊ฐ€ ์•ˆ๋˜๋Š”๋ฐ...

์ถฉ๋Œ์„ ํ•ด์„œ ๋‹ค๋ฅธ ์˜ค๋ธŒ์ ํŠธ์˜ย  Iitem ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค๋Š”๊ฑฐ ์•„๋‹ˆ์•ผ...ย 

๊ทผ๋ฐ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ปดํฌ๋„ŒํŠธ์— ์ถ”๊ฐ€ ๋ชปํ•˜์ง€ ์•Š์•„?ย 

๊ทธ๋ฆฌ๊ณ  ์ถ”๊ฐ€ํ• ์ˆ˜ ์žˆ๋‹ค๊ณ  ์น˜๊ณ  IItem์„ ์‹คํ–‰ํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ....

๊ทธ๊ฒŒ ammo์ธ์ง€ health pack์ธ์ง€ ์–ด๋–ป๊ฒŒ ๊ตฌ๋ณ„ํ•˜๋Š”๊ฑฐ์•ผ?ย 

IItem ์ธํ„ฐํŽ˜์ด์Šค์— ๋“ค์–ด์ž‡๋Š”๊ฑดย void Use(GameObject target); ์ด๊ฒƒ๋ฟ์ด์ž–์•„.