以前のリビジョンの文書です
—-
Warning: Declaration of syntax_plugin_linebreak::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in
/home/stepism/www/ue4/wiki/lib/plugins/linebreak/syntax.php on line
52
Warning: Declaration of syntax_plugin_linebreak::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in
/home/stepism/www/ue4/wiki/lib/plugins/linebreak/syntax.php on line
74
Warning: Declaration of syntax_plugin_markdownextra::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in
/home/stepism/www/ue4/wiki/lib/plugins/markdownextra/syntax.php on line
38
Warning: Declaration of syntax_plugin_markdownextra::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in
/home/stepism/www/ue4/wiki/lib/plugins/markdownextra/syntax.php on line
47
Warning: Declaration of syntax_plugin_syntaxhighlighter3_syntax::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in
/home/stepism/www/ue4/wiki/lib/plugins/syntaxhighlighter3/syntax/syntax.php on line
53
Warning: Declaration of syntax_plugin_syntaxhighlighter3_syntax::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in
/home/stepism/www/ue4/wiki/lib/plugins/syntaxhighlighter3/syntax/syntax.php on line
82
051 Spriteの画面解像度対応
概要
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なので中心に。
実行結果