스테이지 선택 구현코드
UNITY 가로 Scroll UI에 키 입력으로 스크롤 구현.
Unity의 스크롤은 포인터(마우스)로 이동하는데, 키보드로 스크롤 할수 있도록 구현한 코드
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class StageSelectManagerEx : MonoBehaviour {
private int clearStage = 8;
public Transform StageSelectButtonParent;
public GameObject StageSelectButtonPrefab;
public ScrollRect menuScrollRect;
public RectTransform rectTrans;
// Use this for initialization
void Start () {
clearStage = PlayerPrefs.GetInt("clearSingleStage", 1);
Button goLast = null;
for (int i = 1; i <= clearStage; i++)
{
GameObject go = (GameObject)Instantiate(StageSelectButtonPrefab);
go.GetComponentInChildren<Text>().text = i.ToString();
go.name = "Stage" + i.ToString();
go.GetComponentInChildren<Button>().onClick.AddListener(delegate { ButtonStageClick(go.name); });
go.transform.parent = StageSelectButtonParent;
if (i == clearStage)
goLast = go.GetComponentInChildren<Button>();
}
menuScrollRect.horizontalNormalizedPosition = 1.0f;
if (goLast != null)
goLast.Select();
}
void ScrollRight()
{
if (menuScrollRect.horizontalNormalizedPosition < 1.0f)
menuScrollRect.horizontalNormalizedPosition += 0.01f;
else
menuScrollRect.horizontalNormalizedPosition = 1f;
}
void ScrollLeft()
{
if (menuScrollRect.horizontalNormalizedPosition > 0.0f)
menuScrollRect.horizontalNormalizedPosition -= 0.01f;
else
menuScrollRect.horizontalNormalizedPosition = 0f;
}
void ScrollCalc()
{
//스크롤 값을 계산하여 스크롤 함.
GameObject currentSelectButton = EventSystem.current.currentSelectedGameObject;
if (currentSelectButton == null)
return;
float width_center = menuScrollRect.gameObject.transform.position.x;
float width_harf = (menuScrollRect.GetComponent<RectTransform>().rect.width / 2);
float width_min = width_center - width_harf;
float width_max = width_center + width_harf;
float buttons_spacing = currentSelectButton.transform.parent.parent.gameObject.GetComponent<GridLayoutGroup>().spacing.x;
float button_width = currentSelectButton.GetComponent<RectTransform>().rect.width;
float button_width_min = currentSelectButton.transform.position.x - (button_width / 2);
float button_width_max = button_width_min + button_width;
if (button_width_min < width_min)
ScrollLeft();
else if (button_width_max > width_max)
ScrollRight();
}
// Update is called once per frame
void Update () {
ScrollCalc();
}
void ButtonStageClick(string stageName)
{
Application.LoadLevel(stageName);
}
}
'Unity' 카테고리의 다른 글
| 플레이어의 Grid이동 효과 (0) | 2016.01.03 |
|---|---|
| 플레이어 가속도 이동 (1) | 2016.01.03 |
| UNET 정리 1 (0) | 2016.01.02 |
| UNET 동영상강좌(영어) (0) | 2015.12.12 |
| 물리관련 이동시 떨림현상 (2) | 2015.08.15 |



