본문 바로가기
Unity/C# 문법

[유니티] 제네릭 문법

by Skull Crusher 2021. 5. 6.
728x90

********************************* Generic 안 썼을때 막 코드 ********************************* 

1. Stack 스택

제한적으로 접근할 수 있는 나열 구조

여러가지 아이템을 먹었을 때

먼저 먹은 아이템부터 사용하고 싶을때 ex) 카트라이더

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CStackInt
{
    int emptyStack;
    int[] items;
    int top;
    int size;

    public CStackInt(int size)
    {
        this.size = size;
        items = new int[size];
        emptyStack = -1;
        top = emptyStack;
    }

    public void Push(int item)
    {

      items[++top] = item;
    }

    public int Pop()
    {
        return items[top--];
    }

    public bool IsFull()
    {
        return (top + 1) == size;
    }

    public bool IsEmpty()
    {
        return top == emptyStack;
    }
}

public class NoneGenericStack : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

2. int 스택 생성

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CStackInt
{
    int emptyStack;
    int[] items;
    int top;
    int size;

    public CStackInt(int size)
    {
        this.size = size;
        items = new int[size];
        emptyStack = -1;
        top = emptyStack;
    }

    public void Push(int item)
    {

      items[++top] = item;
    }

    public int Pop()
    {
        return items[top--];
    }

    public bool IsFull()
    {
        return (top + 1) == size;
    }

    public bool IsEmpty()
    {
        return top == emptyStack;
    }
}

public class NoneGenericStack : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int size = 5;
        CStackInt istack = new CStackInt(size);

        for (int i = 0; i<7; i++)
        {
            if(!istack.IsFull())
            {
                istack.Push(i);
            }
            else
            {
                Debug.Log("스택이 꽉 찼습니다.!!!");
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

3. 스택 뽑기

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CStackInt
{
    int emptyStack;
    int[] items;
    int top;
    int size;

    public CStackInt(int size)
    {
        this.size = size;
        items = new int[size];
        emptyStack = -1;
        top = emptyStack;
    }

    public void Push(int item)
    {

      items[++top] = item;
    }

    public int Pop()
    {
        return items[top--];
    }

    public bool IsFull()
    {
        return (top + 1) == size;
    }

    public bool IsEmpty()
    {
        return top == emptyStack;
    }
}

public class NoneGenericStack : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int size = 5;
        CStackInt istack = new CStackInt(size);

        for (int i = 0; i<7; i++)
        {
            if(!istack.IsFull())
            {
                istack.Push(i);
            }
            else
            {
                Debug.Log("스택이 꽉 찼습니다.!!!");
            }
        }

        while (!istack.IsEmpty())
        {
            Debug.Log(istack.Pop());
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

3. float

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CStackInt
{
    int emptyStack;
    int[] items;
    int top;
    int size;

    public CStackInt(int size)
    {
        this.size = size;
        items = new int[size];
        emptyStack = -1;
        top = emptyStack;
    }

    public void Push(int item)
    {

      items[++top] = item;
    }

    public int Pop()
    {
        return items[top--];
    }

    public bool IsFull()
    {
        return (top + 1) == size;
    }

    public bool IsEmpty()
    {
        return top == emptyStack;
    }

}

//float
public class CStackFloat
{
    int emptyStack;
    float[] items;
    int top;
    int size;

    public CStackFloat(int size)
    {
        this.size = size;
        items = new float[size];
        emptyStack = -1;
        top = emptyStack;
    }

    public void Push(float item)
    {

        items[++top] = item;
    }

    public float Pop()
    {
        return items[top--];
    }

    public bool IsFull()
    {
        return (top + 1) == size;
    }

    public bool IsEmpty()
    {
        return top == emptyStack;
    }


}

public class NoneGenericStack : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int size = 5;

        //IntStackTest(size);

        FloatStackTest(size);
    }

    void FloatStackTest(int size)
    {
        CStackFloat fstack = new CStackFloat(size);

        for (int i = 0; i < 7; i++)
        {
            if (!fstack.IsFull())
            {
                float data = Random.Range(1f, 30f);

                Debug.Log("istack Push : " + i);
                fstack.Push(i);
            }
            else
            {
                Debug.Log("스택이 꽉 찼습니다.!!!");
            }
        }

        while (!fstack.IsEmpty())
        {
            Debug.Log(fstack.Pop());
        }
    }

    void IntStackTest(int size)
    {
        CStackInt istack = new CStackInt(size);

        for (int i = 0; i < 7; i++)
        {
            if (!istack.IsFull())
            {
                float data = Random.Range(1f, 30f);

                Debug.Log("istack Push : " + i);
                istack.Push(i);
            }
            else
            {
                Debug.Log("스택이 꽉 찼습니다.!!!");
            }
        }

        while (!istack.IsEmpty())
        {
            Debug.Log(istack.Pop());
        }
    }
}

 

4. string

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CStackInt
{
    int emptyStack;
    int[] items;
    int top;
    int size;

    public CStackInt(int size)
    {
        this.size = size;
        items = new int[size];
        emptyStack = -1;
        top = emptyStack;
    }

    public void Push(int item)
    {

        items[++top] = item;
    }

    public int Pop()
    {
        return items[top--];
    }

    public bool IsFull()
    {
        return (top + 1) == size;
    }

    public bool IsEmpty()
    {
        return top == emptyStack;
    }

}

//float
public class CStackFloat
{
    int emptyStack;
    float[] items;
    int top;
    int size;

    public CStackFloat(int size)
    {
        this.size = size;
        items = new float[size];
        emptyStack = -1;
        top = emptyStack;
    }

    public void Push(float item)
    {

        items[++top] = item;
    }

    public float Pop()
    {
        return items[top--];
    }

    public bool IsFull()
    {
        return (top + 1) == size;
    }

    public bool IsEmpty()
    {
        return top == emptyStack;
    }


}

//string
public class CStackString
{
    int emptyStack;
    string[] items;
    int top;
    int size;

    public CStackString(int size)
    {
        this.size = size;
        items = new string[size];
        emptyStack = -1;
        top = emptyStack;
    }

    public void Push(string item)
    {

        items[++top] = item;
    }

    public string Pop()
    {
        return items[top--];
    }

    public bool IsFull()
    {
        return (top + 1) == size;
    }

    public bool IsEmpty()
    {
        return top == emptyStack;
    }


}

public class NoneGenericStack : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int size = 5;

        IntStackTest(size);

        FloatStackTest(size);

        StringStackTest(size);
    }

    void StringStackTest(int size)
    {
        CStackString sstack = new CStackString(size);

        for (int i = 0; i < 7; i++)
        {
            if (!sstack.IsFull())
            {                
                Debug.Log("istack Push : " + "숫자" + (i + 1));
                sstack.Push("숫자" + (i + 1));
            }
            else
            {
                Debug.Log("스택이 꽉 찼습니다.!!!");
            }
        }

        while (!sstack.IsEmpty())
        {
            Debug.Log(sstack.Pop());
        }
    }

    void FloatStackTest(int size)
    {
        CStackFloat fstack = new CStackFloat(size);

        for (int i = 0; i < 7; i++)
        {
            if (!fstack.IsFull())
            {
                float data = Random.Range(1f, 30f);

                Debug.Log("fstack Push : " + data);
                fstack.Push(data);
            }
            else
            {
                Debug.Log("스택이 꽉 찼습니다.!!!");
            }
        }

        while (!fstack.IsEmpty())
        {
            Debug.Log(fstack.Pop());
        }
    }

    void IntStackTest(int size)
    {
        CStackInt istack = new CStackInt(size);

        for (int i = 0; i < 7; i++)
        {
            if (!istack.IsFull())
            {               
                Debug.Log("istack Push : " + i);
                istack.Push(i);
            }
            else
            {
                Debug.Log("스택이 꽉 찼습니다.!!!");
            }
        }

        while (!istack.IsEmpty())
        {
            Debug.Log(istack.Pop());
        }
    }
}

