본문 바로가기
개발/C#

부분 클래스

by 남생이야 2024. 5. 8.

 

  C# 2.0 이후에 class 바로 앞에 "partiial" 키워드를 사용해 부분 클래스를 선언할 수 있다. 부분 클래스는 여러 파일에 걸쳐서 클래스를 분할하는데 사용한다. 이렇게 사용하게 되면 코드 생성 및 수정에 유용해진다. 

 

다음은 Player 클래스를 정의하는데 partial을 사용한 예시이다. Player.cs는 코드 일부만 첨가했다. 

Player.cs 

더보기
public partial class Player : MonoBehaviour
{
    [SerializeField]
    private float walkSpeed = 2.0f; // 걷는 속도

    [SerializeField]
    private float runSpeed = 4.0f;

    [SerializeField]
    private float rotateSpeed = 2.0f;

    [SerializeField]
    private GameObject particlePrefab;

    [SerializeField]
    private GameObject weaponPrefab;
    private GameObject weaponDestination;

    private IWeapon weapon;

    private Transform holsterTransform;
    private Transform handTransform; 

    private Animator animator;

    private void Awake()
    {
        animator = GetComponent<Animator>();
    }

    private void Start()
    {  
        // SearchTransform(transform);

        holsterTransform = transform.FindChildByName("Holster_Sword");
        handTransform = transform.FindChildByName("Hand_Sword");

        if (weaponPrefab != null)
        {
            weaponDestination = Instantiate<GameObject>(weaponPrefab, holsterTransform);
            weapon = weaponDestination.GetComponent<IWeapon>();
        }
    }

    private void SearchTransform(Transform parent)
    {
        print(parent.name);

        for(int i = 0; i < parent.childCount; i++)
            SearchTransform(parent.GetChild(i));
    }


    Vector3 direction;

    private void Update()
    {
        UpdateMoving();
        //UpdateRotating();
        UpdateRaycast();
        UpdateDrawing();
        UpdateAttacking();
    }

    private bool bCanMove = true; 

    private void UpdateMoving()
    {
        if (bCanMove == false)
            return; 

        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        bool bRun = Input.GetButton("Run");

        float speed = bRun  == true ? runSpeed : walkSpeed;

        direction = Vector3.forward * vertical + Vector3.right * horizontal;
        direction = direction.normalized * speed ;

        this.transform.Translate(direction * Time.deltaTime);

        animator.SetFloat("SpeedZ", direction.magnitude);
    }

    private void UpdateRotating()
    {
        float rotY = Input.GetAxis("Mouse X");

        transform.Rotate(0, rotY * rotateSpeed * Time.deltaTime, 0);
    }

 

 

  Player_Animation.cs 클래스는 Player 클래스의 부분 클래스이다. 이 클래스는 Player에서 애니메이션에 대한 기능만을 정의하는 용도로 선언되었다. 

Player_Animation.cs 

더보기
using UnityEngine;

public partial class Player 
{
    // ctrl m m ctrl m l ctrl m o
    #region Drawing
    private bool bDrawing = false;
    private bool bSheething = false; 
    private bool bEquipped = false;

    private void UpdateDrawing()
    {
        if (Input.GetButtonDown("WeaponEquip") == false)
            return;

        if (bDrawing == true)
            return;

        if (bSheething == true)
            return;

        if (bEquipped == false)
        {
            bDrawing = true;
            animator.SetBool("IsEquip", bDrawing);

            return;
        }

        bSheething = true;
        animator.SetBool("IsUnequip", bSheething);
    }

    private void Begin_Equip()
    {
        // 자식 오브젝트 빼기 
        weaponDestination.transform.parent.DetachChildren();
        weaponDestination.transform.position = Vector3.zero;
        weaponDestination.transform.rotation = Quaternion.identity;
        weaponDestination.transform.localScale = Vector3.one;

        // 손에 오브젝트에 붙으면 월드 간격을 다시 초기화 해준다. 
        weaponDestination.transform.SetParent(handTransform, false);
    }

    private void End_Equip()
    {
        bEquipped = true;

        bDrawing = false; 
        animator.SetBool("IsEquip", false);
    }

    private void Begin_Unequip()
    {
        // 자식 오브젝트 빼기 
        weaponDestination.transform.parent.DetachChildren();
        weaponDestination.transform.position = Vector3.zero;
        weaponDestination.transform.rotation = Quaternion.identity;
        weaponDestination.transform.localScale = Vector3.one;

        // 손에 오브젝트에 붙으면 월드 간격을 다시 초기화 해준다. 
        weaponDestination.transform.SetParent(holsterTransform, false);
    }

    private void End_Unequip()
    {
        bEquipped = false;

        bSheething  =false;
        animator.SetBool("IsUnequip", false);
    }

    #endregion

    #region CreateWeapon
    private void UpdateCreateWeapon()
    {
        if (Input.GetButtonDown("WeaponCreate") == false)
            return; 



    }
    #endregion

    #region Attacking
    private bool bAttacking = false;
    private void UpdateAttacking()
    {
        if (Input.GetButton("Attack") == false)
            return;

        if (bDrawing || bSheething)
            return;

        if (bEquipped == false)
            return;

        // 트리거 같은 경우는 한 번 입력한게 남아있기때문에 버그가 남는다. 이걸 방지 위한
        // 아래 코드로 방지 
        if (bAttacking == true)
           return;

        bCanMove = false;

        bAttacking = true;
        animator.SetBool("IsAttacking", true);
        //animator.SetTrigger("Attacking");
    }

    private void End_Attack()
    {
        bCanMove = true;
        bAttacking = false;
        animator.SetBool("IsAttacking", false);
    }

    private void Begin_Collision()
    {
        weapon.Begin_Collision();
    }

    private void End_Collision()
    {
        weapon.End_Collision();
    }

    #endregion
}

 

'개발 > C#' 카테고리의 다른 글

[C#] 인터페이스(Interface)  (0) 2024.05.14
[C#]상속  (0) 2024.05.11
확장 메소드  (0) 2024.05.08
전처리기  (0) 2024.05.08
Particle  (0) 2024.04.28