結論
- Awakeは使用せず、OnEnableメソッド以降で行う
- 理由: Awake内で変数への代入や関数(例: Debug.Log)の実行はできるが、変数への格納情報はAwake外までは保存されない
- → Monobehaviourと動作が異なるため、意図しないバグの原因になりかねない
- 理由: Awake内で変数への代入や関数(例: Debug.Log)の実行はできるが、変数への格納情報はAwake外までは保存されない
事象
- 変数の初期化処理をAwakeで行っても、変数に指定した初期値が保存されていなかった
事象の確認結果
先に結果を提示しておく。
VRChat SDK使用時のUnityEditor
VRChat SDK未使用の通常のUnityEditor
事象の検証手順
~AwakeとOnEnableやStartでのGameObjectTest Init Variables
のTransform.Positionの取得動作の違いについて~
VRChat SDKを使用時のUnityでの動作確認
- Udonsharpスクリプト
TestInitVariables.cs
を作成し、空のGameObjectTest Init Variables
にアタッチする。-
Test Init Variables
の座標は今回(0,2.605f,0)
のようにインスペクターで指定する
-
TestInitVariables.cs
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
public class TestInitVariables : UdonSharpBehaviour
{
private Vector3 enablePos;
private Vector3 startPos;
private Vector3 awakePos;
private bool hasCalledAwakeLog = false;
private bool hasCalledEnableLog = false;
private bool hasCalledStartLog = false;
private void Awake()
{
awakePos = transform.position; // (0,2.605f,0)
Debug.Log("AwakePos in Awake: " + awakePos); // AwakePos in Awake: (0,2.605,0)
}
private void OnEnable()
{
enablePos = transform.position; // (0,2.605f,0)
Debug.Log("EnablePos in OnEnable: " + enablePos); // EnablePos in OnEnable: (0,2.605,0)
}
private void Start()
{
startPos = transform.position;// (0,2.605f,0)
Debug.Log("StartPos in Start: " + startPos);// StartPos in Start: (0,2.605,0)
}
private void Update()
{
if (!hasCalledAwakeLog) { Debug.Log("AwakePos in Update: " + awakePos); hasCalledAwakeLog = true; } // AwakePos in Update: (0,0,0)
if (!hasCalledEnableLog) { Debug.Log("EnablePos in Update: " + enablePos); hasCalledEnableLog = true; } // EnablePos in Update: (0,2.605,0)
if (!hasCalledStartLog) { Debug.Log("StartPos in Update: " + startPos); hasCalledStartLog = true; } // StartPos in Update: (0,2.605,0)
}
}
2.Unityエディタ上でPlayすることでUdonSimulatorを実行し、表示されるログを確認する
VRChat SDK使用時のUnityEditor
VRChat SDK未使用の通常のUnityでの動作確認
-
VRChat SDK使用時と同様 Udonsharpスクリプト
TestInitVariables.cs
を作成し、空のGameObjectTest Init Variables
にアタッチする。- ※ただしVRChat SDKは使用しないため、
TestInitVariables.cs
のスクリプトの継承元をMonoBehaviourに直しておくこと。 -
Test Init Variables
の座標は今回(0,2.605f,0)
のようにインスペクターで指定する
- ※ただしVRChat SDKは使用しないため、
TestInitVariables
using UnityEngine; // ← UdonSharpBehaviour用のusingは削除する
public class TestInitVariables : MonoBehaviour // ← Monobehaviourになおす
{
private Vector3 enablePos;
private Vector3 startPos;
private Vector3 awakePos;
private bool hasCalledAwakeLog = false;
private bool hasCalledEnableLog = false;
private bool hasCalledStartLog = false;
private void Awake()
{
awakePos = transform.position; // (0,2.605f,0)
Debug.Log("AwakePos in Awake: " + awakePos); // AwakePos in Awake: (0,2.605,0)
}
private void OnEnable()
{
enablePos = transform.position; // (0,2.605f,0)
Debug.Log("EnablePos in OnEnable: " + enablePos); // EnablePos in OnEnable: (0,2.605,0)
}
private void Start()
{
startPos = transform.position;// (0,2.605f,0)
Debug.Log("StartPos in Start: " + startPos);// StartPos in Start: (0,2.605,0)
}
private void Update()
{
if (!hasCalledAwakeLog) { Debug.Log("AwakePos in Update: " + awakePos); hasCalledAwakeLog = true; } // AwakePos in Update: (0,0,0)
if (!hasCalledEnableLog) { Debug.Log("EnablePos in Update: " + enablePos); hasCalledEnableLog = true; } // EnablePos in Update: (0,2.605,0)
if (!hasCalledStartLog) { Debug.Log("StartPos in Update: " + startPos); hasCalledStartLog = true; } // StartPos in Update: (0,2.605,0)
}
}
2.Unityエディタ上でPlayし、表示されるログを確認する
VRChat SDK未使用の通常のUnity
Appendix:イベントの実行順序
引用元にも記載されているが、ここにすべてのイベントが表示されているわけではないらし