본문 바로가기

유니티 프로그래밍

유니티 방탈출 게임에서 사물에 마우스를 갖다대면 텍스트 뿌려주기

사실 간단한 기능이지만, 기본이므로 나중에 자주 쓰이니 소스를 기록해 둠

 

 

1. 부모 클래스

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


namespace RoomEscape
{
	public class SceneObject : MonoBehaviour // 자식 객체들은 이 부모 클래스를 이전 받음
	{
		public RectTransform _infoTextTrans; // 유니티에서 표시해줄 텍스트 객체를 드래그앤 드랍해주기
		public string _infoText; // 각각의 텍스트들은 자식 객체에 유니티에서 적어놔줘서 저장해두기
		void Start()
		{
		}
		void Update()
		{
		}
		private void OnMouseEnter()
		{
			_infoTextTrans.gameObject.SetActive(true);
			Text text = _infoTextTrans.GetComponent<Text>();
			text.text = _infoText;

		}
		private void OnMouseExit()
		{
			_infoTextTrans.gameObject.SetActive(false);
		}
	}
}

 

 

2. 자식 클래스

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace RoomEscape
{
	public class BookShelves : SceneObject // 각각의 오브젝트에 실제적으로 붙여주는 컴포넌트 소스
	{
    }
}