質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

0回答

813閲覧

List化したTimelineAssetを任意のタイミングで再生したい

gariyaro

総合スコア8

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/10/18 12:55

前提・実現したいこと

Unityにてアドベンチャーゲームを制作中の初心者です。言語はC#です。
会話シーンのあとにイベントシーンを挿入したく、Timelineを使用しています。
複数のTimelineAssetをリストで管理し、任意の会話が終了したタイミングで再生しようと考えたのですが、最初のTimelineAsset以外は再生されませんでした。
皆様のお知恵をお借りしたいです。

該当のソースコード① 会話キャラにアタッチするスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using UnityEngine.Playables; 6using UnityEngine.Timeline; 7 8public class NPC4 : MonoBehaviour 9{ 10 public TimelineAsset[] timelines; 11 public PlayableDirector playableDirector; 12 [SerializeField] bool never_met = true; 13 public int scnarioNum = 0; 14 [SerializeField] Item flagItem; 15 GameObject player; 16 PlayerScript playerScript; 17 //[SerializeField] TextController textController; 18 [SerializeField] 19 Scenario4 scenario; 20 [SerializeField] 21 private GameObject LetterBox; 22 Letterbox letterbox; 23 GameObject TimeText; 24 TimeManager timeManager; 25 [SerializeField] 26 private GameObject fukidashi; 27 28 29 // Start is called before the first frame update 30 void Start() 31 { 32 playableDirector = GetComponent<PlayableDirector>(); 33 player = GameObject.Find("Player"); 34 playerScript = player.GetComponent<PlayerScript>(); 35 letterbox = LetterBox.GetComponent<Letterbox>(); 36 } 37 38 // Update is called once per frame 39 void Update() 40 { 41 if (never_met == false) 42 { 43 fukidashi.SetActive(false); 44 } 45 if(FlagManager.Instance.ItemList.Contains(flagItem) == true)//アイテムゲット時 46 { 47 scnarioNum = 3; 48 } 49 } 50 private void OnTriggerEnter2D(Collider2D collision) 51 { 52 playerScript.isTalk = false; 53 } 54 private void OnTriggerStay2D(Collider2D collision) 55 { 56 if (playerScript.isTalk == true) 57 { 58 { 59 if (FlagManager.Instance.talking == false) 60 { 61 TalkStart(); 62 } 63 playerScript.isTalk = false; 64 } 65 } 66 } 67 private void OnTriggerExit2D(Collider2D collision) 68 { 69 playerScript.isTalk = false; 70 } 71 72 public void TalkStart() 73 { 74 FlagManager.Instance.talking = true; 75 playerScript.characterState = PlayerScript.CharacterState.talk; 76 if (never_met == true) 77 { 78 Debug.Log("最初の会話発動"); 79 scenario.enabled = true; 80 never_met = false; //一度は会話した状態に 81 } 82 else //一度会話した後はコチラに移行 83 { 84 if(scnarioNum == 2) 85 { 86 Debug.Log("3度目の会話発動"); 87 scenario.enabled = true; 88 } 89 else if (FlagManager.Instance.ItemList.Contains(flagItem) == false) //フラグ立てていない(アイテム未取得) 90 { 91 Debug.Log("2度目の会話発動"); 92 scnarioNum = 1; 93 scenario.enabled = true; 94 } 95 else //アイテムゲットした状態 96 { 97 Debug.Log("アイテムゲット状態での会話"); 98 scnarioNum = 3; 99 scenario.enabled = true; 100 FlagManager.Instance.flags[0] = true; //フラグ0を立てる 101 } 102 } 103 }

該当のソースコード② 各キャラのシナリオを再生するスクリプト

C#

1using System.Linq; 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using TMPro; 6using UnityEngine.UI; 7using UnityEngine.SceneManagement; 8 9public class Scenario4 : MonoBehaviour 10{ 11 [SerializeField] 12 string[] currentScenario; 13 public string[] scenarios; 14 public string[] scenarios2; 15 public string[] scenarios3; 16 public string[] scenarios4; 17 18 [SerializeField] TextMeshProUGUI uiText; 19 20 [SerializeField] 21 [Range(0.001f, 0.3f)] 22 float intervalForCharacterDisplay = 0.05f; //表示する文字の間隔 23 24 private string currentText = string.Empty; //現在の文章 25 private float timeUntilDisplay = 0; //表示までの時間 26 private float timeElapsed = 1; //経過した時間 27 private int currentLine = 0; //配列の番号 28 private int lastUpdateCharacter = -1; //最後に更新する文字 29 GameObject player; 30 PlayerScript playerScript; 31 [SerializeField] 32  NPC4 npc; 33 [SerializeField] 34 private bool Reload = false; 35 36 public bool IsCompleteDisplayText 37 { 38 get { return Time.time > timeElapsed + timeUntilDisplay; } 39 } 40 41 // Start is called before the first frame update 42 void Start() 43 { 44 //SetNextLine(); 45 46 player = GameObject.Find("Player"); 47 playerScript = player.GetComponent<PlayerScript>(); 48 } 49 50 // Update is called once per frame 51 void Update() 52 { 53 if (npc.scnarioNum == 0) 54 { 55 if (Reload == false) 56 { 57 currentScenario = scenarios; 58 currentLine = 0; //currentLineをゼロに戻す 59 SetNextLine(); 60 Reload = true; 61 } 62 LoadScenario(); 63 ScenarioEnd(); 64 } 65 66 if (npc.scnarioNum == 1) 67 { 68 if (Reload == false) 69 { 70 currentScenario = scenarios2; 71 currentLine = 0; 72 SetNextLine(); 73 Reload = true; 74 } 75 LoadScenario(); 76 ScenarioEnd(); 77 } 78 79 if (npc.scnarioNum == 2) 80 { 81 if (Reload == false) 82 { 83 currentScenario = scenarios3; 84 currentLine = 0; 85 SetNextLine(); 86 Reload = true; 87 } 88 LoadScenario(); 89 ScenarioEnd(); 90 } 91 if (npc.scnarioNum == 3) 92 { 93 if (Reload == false) 94 { 95 currentScenario = scenarios4; 96 currentLine = 0; 97 SetNextLine(); 98 Reload = true; 99 } 100 LoadScenario(); 101 ScenarioEnd(); 102 } 103 } 104 public void SetNextLine() 105 { 106 currentText = currentScenario[currentLine]; 107 timeUntilDisplay = currentText.Length * intervalForCharacterDisplay; 108 timeElapsed = Time.time; 109 currentLine++; 110 lastUpdateCharacter = -1; 111 } 112 113 public void LoadScenario() 114 { 115 // 文字の表示が完了してるなら次の行を表示する 116 if (IsCompleteDisplayText) 117 { 118 if (currentLine < currentScenario.Length && Input.GetKeyDown(KeyCode.Z)) 119 { 120 SetNextLine(); 121 } 122 } 123 else 124 { 125 // 完了してないなら文字をすべて表示する 126 if (Input.GetKeyDown(KeyCode.Z)) 127 { 128 timeUntilDisplay = 0; 129 } 130 } 131 132 int displayCharacterCount = (int)(Mathf.Clamp01((Time.time - timeElapsed) / timeUntilDisplay) * currentText.Length); 133 if (displayCharacterCount != lastUpdateCharacter)//再生中の文字 134 { 135 uiText.text = currentText.Substring(0, displayCharacterCount); 136 lastUpdateCharacter = displayCharacterCount; 137 } 138 } 139 public void ScenarioEnd() 140 { 141 if (currentScenario.Length == currentLine) //シナリオが最後まで読まれると~ 142 { 143 Debug.Log("シナリオえんど"); 144 currentText = string.Empty; 145 playerScript.characterState = PlayerScript.CharacterState.normal; 146 FlagManager.Instance.talking = false; 147 Reload = false; 148 this.enabled = false; 149 150 if (npc.scnarioNum == 1) 151 { 152 npc.playableDirector.Play(npc.timelines[0]); //再生される 153される 154 npc.scnarioNum = 2; 155 } 156 if (npc.scnarioNum == 3) 157 { 158 npc.playableDirector.Play(npc.timelines[1]); //再生されない 159 } 160 } 161 } 162}

補足情報(FW/ツールのバージョンなど)

Unity2019.4.1f1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sakura_hana

2020/10/20 07:30

Debug.Logを使って各変数の値・どこまで到達しているか(if分岐が想定通りに動作しているか)を確認してください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問