250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ใ…ฃใ„ท
  • MySQL
  • ์œ„์ž„
  • ์ปดํฌ์ง€์…˜

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

Unity/Unity(C#์ค‘๊ธ‰)

์œ ๋‹ˆํ‹ฐ Reflection ์‚ฌ์šฉํ•˜์—ฌ ํƒ€ ํด๋ž˜์Šค๋ฅผ ์ปดํฌ๋„ŒํŠธ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ, ํƒ€ ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค์— ์ ‘๊ทผํ•˜๊ธฐ

2021. 12. 15. 11:35
728x90

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);
        }
    }

}
728x90

'Unity > Unity(C#์ค‘๊ธ‰)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

ํ™•์žฅ๋ฉ”์†Œ๋“œ  (0) 2022.01.13
๋‹ค์–‘ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋žŒ๋‹ค์‹  (0) 2022.01.12
์ด๋ฒคํŠธ  (0) 2021.12.26
Delegate  (0) 2021.12.25
    'Unity/Unity(C#์ค‘๊ธ‰)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • ํ™•์žฅ๋ฉ”์†Œ๋“œ
    • ๋‹ค์–‘ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ๋žŒ๋‹ค์‹
    • ์ด๋ฒคํŠธ
    • Delegate
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”