About
Contents
STL
Android
Eigen
enchant.js
Firefox OS
OpenGL
OpenGL ES 2.0
pukiwiki
UE4
Unity
Windows Phone
Xamarin
Materials Link
その他
PR
STL
Android
Eigen
enchant.js
Firefox OS
OpenGL
OpenGL ES 2.0
pukiwiki
UE4
Unity
Windows Phone
Xamarin
以前のリビジョンの文書です
Spriteのサイズや位置を画面解像度に合わせて調整します。
using UnityEngine; using System.Collections; public class CameraScript : MonoBehaviour { public const float PixelToUnits = 100.0f; // ゲーム内解像度 public const int BaseScreenWidth = 640; public const int BaseScreenHeight = 960; void Awake() { Camera cam = gameObject.GetComponent<Camera>(); cam.orthographicSize = BaseScreenHeight / PixelToUnits / 2; float baseAspect = (float)BaseScreenHeight / (float)BaseScreenWidth; float nowAspect = (float)Screen.height/(float)Screen.width; float changeAspect; if( baseAspect > nowAspect ) { changeAspect = nowAspect / baseAspect; cam.rect = new Rect( ( 1.0f - changeAspect ) * 0.5f, 0.0f, changeAspect, 1.0f ); } else { changeAspect = baseAspect / nowAspect; cam.rect = new Rect( 0.0f, ( 1.0f - changeAspect ) * 0.5f, 1.0f, changeAspect ); } } }
orthographicSize とrectを調整しています。これで解像度に合わせたサイズに描画されます。
座標設定用の関数を用意します。
public void SetPos ( ref GameObject _obj, float _posX, float _posY ) { float fPixelToUnits = CameraScript.PixelToUnits; float baseScreenWidth = CameraScript.BaseScreenWidth; float baseScreenHeight = CameraScript.BaseScreenHeight; Vector2 topleft = new Vector2 ( -(( baseScreenWidth * 0.5f ) / fPixelToUnits ), (( baseScreenHeight * 0.5f ) / fPixelToUnits ) ); Vector2 offset = new Vector2 ( (( _posX ) / fPixelToUnits ), -(( _posY ) / fPixelToUnits ) ); _obj.transform.position = new Vector3 ( topleft.x + offset.x, topleft.y + offset.y, 0.0f ); }
直接positionを操作するのではなく、SetPos関数を実装して設定します。 SetPos関数はSpriteの中心を(0, 0)と見立てて座標指定します。 これを任意のGameObjectから呼び出してあげます。
GameObject obj = gameObject; SetPos( ref obj, 320.0f, 32.0f ); // 画像サイズが640x32なので中心に。