[Unity] 시야의 4분의 1 크기의 원형 랜드마크 만들기

17706 단어 UniRxUnity

개요


유닛으로 시야의 4분의 1이 좋은 게임을 만들려고 하니 그 설치 방법을 총괄해 보자.
이 글에서 소개된 완성형은 주사위를 흔드는 버튼을 누르면 주사위의 눈이 표시되고, 캐릭터는 그 정도로만 이동하는 등 원형이 대단하다는 것이다.

운영 환경


Unity 2017.3.1f1

사용 자산

  • UniRx 5.7.0
  • DOTween 1.1.640
  • 사용된 소재 자산
  • 드림 포인트 캐릭터 꾸러미2
  • 2D PixelArt - Isometric Blocks
  • 압축의 설치


    세 개의 스크립트를 만듭니다. 하나는 휘발성 이벤트를 실현하는 데 사용되고, 하나는 단추를 눌렀을 때 이 이벤트를 실행하는 데 사용되며, 하나는 주사위 결과를 표시하는 데 사용됩니다.
    DiceRollObserver.cs
    // ダイスを振るイベント
    
    using UniRx;
    using UnityEngine;
    using Random = UnityEngine.Random;
    
    public class DiceRollObserver : MonoBehaviour
    {
        public Subject<int> OnDiceRolledObservable = new Subject<int>();
    
        public void RollDice()
        {
            // 今回は1〜6の目が出るダイス
            this.OnDiceRolledObservable.OnNext(Random.Range(1, 7));
        }
    }
    
    RollDiceButtonDispacher.cs
    // ダイスを振るボタンを押した時の処理
    
    using UnityEngine;
    using UniRx;
    using UnityEngine.UI;
    
    [RequireComponent(typeof(Button))]
    public class RollDiceButtonDispatcher : MonoBehaviour
    {
        public DiceRollObserver DiceRollObserver;
    
        void Start()
        {
            this.GetComponent<Button>().OnClickAsObservable()
                .Subscribe(_ => this.DiceRollObserver.RollDice())
                .AddTo(this);
        }
    }
    
    DiceRenderer.cs
    // ダイスの数字を表示する
    
    using UnityEngine;
    using UnityEngine.UI;
    using UniRx;
    
    [RequireComponent(typeof(DiceRollObserver))]
    [RequireComponent(typeof(Text))]
    public class DiceRenderer : MonoBehaviour
    {
        void Start()
        {
            // ダイスを振るイベントを受けて、テキストの描画
            this.GetComponent<DiceRollObserver>()
                .OnDiceRolledObservable
                .Subscribe(value => this.Render(value))
                .AddTo(this);
        }
    
        // テキストの描画
        private void Render(int value)
        {
            this.GetComponent<Text>().text = value.ToString();
        }
    }
    
    UniRx에서 주사위를 흔드는 이벤트, 감시 버튼의 처리, 동적 이벤트의 처리를 감시한다고 적혀 있다.

    캐릭터 이동 처리의 실현


    커다란 빈칸과 캐릭터의 위치 정보를 저장하고 주사위가 던진 사건에 반응하여 캐릭터를 움직이는 스크립트를 만든다.
    Field.cs
    // すごろくのマス情報
    
    using UnityEngine;
    
    public class Field : MonoBehaviour {
        // すごろくの座標を取得するために各マスのオブジェクトを格納(transformのリストでもいい)
        public GameObject[] MassGameObjects;
    }
    
    Charactor.cs
    // キャラクターの情報
    
    using UnityEngine;
    
    public class Charactor: MonoBehaviour
    {
        // キャラクターの位置
        public int Place;
    }
    
    CharactorRenderer.cs
    // キャラクターの表示
    
    using UnityEngine;
    using UniRx;
    using DG.Tweening;
    
    [RequireComponent(typeof(Charactor))]
    public class CharactorRenderer : MonoBehaviour
    {
        public DiceRollObserver DiceRollObserver;
        public Field Field;
        private Charactor Charactor;
    
        void Start()
        {
            this.Charactor = this.GetComponent<Charactor>();
    
            // ダイスを振るイベントを受けて、移動処理を実行
            this.DiceRollObserver.OnDiceRolledObservable
                .Subscribe(value => this.Move(value))
                .AddTo(this);
        }
    
        // ダイスの目だけ移動
        private void Move(int moveCount)
        {
            Sequence moveSequence = DOTween.Sequence();
            for (int i = 1; i <= moveCount; i++)
            {
                int nextPlace = i + this.Charactor.Place;
                // 一周した場合は0に戻す
                if (nextPlace >= this.Field.MassGameObjects.Length)
                {
                    nextPlace -= this.Field.MassGameObjects.Length;
                }
    
                // すごろくのマスオブジェクトの座標を元に、キャラクターの次の移動先を決める
                Vector3 nextPosition = Field.MassGameObjects[nextPlace].transform.localPosition;
                this.AppendMove(moveSequence, nextPosition);
            }
        }
    
        // キャラクターの移動処理を追加
        private void AppendMove(Sequence sequence, Vector3 newPosition)
        {
            sequence.Append(
                this.GetComponent<RectTransform>()
                    .DOLocalMove(newPosition, 0.5f)
                    .OnComplete(() =>
                    {
                        // 移動後の処理
                        // キャラクターの位置の更新
                        this.Charactor.Place = this.GetNewPlace(this.Charactor.Place);
                        // 位置によってキャラクターの向きを変える処理
                        this.ChangeDirection(this.Charactor.Place);
                    })
            );
        }
    
        // 位置を一つ進めた新しい位置を返す
        private int GetNewPlace(int place)
        {
            int _place = place;
            if (Field.MassGameObjects.Length - 1 != place)
            {
                _place++;
            }
            else // 一周した場合
            {
                _place = 0;
            }
    
            return _place;
        }
    
        // キャラクターの向きを変える
        private void ChangeDirection(int place) {
            // 省略
        }
    
    쌍육의 격자에 0~19의 값Field.MassGameObjects을 분배하고 캐릭터의 현재 위치 정보Charactor.Place만 참조하여 DOLocalMove로 이동한 쌍육의 값을 참조한다.
    DOTween의 Sequence를 사용하여 애니메이션을 결합하거나 Callback 처리를 통해 이동 후 현재 위치의 업데이트와 방향을 업데이트할 수 있습니다.
    원형 이외의 원반을 만들 때는 고려해야 할 것 같지만, 유닉스와 DOTween을 사용하면 원형의 힘을 실현할 수 있다.

    참고 문장


    만들면서 이해하는 유닛.
    DOTween의 Sequence를 사용하여 애니메이션을 결합합니다.

    좋은 웹페이지 즐겨찾기