前提
Unityにてゲーム開発をしたいと思い勉強中です。
一応Javaの資格は持っていますが、UnityはもちろんJavaでの開発経験もないに等しいです。
プログラムの概念や、わからない事に関して調べる方法等の知識が少しあるぐらいです。
なので現在、ゲームがどのように作られているのかをUnityの公式ホームページにあるチュートリアルの3DGameKitというプロジェクトを落とし込んで勉強しているところです。
わからない事柄
3DGameKitのプロジェクト内にあります、
3DGameKit->Scripts->Game->Player->PlayerController.csにて、表題にあるように自身を参照するソース、バッキングフィールド(そもそもバッキングフィールドとは何かが解らない)をしているソースがあるのですがこれが何の為にしているのかわからずにいます。
該当のソースコード(一部抜粋)
public class PlayerController : MonoBehaviour, IMessageReceiver { //なぜ自身を参照するのか protected static PlayerController s_Instance; //なぜ自身のプレハブをインスタンス??? //はたまた自身を参照したものをgetする?何をgetする??? public static PlayerController instance { get { return s_Instance; } }
詳細のソースは下部に載せておきます。
質問
ググって見ても上記ソースについて、いまいち理解ができず何をしているのか・何がしたいのかがわかりません。
該当ソースをする・しないではどのように変わってくるのでしょうか。
するメリットとして何が挙げられるのでしょうか。
これらに関してどなたかご教授の程お願いします。
また、蛇足ですがゲーム開発能力やプログラミング能力(アルゴリズム関連等)の向上方法に関してもご教示いただけますと幸いです。
該当のソースコード(詳細ソース)
using UnityEngine; using Gamekit3D.Message; using System.Collections; using UnityEngine.XR.WSA; namespace Gamekit3D { [RequireComponent(typeof(CharacterController))] [RequireComponent(typeof(Animator))] public class PlayerController : MonoBehaviour, IMessageReceiver { protected static PlayerController s_Instance; public static PlayerController instance { get { return s_Instance; } } public bool respawning { get { return m_Respawning; } } public float maxForwardSpeed = 8f; // How fast Ellen can run. public float gravity = 20f; // How fast Ellen accelerates downwards when airborne. public float jumpSpeed = 10f; // How fast Ellen takes off when jumping. public float minTurnSpeed = 400f; // How fast Ellen turns when moving at maximum speed. public float maxTurnSpeed = 1200f; // How fast Ellen turns when stationary. public float idleTimeout = 5f; // How long before Ellen starts considering random idles. public bool canAttack; // Whether or not Ellen can swing her staff. public CameraSettings cameraSettings; // Reference used to determine the camera's direction. public MeleeWeapon meleeWeapon; // Reference used to (de)activate the staff when attacking. public RandomAudioPlayer footstepPlayer; // Random Audio Players used for various situations. public RandomAudioPlayer hurtAudioPlayer; public RandomAudioPlayer landingPlayer; public RandomAudioPlayer emoteLandingPlayer; public RandomAudioPlayer emoteDeathPlayer; public RandomAudioPlayer emoteAttackPlayer; public RandomAudioPlayer emoteJumpPlayer; ~略~ protected bool IsMoveInput { get { return !Mathf.Approximately(m_Input.MoveInput.sqrMagnitude, 0f); } } public void SetCanAttack(bool canAttack) { this.canAttack = canAttack; } // Called automatically by Unity when the script is first added to a gameobject or is reset from the context menu. void Reset() { meleeWeapon = GetComponentInChildren<MeleeWeapon>(); Transform footStepSource = transform.Find("FootstepSource"); if (footStepSource != null) footstepPlayer = footStepSource.GetComponent<RandomAudioPlayer>(); Transform hurtSource = transform.Find("HurtSource"); if (hurtSource != null) hurtAudioPlayer = hurtSource.GetComponent<RandomAudioPlayer>(); Transform landingSource = transform.Find("LandingSource"); if (landingSource != null) landingPlayer = landingSource.GetComponent<RandomAudioPlayer>(); cameraSettings = FindObjectOfType<CameraSettings>(); if (cameraSettings != null) { if (cameraSettings.follow == null) cameraSettings.follow = transform; if (cameraSettings.lookAt == null) cameraSettings.follow = transform.Find("HeadTarget"); } } // Called automatically by Unity when the script first exists in the scene. void Awake() { m_Input = GetComponent<PlayerInput>(); m_Animator = GetComponent<Animator>(); m_CharCtrl = GetComponent<CharacterController>(); meleeWeapon.SetOwner(gameObject); s_Instance = this; } ~以下略~
補足情報
拝見したブログ等
『プレハブに自身の参照を持たせた時にハマった挙動』
http://sukiaraba.hatenablog.jp/entry/2016/12/25/132334
『インスペクターで値も設定したいけど,プロパティを使いたい。めんどくさいのも嫌!』
https://qiita.com/RyotaMurohoshi/items/b64b36009ba652b21e2c
『C# 7.3から自動実装プロパティのバッキングフィールドに属性をつけられる ...』
https://qiita.com/RyotaMurohoshi/items/bf078f890473d6b5568b
『実行時のプレハブのインスタンス化 - Unity マニュアル - Unity - Manual』
https://docs.unity3d.com/ja/current/Manual/InstantiatingPrefabs.html
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/04 10:42
2019/03/04 12:40
2019/03/05 23:02