Unity NGUI 초기설정 / 화면 맞추기
Unity 2016. 5. 17. 00:24 |NGUI를 처음 접하면서 화면 구성을 하고 나서 핸드폰에 적용하면
틀어 지는 현상이 나온다.
내가 기존에 2D를 작업 하던 사이즈는
800 X 1280 이다. (세로)
여기서 NGUI를 적용할려고 자료를 찾아 보니 결국
스크립트 수정을 해줘야 하는것으로 보여 진다.
아래와 같이 수정하니 문제 없이 된다.
1. NGUI 의 UI 스크립트 UIRoot.cs 열기
2. Inspector Window 에서 설정
Scaling Style - constrained On Mobiles
Content Width - 800 Fit Check
Content Height - 1280 Fit Check
3. UIRoot.cs 스크립트의 Update() 함수에서 아레 부분을 수정한다.
mTrans.localScale = new Vector3(size, size, size);
>>>
float fX = (NGUITools.screenSize.x / 800f);
float fTemp = (1280f * fX);
float fY = NGUITools.screenSize.y / fTemp;
mTrans.localScale = new Vector3(size, size * fY, size);
이상입니다.
거열산성 하부약수터
내 일상 2016. 4. 20. 21:12 |요즘 날씨가 좋으면 무조건 하루에 한번 하부약수터를 간다.
올라 갈때는 조금 힘들지만 도착한 약수터 풍경은 매일 나한테 좋은 생각들을 만들어 준다.
Prefab 객체 Button을 Canvas 자식 객체로 동적 생성하기
Unity 2016. 4. 20. 21:04 |Button을 Prefab로 등록한후 게임상에서 환경에 따라서 Button을 여러개 생성해야할 경우가 생긴다.
부모가 되는 GameObject의 Transform 객체 하부로 연결하면 된다.
> GameController 는 Main Camera 에 스크립트로 등록한다.
> uiRoot는 UI의 최상의 Canvas 객체이다.
> tileButton는 Prefab로 등록되어 있는 버튼 객체이다.
Start() 함수에서
Instantiate (tileButton); 이 코드만 실행하면 Root의 자식으로 생기기 때문에
원래 의도한 Canvas의 자식으로 생성되지 않는다.
아래와 같은 코드로 하게 되면 자식으로 등록이 된다.
-- 코드 --
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameController : MonoBehaviour {
public GameObject uiRoot;
public Button tileButton;
// Use this for initialization
void Start () {
Button child = Instantiate (tileButton);
child.transform.parent = uiRoot.transform;
}
// Update is called once per frame
void Update () {
}
}
Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which can prevent common UI scaling issues.
이 경우 SetParent()를 사용하면 된다.
child.transform.parent = uiRoot.transform;
child.transform.SetParent (uiRoot.transform);
[Unity 유니티] 안드로이드 Back 버튼 처리하기
Unity 2016. 4. 16. 12:48 |안드로이드에서 Back 버튼에 대한 이벤트는 아래와 같이 처리 하면된다.
if( Input.GetKeyDown( KeyCode.Escape ) )
{
// 처리할 로직을 여기에 넣으면 된다.
}
Update()에서 값을 체크 해서 사용하면 된다.