Unity 3D 개인프로젝트 21.04.24.
당장 구현해야 하는 부분
- 회피시 위치이동
- 아이템 주우면 인벤토리 및 캐릭터와 연동
============================================================================
아이템 인벤토리 및 캐릭터연동을 공부하는데 잘 안 돼버렸다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestHero : MonoBehaviour
{
public float speed = 2f;
public Animation anime;
public System.Action<int> OnGetItem;
private Coroutine moveRoutine;
void Start()
{
this.moveRoutine = this.StartCoroutine(MoveForwardImpl());
}
public void Stop()
{
if(this.moveRoutine != null)
{
this.StopCoroutine(this.moveRoutine);
}
}
IEnumerator MoveForwardImpl()
{
while (true)
{
this.transform.Translate(Vector3.forward * this.speed * Time.deltaTime);
yield return null;
}
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Item")
{
var item = other.GetComponent<TestItem>();
this.OnGetItem(item.id);
Destroy(item.gameObject);
}
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestItem : MonoBehaviour
{
public int id = 100;
public float speed = 10f;
// Update is called once per frame
void Update()
{
this.transform.Rotate(0, this.speed * Time.deltaTime, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UITestItem : MonoBehaviour
{
public int id;
public Image icon;
public void Init(int id, Sprite icon)
{
this.id = id;
this.icon.sprite = icon;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TEST : MonoBehaviour
{
public Button open;
public Button close;
public UITestInven inven;
public TestHero hero;
// Start is called before the first frame update
void Start()
{
this.hero.OnGetItem = (id) =>
{
Debug.LogFormat("{0}을 획득했습니다.", id);
this.inven.AddItem(id);
this.hero.Stop();
};
this.inven.Close();
this.open.onClick.AddListener(() =>
{
this.inven.gameObject.SetActive(true);
});
this.close.onClick.AddListener(() =>
{
this.inven.gameObject.SetActive(false);
});
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
public class UITestInven : MonoBehaviour
{
public SpriteAtlas atlas;
public GameObject[] empty;
public GameObject prefab;
List<UIItem> items = new List<UIItem>();
public void Open()
{
this.gameObject.SetActive(true);
}
public void Close()
{
this.gameObject.SetActive(false);
}
public void AddItem(int id)
{
var parent = this.empty[this.items.Count].transform;
var go = Instantiate<GameObject>(this.prefab, parent);
var uiItem = go.GetComponent<UITestItem>();
var icon = this.atlas.GetSprite("inventory_item_sword_A");
uiItem.Init(id, icon);
}
}
일단은 어떤 원리로 돌아가는지 했다. 오류가 났지만 이해는 했으니 테이블도 있는 실전에 적용해보자.
(오류는 인벤토리에서 AddItem하면서 uiItem.Init에서 발생했다.)
============================================================================
Item이란 빈 오브젝트를 생성하여 그 안에 보여줄 물체를 넣어두고 Item의 태그를 Item으로 바꿔주었다.
그리고 HeroController에서 OnTriggerEnter을 이용하여 둘이 부딪히면 아이템이 사라지게 했다. 아직은 데이터 테이블이 없어서 여러 에러가 나지만 테이블을 만들고 실행한다면 정상작동 할 듯 하다.
-데이터 테이블 만들기
-먹을때 OnGetItem대리자로 아이템의 id를 구하고 그 id의 해당되는 아이템을 캐릭터의 오른손에 쥐어주기 (오른손의 위치는 스크립트에 바인딩 시키자.)
-회피기는 뒤쪽으로 (3정도?) 적당한 속도로 이동시키게 수정하자. 그리고 아이템을 먹으면 인벤토리에 객체를 띄우는데 그 이미지는 스프라이트아틀라스를 이용하고 아이디와 수량은 전부 코드로 넘겨주자. 버튼도 하나 달아서 클릭하면 간략한 정보가 뜨게 할 예정이다.
이걸 다 하고 시간이 넉넉하다면
-로그인 구현
-적과의 싸움 구현
-애니메이션 이용해서 으로 스킬 애니메이션 되면 구현