1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Unity 2Dゲーム作成時 備忘録

Last updated at Posted at 2023-01-14

随時追記

アスペクト比とCanvasの設定 (最初に行う)

  • [Game]ビューで画面のアスペクト比を選択(無い場合は自分で入力して設定)
  • [Create]>[UI]>[Canvas]
  • CanvasのInspector
    • [RenderMode]を[ScreenSpace-Camera]にしてMainカメラをD&D
    • [CanvasScaler(Script)]の[UI Scale Mode]>[Scale With Screen Size]にして、上記のアスペクトに変更
    • [ScreenMatchMode]を[Expand]に変更

フォルダ構成例

  • [Prefabs]
  • [Resources]
    • [Images]
  • [Scenes]
  • [Scripts]

オブジェクトに重力を付ける

Rigidbody2D

当たり判定

collider

※タイルマップは以下
tilemap collider

Unityにログ出力

Debug.Log("test");

ゲームオブジェクトの取得

方法1.Findで取得。ただし、そのオブジェクトが非アクティブの際は失敗しNullが返る

public GameObject GO;
GO=GameObject.Find("GameManager")

方法2.publicで宣言し、unityのインスペクターでアタッチする。

オブジェクト非表示

GameObject.GetComponent<SpriteRenderer>().enabled=false;

GameObject.SetActive(false)だとfindで失敗する。

他のオブジェクトにアタッチしたscriptのデータを取得

FindでGameManagerオブジェクトのscriptコンポーネント(GameManager.cs)を探し、
String型のtestを参照する例。

    public GameManager GM;
    void Start()
    {
        GM=GameObject.Find("GameManager").GetComponent<GameManager>();
        String test =GM.test;
    }
    

imegeのスプライト変更例

1.GameManagerというクラスにあらかじめ、public Sprite[] itemSprite と宣言して、
unityのインスペクターにアタッチしておく。
2.下記コマンドでTargetGameObjectのImageのspriteを変更する。

TargetGameObject.GetComponent<Image>().sprite= GM.itemSprite[0];

sprite と image 使い分け

uGUIは動かすことを余り想定していない
動かさなければ、低負荷 低消費電力
uGUIはCanvas単位で動かす
Spriteは動かすことを前提
沢山描画する場合に対して最適化
といった方針が見えます。

動かさないがα0の範囲が大きいならSprite Renderer
(但し、矩形できれいに抜ける場合はその限りではない)
スプライトを常に大量に動かすならSprite Renderer
オーバードローが多いならSprite Renderer
画像を絶対に動かさないならuGUIのImage
小さいスプライトを一部動かすならuGUIのImage
(動かすImageは別途Canvas必須)

引用元: https://tsubakit1.hateblo.jp/entry/2016/09/26/080000

spriteにbuttonコンポーネントを追加時の注意点

imageコンポーネントも追加しないとclick処理が実行されない。
参考:https://forum.unity.com/threads/button-component-on-sprite-object.285765/

imageコンポーネントのrange

Raycast Paddingでrangeの調節可能

BGM設定

オブジェクトを選択し、AddComponent >Audio>AudioSourceでアタッチ
AudioClipでファイルを指定

BGMの止め方

GetComponent<AudioSource>().Stop();

効果音

同じくAudioSourceクラスのPlayOneShotメソッドでAudioClip型の音源ファイルを1回再生できる。

  AudioSource audioSource;
  public AudioClip sound1;
  void Start()
    {
        audioSource = GetComponent<AudioSource>();
        audioSource.PlayOneShot(sound1);
    }

参考URL:https://www.sejuku.net/blog/83535

Font 変更

対応フォント形式(.ttf .otf)をダウンロードし、Assetsフォルダに入れる。
ダウンロード可能サイト:http://modi.jpn.org/
参考URL:https://www.sejuku.net/blog/83669

UIが画面から見切れないように設定する

・Screen Match Mode

・Match Width or Height
UI要素の縮小を、縦を基準にするか、横を基準にするか設定可能

・Expand
UI要素が画面から絶対に見切れないように、縦横に伸縮。

・Shrink
描画位置がUIの範囲外を映さないように、UIをトリミングして表示。背景画像に有効。

参考URL:https://torampo.com/?p=87

world座標の取得

Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

要MonoBehaviourクラスの継承
Camera.main : MainCameraタグが付いたカメラを取得

レイヤーマスクの取得方法

LayerMask.GetMask("マスク名");

レイヤーマスク:タグ名のこと。
要MonoBehaviourクラスの継承

クリックでオブジェクトを検知

検知させたいオブジェクトにはRigitBody2DとBoxCollider2Dをアタッチしておく。

	void Update()
	{
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		//Rayの長さ
		float maxDistance = 10;
		RaycastHit2D hit = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction, maxDistance);
		
		//右クリックして、Raycastがnullじゃない(オブジェクト)と衝突したら処理をする。
		if(Input.GetMouseButtonDown(0) && hit.collider){
			//処理
			Debug.Log(hit.collider.gameObject.name);
		}
	}
	
	//参考: https://kan-kikuchi.hatenablog.com/entry/RayCast4
	

		//RigitBody2DのBodyTypeはStatic設定
		
		//Dynamic
		//物理演算で移動するオブジェクト
		
		//Kinematic
		//スクリプトで動かすオブジェクト
		
		//Static
		//原則移動しない、稀に再配置もしくはOn・OFFするオブジェクト
		
		//Rigidbody2d無し
		//動かさない。
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?