상세 컨텐츠

본문 제목

[개발일지] UI UnitWindow : NullReferenceException 오류

카테고리 없음

by 강자이 2024. 7. 9. 15:19

본문

오류내용

받아온 Unit 정보들도 잘 호출되는데, NUll오류가 남.

 

 

 

 

< 오류 이유>

1. 호출 순서 때문

- unUnit 정보가 먼저 들어옴

- 정보가 GetText((int))에 들어옴

- 그런데 Init()이 Start()에서 아직 호출되지 않음

- 따라서 Null 오류가 난 이후, Start()에서 Init()이 호출되며 값이 들어감

 

<오류 코드>

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UI_Unit : UI_Popup
{
    //enum
    enum Buttons
    {
        Btn_Close
    }

    enum Texts
    {
        TXT_Character,
        TXT_Characteristic,
        TXT_AttackPower,
        TXT_AttackPower_Increase,
        TXT_AttackSpeed,
        TXT_AttackSpeed_Increase,
        TXT_Info

    }

    enum Images
    {
        Image_Unit,
        Image_Skill,
        Image_Character        
    }
    private Unit unit;
    private void Start()
    {
        Init();

        UpdateUnitInfo(unit);
    }

    public override void Init()
    {
        base.Init();

        Bind<Button>(typeof(Buttons));
        Bind<TMP_Text>(typeof(Texts));
        Bind<Image>(typeof(Images));

        GetButton((int)Buttons.Btn_Close).gameObject.AddUIEvent((PointerEventData data) => OnButtonClicked(Buttons.Btn_Close, data));        
    }

    private void OnButtonClicked(Buttons buttonType, PointerEventData data)
    {
        switch (buttonType)
        {
            case Buttons.Btn_Close:                
                ClosePopupUI();
                break;
        }
    }

    public void UpdateUnitInfo(Unit unit)
    {
        this.unit = unit;

        GetText((int)Texts.TXT_Character).text = unit.DataHandler.Data.Name;
        GetText((int)Texts.TXT_Characteristic).text = unit.DataHandler.Data.UnitRank.ToString();
        GetText((int)Texts.TXT_AttackPower).text = unit.StatHandler.Stat.AttackPower.ToString();
        GetText((int)Texts.TXT_AttackPower_Increase).text = CalculateAttackPowerIncrease().ToString(); 
        GetText((int)Texts.TXT_AttackSpeed).text = unit.StatHandler.Stat.AttackSpeed.ToString();
        GetText((int)Texts.TXT_AttackSpeed_Increase).text = CalculateAttackSpeedIncrease().ToString(); 
        GetText((int)Texts.TXT_Info).text = unit.DataHandler.Data.Description;
    }

    private float CalculateAttackPowerIncrease()
    {
        // 공격력 증가량 계산 로직 추가
        return 0f;
    }

    private float CalculateAttackSpeedIncrease()
    {
        // 공격속도 증가량 계산 로직 추가
        return 0f;
    }
}

 

 

<해결 내용>

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UI_Unit : UI_Popup
{
    //enum
    enum Buttons
    {
        Btn_Close
    }

    enum Texts
    {
        TXT_Character,
        TXT_Characteristic,
        TXT_AttackPower,
        TXT_AttackPower_Increase,
        TXT_AttackSpeed,
        TXT_AttackSpeed_Increase,
        TXT_Info

    }

    enum Images
    {
        Image_Unit,
        Image_Skill,
        Image_Character        
    }
    private Unit unit;
    private void Start()
    {
        /*Init();
        GetText((int)Texts.TXT_AttackPower).text = unit.StatHandler.Stat.AttackRange.ToString();
        GetText((int)Texts.TXT_AttackSpeed).text = unit.StatHandler.Stat.MoveSpeed.ToString();

        UpdateUnitInfo(unit);*/

        Init();
        UpdateUI();
    }

    public override void Init()
    {
        base.Init();

        Bind<Button>(typeof(Buttons));
        Bind<TMP_Text>(typeof(Texts));
        Bind<Image>(typeof(Images));

        GetButton((int)Buttons.Btn_Close).gameObject.AddUIEvent((PointerEventData data) => OnButtonClicked(Buttons.Btn_Close, data));        
    }

    private void OnButtonClicked(Buttons buttonType, PointerEventData data)
    {
        switch (buttonType)
        {
            case Buttons.Btn_Close:                
                ClosePopupUI();
                break;
        }
    }

    public void UpdateUnitInfo(Unit unit)
    {
        this.unit = unit;
    }

    private void UpdateUI()
    {
        //부모함수에 넣으면 좋음
        GetText((int)Texts.TXT_Character).text = unit.DataHandler.Data.Name;
        GetText((int)Texts.TXT_Characteristic).text = unit.DataHandler.Data.UnitRank.ToString();
        GetText((int)Texts.TXT_AttackPower).text = unit.StatHandler.Stat.AttackPower.ToString();
        GetText((int)Texts.TXT_AttackPower_Increase).text = CalculateAttackPowerIncrease().ToString();
        GetText((int)Texts.TXT_AttackSpeed).text = unit.StatHandler.Stat.AttackSpeed.ToString();
        GetText((int)Texts.TXT_AttackSpeed_Increase).text = CalculateAttackSpeedIncrease().ToString();
        GetText((int)Texts.TXT_Info).text = unit.DataHandler.Data.Description;
    }

    private float CalculateAttackPowerIncrease()
    {
        // 공격력 증가량 계산 로직 추가
        return 0f;
    }

    private float CalculateAttackSpeedIncrease()
    {
        // 공격속도 증가량 계산 로직 추가
        return 0f;
    }
}

댓글 영역