0420 Item Pick up+인벤토리 연동
2021. 4. 20. 18:29ㆍunity
using UnityEngine;
public class Hero : MonoBehaviour
{
public float speed;
public Animation anim;
public System.Action<int> OnGetItem;
private Coroutine moveRoutine;
// Start is called before the first frame update
void Start()
{
this.moveRoutine=this.StartCoroutine(this.MoveForwardImpl());
}
IEnumerator MoveForwardImpl()
{
this.anim.Play("run@loop");
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<Item>();
this.OnGetItem(item.id);
Debug.Log("Item Get!");
Destroy(item.gameObject);
this.transform.Translate(Vector3.zero*this.speed*Time.deltaTime);
this.MoveStop();
}
}
public void MoveStop()
{
if (this.moveRoutine != null)
{
this.StopCoroutine(MoveForwardImpl());
this.anim.Play("idle@loop");
}
}
}
using UnityEngine;
using UnityEngine.UI;
public class UIPopupInventory : MonoBehaviour
{
public Button btnClose;
public void Open()
{
this.gameObject.SetActive(true);
}
public void Close()
{
this.gameObject.SetActive(false);
}
}
using UnityEngine;
using UnityEngine.UI;
public class UIItem : MonoBehaviour
{
public int id;
public Image icon;
public void Init(int id, Image icon)
{
this.id = id;
this.icon = icon;
}
}
using UnityEngine;
public class Item : MonoBehaviour
{
public float speed;
public int id = 100;
// Update is called once per frame
void Update()
{
this.transform.Rotate(0, this.speed * Time.deltaTime,0);
}
}
cafe.naver.com/gameprogramming7/244
drive.google.com/file/d/1ePFQyQBOURCZfKK042XKUahABag4309P/view?usp=sharing
'unity' 카테고리의 다른 글
0421 데이터 저장 및 로딩 (0) | 2021.04.21 |
---|---|
0421 UI Stage (0) | 2021.04.21 |
0420 UI Inventory (0) | 2021.04.20 |
0419 UI (0) | 2021.04.19 |
0416 페이드 인으로 씬 전환 (0) | 2021.04.16 |