0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity】設定メモ20250425

Last updated at Posted at 2025-04-24

まとめリンク

Unity関連
AtCoder関連

【Unity(2021)】ビューが開かれたときに何かしたい

空のGameObjectを作成

  1. ヒエラルキーで右クリック → Create Empty
    名前を「SplashView」に変更

C#スクリプトの作成・アタッチ

  1. プロジェクトビューで右クリック → Create > C# Script
    名前を「ViewOpenEvent」にする
  2. 作成した「ViewOpenEvent」スクリプトを ViewOpenEvent にドラッグ&ドロップ

C#コード

「ビューが開かれたときに何かしたい」=オブジェクトが有効化されたときに何かする
→ Unityでは OnEnable() または Start() を使う。

using UnityEngine;

public class ViewOpenEvent : MonoBehaviour
{
    // この関数はオブジェクトが有効化されたときに呼ばれます
    void OnEnable()
    {
        Debug.Log("ビュー(ViewOpenEvent)が開かれました!");
    }
}

動作確認

  1. Unityエディタで「ViewOpenEvent」GameObjectのチェックボックスをオン/オフを行う。

WS001642802_.jpg

  1. コンソールに ビュー(ViewOpenEvent)が開かれました! と表示される。

WS001642801.jpg


【Unity(2021)】一定時間待ってから別の画面に遷移したい

1. シーンを2つ作成

  1. File > New Sceneでシーンを2つ作成し、名前をそれぞれ

    • SplashScene(スプラッシュやロゴ表示用)
    • MainScene(遷移先)
      にする。
  2. File > Save As... でそれぞれ保存する。

2. シーンをビルド設定に追加

  • メニューから File > Build Settings... を開き、Add Open Scenes ボタンで両方のシーンを追加する。
    SplashScene を上(Index 0)、MainScene を下(Index 1)に並べておく。

WS001642835.jpg

3. 遷移用オブジェクトの作成

  • SplashScene を開き、Hierarchyで右クリック → Create Empty で空のGameObjectを作成し、名前を SceneAutoSwitcher にする。

C#スクリプト例

1. スクリプト作成

AssetsフォルダにSceneAutoSwitcher.csという名前で新規C#スクリプトを作成し、以下の内容にします。

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneAutoSwitcher : MonoBehaviour
{
    [Tooltip("遷移までの待機秒数")]
    public float waitSeconds = 2f;

    void Start()
    {
        // コルーチンで指定秒数待ってからシーン遷移
        StartCoroutine(WaitAndSwitchScene());
    }

    System.Collections.IEnumerator WaitAndSwitchScene()
    {
        yield return new WaitForSeconds(waitSeconds);
        // Build SettingsでIndex 1のシーンへ遷移
        SceneManager.LoadScene(1);
    }
}

2. スクリプトのアタッチ

  • SplashSceneSceneAutoSwitcherオブジェクトにこのスクリプトをアタッチします。
  • InspectorでWait Secondsを好きな秒数に設定できます(デフォルト2秒)。

上動画のように2秒後に画面が変化する。
イベントトリガーでフェードアウト使いながらであれば、画面遷移に結構利用できそうですね。

【Unity(2021)】GameObjectリストを一定間隔で順番に表示する

C#スクリプト例

1. スクリプト作成

AssetsフォルダにLoopButtons.csという名前で新規C#スクリプトを作成し、以下の内容にします。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoopButtons : MonoBehaviour
{
    //! Viewのリスト.
    [SerializeField] List<GameObject> viewList = new List<GameObject>();

    void Start()
    {
        // 最初のViewを表示
        Test_ChangeView(0);
        // コルーチン開始
        StartCoroutine(ChangeViewLoop());
    }

    // ------------------------------------------------
    /// View移動処理.
    // ------------------------------------------------
    public void Test_ChangeView(int index)
    {
        for (int i = 0; i < viewList.Count; i++)
        {
            viewList[i].SetActive(i == index);
        }
    }

    // ------------------------------------------------
    /// 1秒ごとにViewを切り替えてループ.
    // ------------------------------------------------
    IEnumerator ChangeViewLoop()
    {
        int index = 0;
        while (true)
        {
            Test_ChangeView(index);
            index = (index + 1) % viewList.Count; // 0~Count-1でループ
            yield return new WaitForSeconds(1f);
        }
    }
}

【Unity(2022)】[DOTween]UIオブジェクトを滑らかに消す方法:右スライド+アルファフェード+縮小アニメーションの統合

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class UITransition : MonoBehaviour
{
    public RectTransform Rect
    {
        get
        {
            if (rect == null) rect = GetComponent<RectTransform>();
            return rect;
        }
    }
    RectTransform rect = null;

    // CanvasGroupの参照
    CanvasGroup canvasGroup;

    void Awake()
    {
        // CanvasGroupを取得または追加
        canvasGroup = GetComponent<CanvasGroup>();
        if (canvasGroup == null)
        {
            canvasGroup = gameObject.AddComponent<CanvasGroup>();
        }
    }

    void Start()
    {
        TransitionOut();
    }


    //! アウトのシークエンス.
    Sequence outSequence = null;
    // ----------------------------------------------------
    // トランジションアウト.
    // ----------------------------------------------------
    public void TransitionOut()
    {
        // 既存のアニメーションを停止
        if (outSequence != null)
        {
            outSequence.Kill();
            outSequence = null;
        }

        outSequence = DOTween.Sequence();

        // フェードアウト
        if (canvasGroup != null)
        {
            canvasGroup.alpha = 1f;
            outSequence.Join(
                canvasGroup.DOFade(0, 3f)
                    .SetEase(Ease.OutQuad) // イージング追加
            );
        }

        // 右方向への移動(250px)
        outSequence.Join(
            Rect.DOLocalMoveX(250f, 3f)
                .SetEase(Ease.InOutSine)
        );

        // スケールダウン(0.25倍)
        outSequence.Join(
            Rect.DOScale(0.25f, 3f)
                .SetEase(Ease.OutBack)
        );

        // アニメーション終了時のコールバック
        outSequence.OnComplete(() => {
            Debug.Log("Transition完了");
            gameObject.SetActive(false); // 非アクティブ化
        });

        outSequence.SetLink(gameObject);
    }

}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?