1. 개발 현황

1. HUD 활용 시 편의성 확장

- Json 데이터 크기에 따라 동적 HUD 생성 구현

2. Json 관리 추가

- Power Up 관련 Json 추가

3. HUD 동작 개선

- Awake 함수 동작 개선

 

2. 상세 개발 내용

1. HUD 활용 시 편의성 확장

- Json 데이터 크기에 따라 동적 HUD 생성 구현

2. Json 관리 추가

- Power Up 관련 Json 추가

아래의 구조체를 통해 Power Up 데이터를 제어함

[System.Serializable]
public struct JsonPowerUpData
{
    public bool Enable;
    public int SpriteId;
    public Enum.DescType DescType;

    public float[] MovementSpeed;
    public int[] Projectile;
    public float[] ProjectileSize;
    public float[] Damage;
    public float[] Speed;
    // Melee
    public float[] Range;
    // Range
    public float[] CoolTime;
    public int[] Pierce;

    // Coeffcient
    public float[] MovementSpeedCoef;
    public float[] ProjectileCoef;
    public float[] ProjectileSizeCoef;
    public float[] DamageCoef;
    public float[] SpeedCoef;
    // Melee
    public float[] RangeCoef;
    // Range
    public float[] CoolTimeCoef;
    public float[] PierceCoef;
    
}

3. HUD 동작 개선

- Awake 함수 동작 개선

1) 이유:

HUD의 경우 Json Load 이후 Template을 바탕으로 GameObject를 생성하는 동작이 이뤄져야 함.

허나, HUD가 Active가 아닌 경우, Awake 동작에서 오동작이 발생함

(1) 소스코드

public class HUDBtnItem : MonoBehaviour
{
    public int mId;

    public Image mIcon;
    public TMP_Text mTextLevel;
    public TMP_Text mTextName;
    public TMP_Text mTextDesc;
    private void Awake()
    {
        mIcon = GetComponentsInChildren<Image>()[1];

        TMP_Text[] texts = GetComponentsInChildren<TMP_Text>();
        mTextLevel = texts[0];
        mTextName = texts[1];
        mTextDesc = texts[2];

        UpdateText();
        UpdateSprite();
    }

    public void UpdateTextName()
    {
        if (mId < GameManager.instance.mJsonWeaponData.Length)
        {
            mTextName.text = GameManager.instance.mJsonTextData[(int)GameManager.instance.mSettingData.LanguageType].WeaponName[mId];
        }
        else
        {
            int idx = mId - GameManager.instance.mJsonWeaponData.Length;
            mTextName.text = GameManager.instance.mJsonTextData[(int)GameManager.instance.mSettingData.LanguageType].PerkName[idx];
        }
    }
}

(2) Object 내 연동이 이뤄지지 않음을 확인 가능

2) 기존 해결 방법

(1) 항상 Active로 존재하도록 Rect Transform의 Scale 항목을 제어함 (초기 Scale 0)

- Display가 필요한 순간 Scale을 1로 제어

    public void Show()
    {
        Time.timeScale = 0;
        isActive = true;
        mRect.localScale = Vector3.one;

        GameManager.instance.PlayEffect(true);
    }

    public void Hide()
    {
        Time.timeScale = 1;
        isActive = false;
        mRect.localScale = Vector3.zero;

        GameManager.instance.PlayEffect(false);
    }

(2) Script Execution Order을 통해 HUD Awake() 함수 동작 순서 제어

GameObject.Awake() -> Json Load

HUD...Awake() -> Json 데이터에 따라 필요한 HUD 동적 선언

 

3) 개선 방법

- 불필요한 HUD는 Disable 할 수 있도록, Awake 함수를 제거

- 대신 Template에서 연결이 필요한 항목 (e.g. Text)는 public으로 선언하여 컴파일 간 동작 순서나 Active와 무관하게 동작이 가능하도록 변경함.

public class HUDPowerUp : MonoBehaviour
{
    public GameObject mBtnTemplet;
    GameObject[] mItems;
    public TMP_Text mTextTitle;

    public void UpdateText()
    {
        mTextTitle.text = GameManager.instance.mJsonTextData[(int)GameManager.instance.mSettingData.LanguageType].HUDLevelUpTitle[0];
        for (int i = 0; i < mItems.Length; ++i)
        {
            mItems[i].GetComponent<HUDBtnPowerUp>().UpdateText();
        }
    }

    public void Init()
    {
        mItems = new GameObject[GameManager.instance.mJsonPowerUpData.Length];
        mItems[0] = mBtnTemplet;
        mItems[0].GetComponent<HUDBtnPowerUp>().Init(0);
        for (int i = 1; i < mItems.Length; ++i)
        {
            mItems[i] = Instantiate(mBtnTemplet);
            mItems[i].GetComponent<HUDBtnPowerUp>().Init(i);
            mItems[i].transform.SetParent(mBtnTemplet.transform.parent);
            mItems[i].transform.name = "BtnPowerUp" + i;
        }
    }
}

 

 

3. TODO

1. 강화 시스템 구현 완료 (골드 활용)

2. 상자 개봉 로직 추가

3. 무기 & 퍽 조합 시 무기 업그레이드 로직 추가

4. 게임 이어하기 로직 추가

5. 게임 세이브 슬롯 추가 (3개)

 

4. 참고자료

https://forum.unity.com/threads/start-vs-awake.41633/

 

Start() VS Awake()

Hi, What is diffrent between this 2 function exactly? So thanks.

forum.unity.com

https://forum.unity.com/threads/is-it-safe-to-use-awake-for-hooking-up-references.289807/

 

Is it safe to use Awake for hooking up references

Hey, I'd like to know when is it safe to connect game object references. By that i mean: Call GetComponent<T> to assign component references. Call...

forum.unity.com

https://www.reddit.com/r/Unity3D/comments/nehp9y/awake_and_getcomponent_vs_public_and_drag_androp/

 

From the Unity3D community on Reddit

Explore this post and more from the Unity3D community

www.reddit.com

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

 

Unity - Scripting API: Object.Instantiate

This function makes a copy of an object in a similar way to the Duplicate command in the editor. If you are cloning a GameObject you can specify its position and rotation (these default to the original GameObject's position and rotation otherwise). If you

docs.unity3d.com

 

 

+ Recent posts