前提・実現したいこと
unityでtimelineを利用してTextMeshProを管理する方法がネットにあったので試そうとしたのですが、
TextMeshProの中身を参照しようとする際になぜかnullReferenceになってしまいます。
どのようにすれば解決できるか教えていただけないでしょうか?
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object TextPlayableBehaviour.OnGraphStart (UnityEngine.Playables.Playable playable) (at Assets/Scripts/TextPlayableBehaviour.cs:16) NullReferenceException: Object reference not set to an instance of an object TextPlayableBehaviour.ProcessFrame (UnityEngine.Playables.Playable playable, UnityEngine.Playables.FrameData info, System.Object playerData) (at Assets/Scripts/TextPlayableBehaviour.cs:45)
該当のソースコード
c#
1using System.Collections; 2using System.Collections.Generic; 3using TMPro; 4using UnityEngine; 5using UnityEngine.Playables; 6 7// A behaviour that is attached to a playable 8public class TextPlayableBehaviour : PlayableBehaviour 9{ 10 public GameObject charaObject; 11 private string text; 12 13 // Called when the owning graph starts playing 14 public override void OnGraphStart(Playable playable) 15 { 16 this.text = this.charaObject.GetComponent<TextMeshPro>().text; 17 this.charaObject.GetComponent<TextMeshPro>().text = ""; 18 } 19 20 // Called when the owning graph stops playing 21 public override void OnGraphStop(Playable playable) 22 { 23 this.charaObject.GetComponent<TextMeshPro>().text = this.text; 24 } 25 26 // Called when the state of the playable is set to Play 27 public override void OnBehaviourPlay(Playable playable, FrameData info) 28 { 29 30 } 31 32 // Called when the state of the playable is set to Paused 33 public override void OnBehaviourPause(Playable playable, FrameData info) 34 { 35 36 } 37 38 // Called each frame while the state is set to Play 39 public override void ProcessFrame(Playable playable, FrameData info, object playerData) 40 { 41 // PlayableTrackのClip上でシークバーが移動するたびに呼ばれ続ける(PrepareFrameの後) 42 if (charaObject == null) { return; } 43 var percent = (float)playable.GetTime() / (float)playable.GetDuration(); 44 45 this.charaObject.GetComponent<TextMeshPro>().text = 46 this.text.Substring(0, (int)Mathf.Round(this.text.Length * percent)); 47 } 48} 49
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.Playables; 5 6[System.Serializable] 7public class TextPlayableAsset : PlayableAsset 8{ 9 public ExposedReference<GameObject> charaObj; 10 // public string text; 11 12 // Factory method that generates a playable based on this asset 13 public override Playable CreatePlayable(PlayableGraph graph, GameObject go) 14 { 15 var behaviour = new TextPlayableBehaviour(); 16 behaviour.charaObject = charaObj.Resolve(graph.GetResolver()); 17 // behaviour.text = text; 18 return ScriptPlayable<TextPlayableBehaviour>.Create(graph, behaviour); 19 } 20}
補足情報(FW/ツールのバージョンなど)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/27 02:14