StoryEngine.cs
· 2.3 KiB · C#
原始檔案
Playground
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class StoryEngine : MonoBehaviour
{
public delegate void HandlePassage(StoryEngine engine, params Object[] args);
Dictionary<string, HandlePassage> callbacks = new Dictionary<string, HandlePassage>();
Dictionary<string, AudioClip> audioClips = new Dictionary<string, AudioClip>();
Dictionary<string, Animator> actors = new Dictionary<string, Animator>();
AudioSource source;
// Start is called before the first frame update
void Start()
{
source = GetComponent<AudioSource>();
}
public void LoudAudioClips(string path)
{
audioClips = new Dictionary<string, AudioClip>();
var clips = Resources.LoadAll<AudioClip>(path);
foreach (var clip in clips)
{
audioClips.Add(clip.name, clip);
}
Debug.Log(audioClips);
}
public void AddActor(string path)
{
GameObject gameObject = GameObject.Find(path);
Animator animator = gameObject.GetComponent<Animator>();
var result = path.Split('/');
string name = result[result.Length - 1];
if (animator)
{
actors.Add(name, animator);
}
}
public void AddActor(string name, Animator animator)
{
actors.Add(name, animator);
}
public void PlayClip(string name)
{
source.clip = audioClips[name];
source.Play();
}
public void SetActorTrigger(string actorName, string name)
{
actors[actorName].SetTrigger(name);
}
public float GetClipLength(string name)
{
return audioClips[name].length;
}
public void Register(string name, HandlePassage callback)
{
callbacks.Add(name, callback);
}
public void Run(string name, params Object[] args)
{
if (callbacks.ContainsKey(name)) {
callbacks[name](this, args);
}
}
public void Run(float delay, string name, params Object[] args)
{
StartCoroutine(WaitAndRun(delay, name, args));
}
private IEnumerator WaitAndRun(float delay, string name, params Object[] args)
{
yield return new WaitForSeconds(delay);
Run(name, args);
}
}
1 | using System.Collections; |
2 | using System.Collections.Generic; |
3 | using UnityEngine; |
4 | |
5 | [RequireComponent(typeof(AudioSource))] |
6 | public class StoryEngine : MonoBehaviour |
7 | { |
8 | public delegate void HandlePassage(StoryEngine engine, params Object[] args); |
9 | Dictionary<string, HandlePassage> callbacks = new Dictionary<string, HandlePassage>(); |
10 | Dictionary<string, AudioClip> audioClips = new Dictionary<string, AudioClip>(); |
11 | Dictionary<string, Animator> actors = new Dictionary<string, Animator>(); |
12 | AudioSource source; |
13 | // Start is called before the first frame update |
14 | |
15 | void Start() |
16 | { |
17 | source = GetComponent<AudioSource>(); |
18 | } |
19 | |
20 | |
21 | public void LoudAudioClips(string path) |
22 | { |
23 | audioClips = new Dictionary<string, AudioClip>(); |
24 | var clips = Resources.LoadAll<AudioClip>(path); |
25 | foreach (var clip in clips) |
26 | { |
27 | audioClips.Add(clip.name, clip); |
28 | } |
29 | Debug.Log(audioClips); |
30 | } |
31 | |
32 | public void AddActor(string path) |
33 | { |
34 | GameObject gameObject = GameObject.Find(path); |
35 | Animator animator = gameObject.GetComponent<Animator>(); |
36 | var result = path.Split('/'); |
37 | string name = result[result.Length - 1]; |
38 | if (animator) |
39 | { |
40 | actors.Add(name, animator); |
41 | } |
42 | } |
43 | |
44 | public void AddActor(string name, Animator animator) |
45 | { |
46 | actors.Add(name, animator); |
47 | } |
48 | |
49 | public void PlayClip(string name) |
50 | { |
51 | source.clip = audioClips[name]; |
52 | source.Play(); |
53 | } |
54 | |
55 | public void SetActorTrigger(string actorName, string name) |
56 | { |
57 | actors[actorName].SetTrigger(name); |
58 | } |
59 | |
60 | public float GetClipLength(string name) |
61 | { |
62 | return audioClips[name].length; |
63 | } |
64 | public void Register(string name, HandlePassage callback) |
65 | { |
66 | callbacks.Add(name, callback); |
67 | } |
68 | |
69 | public void Run(string name, params Object[] args) |
70 | { |
71 | if (callbacks.ContainsKey(name)) { |
72 | callbacks[name](this, args); |
73 | } |
74 | } |
75 | |
76 | public void Run(float delay, string name, params Object[] args) |
77 | { |
78 | StartCoroutine(WaitAndRun(delay, name, args)); |
79 | } |
80 | |
81 | private IEnumerator WaitAndRun(float delay, string name, params Object[] args) |
82 | { |
83 | yield return new WaitForSeconds(delay); |
84 | Run(name, args); |
85 | } |
86 | |
87 | } |
88 |