Reflection์ ์์ฉ ๊ฐ๋ฅํ ๋ถ๋ถ์ด ๊ต์ฅํ ๋ง๋ค.
๋ง์ ๋ก์ง์ ํ์ฉํ ์ ์์ ๊ฒ ๊ฐ์์ ๊ธฐ๋๋๋ค.
<1>
using System;
using System.Reflection;
์ด๋ ๊ฒ ๋์ค์ ์จ์ฃผ์ด์ผ ์ฌ์ฉํ ์ ์๋ค.
<2>ํ ํด๋์ค๋ฅผ ์ปดํฌ๋ํธ๋ก ๊ฐ์ ธ์ค๊ธฐ
- quantity๋ผ๋ ๋ณ์๋ฅผ ๊ฐ์ง๊ณ ์๋ ์กํธ์ด๋ผ๋ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ์๋์ ๊ฐ์ด ์์ฑ
public class Songpyeon : MonoBehaviour
{
public int quantity;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;
public class Reflection : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent("Songpyeon") != null)
{
Type tp = other.GetComponent("Songpyeon").GetType();
if(gameObject.GetComponent<Songpyeon>()==null)
{
gameObject.AddComponent(tp);
}
Invoke("PlusQuantity", 0);
}
}
void PlusQuantity()
{
gameObject.GetComponent<Songpyeon>().quantity++;
}
}
์ฌ์ค์ ์๋์ ์ฝ๋๋ฅผ
Type tp = other.GetComponent("Songpyeon").GetType();
if(gameObject.GetComponent<Songpyeon>()==null)
{
gameObject.AddComponent(tp);
}
์ด๋ ๊ฒ ์์ฑํ์ฌ๋ ๋๊ฐ์ ํจ๊ณผ๊ฐ ๋์จ๋ค.
if(gameObject.GetComponent<Songpyeon>()==null)
{
gameObject.AddComponent(Type.GetType("Songpyeon"));
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;
public class Reflection : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent("Songpyeon") != null)
{
if (gameObject.GetComponent<Songpyeon>() == null)
{
gameObject.AddComponent(Type.GetType("Songpyeon"));
}
Invoke("PlusQuantity", 0);
}
}
void PlusQuantity()
{
gameObject.GetComponent<Songpyeon>().quantity++;
}
}
<3>ํ ํด๋์ค ์ธ์คํด์ค์ ์ ๊ทผํ๊ธฐ
์๋์ ๊ฐ์ด ์์ฑํ๋ฉด ํธ๋ฆฌ๊ฑฐ ์ถฉ๋์ฒด ์์ ์กํธ ํด๋์ค๊ฐ ์๋ค๋ฉด
์กํธ ํด๋์ค์ ์ธ์คํด์ค quantity ๋ณ์๊ฐ 50์ผ๋ก ๋ณ๊ฒฝ๋๋ค.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;
public class Reflection : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent("Songpyeon") != null)
{
Type tp = other.GetComponent("Songpyeon").GetType();
tp.GetField("quantity").SetValue(other.GetComponent("Songpyeon"), 50);
}
}
}
'Unity > Unity(C#์ค๊ธ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ฅ๋ฉ์๋ (0) | 2022.01.13 |
---|---|
๋ค์ํ๊ฒ ์์ฑํ ์ ์๋ ๋๋ค์ (0) | 2022.01.12 |
์ด๋ฒคํธ (0) | 2021.12.26 |
Delegate (0) | 2021.12.25 |