********************************* Generic 코드 ********************************* 

아이템 타입이 달라도 <T> 클래스 하나로 사용 가능!!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//제네릭 메소드
public class CStack<T>
{
    int emptyStack;
    T[] items;
    int top;
    int size;

    public CStack(int size)
    {
        this.size = size;
        items = new T[size];
        emptyStack = -1;
        top = emptyStack;
    }

    public void Push(T item)
    {

        items[++top] = item;
    }

    public T Pop()
    {
        return items[top--];
    }

    public bool IsFull()
    {
        return (top + 1) == size;
    }

    public bool IsEmpty()
    {
        return top == emptyStack;
    }

}

public class GenericStack : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int size = 5;
        IntStackTest(size);
        FloatStackTest(size);
        StringStackTest(size);
    }

    void StringStackTest(int size)
    {
        CStack<string> sstack = new CStack<string>(size);

        for (int i = 0; i < 7; i++)
        {
            if (!sstack.IsFull())
            {
                Debug.Log("istack Push : " + "숫자" + (i + 1));
                sstack.Push("숫자" + (i + 1));
            }
            else
            {
                Debug.Log("스택이 꽉 찼습니다.!!!");
            }
        }

        while (!sstack.IsEmpty())
        {
            Debug.Log(sstack.Pop());
        }
    }

    void FloatStackTest(int size)
    {
        CStack<float> fstack = new CStack<float>(size);

        for (int i = 0; i < 7; i++)
        {
            if (!fstack.IsFull())
            {
                float data = Random.Range(1f, 30f);

                Debug.Log("fstack Push : " + data);
                fstack.Push(data);
            }
            else
            {
                Debug.Log("스택이 꽉 찼습니다.!!!");
            }
        }

        while (!fstack.IsEmpty())
        {
            Debug.Log(fstack.Pop());
        }
    }

    void IntStackTest(int size)
    {
        CStack<int> istack = new CStack<int>(size);

        for (int i = 0; i < 7; i++)
        {
            if (!istack.IsFull())
            {
                Debug.Log("istack Push : " + i);
                istack.Push(i);
            }
            else
            {
                Debug.Log("스택이 꽉 찼습니다.!!!");
            }
        }

        while (!istack.IsEmpty())
        {
            Debug.Log(istack.Pop());
        }
    }
}

댓글