当前位置:   article > 正文

Unity Koreographer 之二 音乐制作插件中 Demo 的实现过程说明演示_koreographer professional edition

koreographer professional edition

Unity Koreographer 之二 音乐制作插件中 Demo 的实现过程说明演示

目录

Unity Koreographer 之二 音乐制作插件中 Demo 的实现过程说明演示

一、Koreographer 简单介绍

二、开发环境

三、注意事项

四、Demo 演示效果预览

五、分步演示说明实现步骤

1、基本场景和数据搭建

2、球根据节拍跳动效果

3、粒子特效根据节拍设置的时间span,来控制粒子发射

4、正方体逐渐变化的效果

5、节拍上的文字获取并展示效果

6、灯光随着节拍一闪一闪的效果

7、随着节拍颜色的动态变化

8、全部场景效果叠加起来,就是Demo的整体效果


一、Koreographer 简单介绍

Koreographer  官网:Sonic Bloom

    Koreographer Professional Edition™ 简化了在视频游戏中同步游戏玩法和音乐的过程。它简单的编辑界面允许你将音乐的节奏、节拍、音符、音量和其他动态元素映射到游戏中的事件中。

    Koreographer 可以用来创造节奏游戏,使任何游戏更加电影化,用音乐增强游戏环境,并创造新的控制和音乐驱动的游戏玩法。

    Koreographer功能:

        1.创建节奏游戏,启用唇形同步或以最小的努力为游戏添加字幕
        2.准确地将游戏中的事件同步到游戏音乐中的不同点
        3.快速创建新的音乐游戏和效果
 

Koreographer Professional Edition 下载建议去 Unity Asset Store 下载,这里下载路径仅供学习使用,商用建议购买正版。

Koreographer Professional Edition 1.6.1 学习下载地址:审核中

本节主要是介绍,Koreographer  中的 Demo 实现过程,目的是熟悉 Koreographer  和 相关接口,便于后面 Koreographer  的深入学习和使用。

有时间你也可以参看官网的学习更多,Koreographer  官网:Sonic Bloom

二、开发环境

1、 Koreographer Professional Edition 1.6.1

2、Unity 2019.3.13f1

三、注意事项

1、导入后 Koreographer  有四个文件夹 Editor Default Resources 、Gizmos、Koreographer、Plugins ,做好不要新建一个文件夹,再把他们统一在该文件下,这样可能会导致一些未知错误,或者没有报错但是一些功能用不了

四、Demo 演示效果预览

五、分步演示说明实现步骤

1、基本场景和数据搭建

1)在场景中,搭建场景如下

2)在工程中新建一个Koreography,用来添加音频和相关数据、节拍事件设置

 

 3)在场景中添加一个 GameObject ,命名为 Music Player,添加组件,Koreographer,AudioSource,Simple Music Player, Simple Music Player 上对应添加之前新建的 Koreography

2、球根据节拍跳动效果

1)在场景中,两个球体,并在球体上添加刚体

 

2)在工程中新建一个 Koreohgraphy Track ,设置 Event ID ,并赋值给 Koreohgraphy

 

3)选中 Koreohgraphy ,点击 Open In Koreography Editor,选择对应 Koreohgraphy Track 的 Event ID,进行节拍设置,这里设置 Payload 为 float ,值可以根据需要设置大小(Demo中没有使用该值,获取可以使用 evt.GetFloatValue() 获取自己对用设置的值)

 

4)这样对应的 Koreohgraphy Track 的 Event List也会被对应设置上

 

5)新建脚本,用来获取对应事件,使得小球跳动,并把脚本添加到,小球上,对应设置 Event ID

  1. //----------------------------------------------
  2. // Koreographer
  3. // Copyright © 2014-2020 Sonic Bloom, LLC
  4. //----------------------------------------------
  5. using UnityEngine;
  6. namespace SonicBloom.Koreo.Demos
  7. {
  8. [RequireComponent(typeof(Rigidbody))]
  9. [AddComponentMenu("Koreographer/Demos/Musical Impulse")]
  10. public class MusicalImpulse : MonoBehaviour
  11. {
  12. [EventID]
  13. public string eventID;
  14. public float jumpSpeed = 3f;
  15. Rigidbody rigidbodyCom;
  16. void Start()
  17. {
  18. // Register for Koreography Events. This sets up the callback.
  19. Koreographer.Instance.RegisterForEvents(eventID, AddImpulse);
  20. rigidbodyCom = GetComponent<Rigidbody>();
  21. }
  22. void OnDestroy()
  23. {
  24. // Sometimes the Koreographer Instance gets cleaned up before hand.
  25. // No need to worry in that case.
  26. if (Koreographer.Instance != null)
  27. {
  28. Koreographer.Instance.UnregisterForAllEvents(this);
  29. }
  30. }
  31. void AddImpulse(KoreographyEvent evt)
  32. {
  33. // Add impulse by overriding the Vertical component of the Velocity.
  34. Vector3 vel = rigidbodyCom.velocity;
  35. //vel.y = jumpSpeed + evt.GetFloatValue();
  36. vel.y = jumpSpeed ;
  37. rigidbodyCom.velocity = vel;
  38. }
  39. }
  40. }

 

6)运行场景,效果如上

3、粒子特效根据节拍设置的时间span,来控制粒子发射

1)在场景中,添加两个粒子效果

2)同理新建 Koreohgraphy Track,并设置到  Koreohgraphy 上

 

3)编辑 Koreohgraphy 对应 Koreohgraphy Track 的节拍事件,这里根据节拍,设置时间跨度

4)新建脚本,根据时间跨度来设置粒子发射数量,脚本挂在到粒子上,并对应赋值

 

  1. //----------------------------------------------
  2. // Koreographer
  3. // Copyright © 2014-2020 Sonic Bloom, LLC
  4. //----------------------------------------------
  5. using UnityEngine;
  6. namespace SonicBloom.Koreo.Demos
  7. {
  8. [RequireComponent(typeof(ParticleSystem))]
  9. [AddComponentMenu("Koreographer/Demos/Emit Particles On Span")]
  10. public class EmitParticlesOnSpan : MonoBehaviour
  11. {
  12. [EventID]
  13. public string eventID;
  14. public float particlesPerBeat = 100;
  15. ParticleSystem particleCom;
  16. int lastEmitFrame = -1;
  17. void Start()
  18. {
  19. particleCom = GetComponent<ParticleSystem>();
  20. // Register for Koreography Events. This sets up the callback.
  21. Koreographer.Instance.RegisterForEvents(eventID, OnParticleControlEvent);
  22. }
  23. void OnDestroy()
  24. {
  25. // Sometimes the Koreographer Instance gets cleaned up before hand.
  26. // No need to worry in that case.
  27. if (Koreographer.Instance != null)
  28. {
  29. Koreographer.Instance.UnregisterForAllEvents(this);
  30. }
  31. }
  32. void OnParticleControlEvent(KoreographyEvent evt)
  33. {
  34. // If two Koreography span events overlap, this can be called twice in the same frame.
  35. // This check ensures that we only ask the particle system to emit once for any frame.
  36. if (Time.frameCount != lastEmitFrame)
  37. {
  38. // Spans get called over a specified amount of music time. Use Koreographer's beat delta
  39. // to calculate the number of particles to emit this frame based on the "particlesPerBeat"
  40. // rate configured in the Inspector.
  41. int particleCount = (int)(particlesPerBeat * Koreographer.GetBeatTimeDelta());
  42. Debug.Log(GetType()+ "/OnParticleControlEvent()/ Koreographer.GetBeatTimeDelta:" + Koreographer.GetBeatTimeDelta());
  43. Debug.Log(GetType()+ "/OnParticleControlEvent()/ particleCount:" + particleCount);
  44. // Emit the calculated number of particles!
  45. particleCom.Emit(particleCount);
  46. lastEmitFrame = Time.frameCount;
  47. }
  48. }
  49. }
  50. }

5)运行场景,效果如上

4、正方体逐渐变化的效果

1)在场景中,添加两个靠边 Cube

2)同理新建 Koreohgraphy Track,并设置到  Koreohgraphy 上

 

3)编辑 Koreohgraphy 对应 Koreohgraphy Track 的节拍事件,这里根据节拍,设置时间跨度上的Curve 值

 

4)新建脚本,根据时间跨度上的Curve 的 Value来动态变化 Cube 大小,脚本挂在到Cube上,并对应赋值

  1. //----------------------------------------------
  2. // Koreographer
  3. // Copyright © 2014-2020 Sonic Bloom, LLC
  4. //----------------------------------------------
  5. using UnityEngine;
  6. namespace SonicBloom.Koreo.Demos
  7. {
  8. [AddComponentMenu("Koreographer/Demos/Cube Scaler")]
  9. public class CubeScaler : MonoBehaviour
  10. {
  11. [EventID]
  12. public string eventID;
  13. public float minScale = 0.5f;
  14. public float maxScale = 1.5f;
  15. void Start()
  16. {
  17. // Register for Koreography Events. This sets up the callback.
  18. Koreographer.Instance.RegisterForEventsWithTime(eventID, AdjustScale);
  19. }
  20. void OnDestroy()
  21. {
  22. // Sometimes the Koreographer Instance gets cleaned up before hand.
  23. // No need to worry in that case.
  24. if (Koreographer.Instance != null)
  25. {
  26. Koreographer.Instance.UnregisterForAllEvents(this);
  27. }
  28. }
  29. void AdjustScale(KoreographyEvent evt, int sampleTime, int sampleDelta, DeltaSlice deltaSlice)
  30. {
  31. if (evt.HasCurvePayload())
  32. {
  33. // Get the value of the curve at the current audio position. This will be a
  34. // value between [0, 1] and will be used, below, to interpolate between
  35. // minScale and maxScale.
  36. float curveValue = evt.GetValueOfCurveAtTime(sampleTime);
  37. transform.localScale = Vector3.one * Mathf.Lerp(minScale, maxScale, curveValue);
  38. Debug.Log(GetType()+ "/AdjustScale()/ curveValue : "+ curveValue);
  39. Debug.Log(GetType()+ "/AdjustScale()/ transform.localScale : " + transform.localScale);
  40. }
  41. }
  42. }
  43. }

 

5)运行场景,效果如上

5、节拍上的文字获取并展示效果

1)场景新建一个GameObject(这里用的 GUI,你也可以使用 UGUI)

 

 2)同理新建 Koreohgraphy Track,并设置到  Koreohgraphy 上

3)编辑 Koreohgraphy 对应 Koreohgraphy Track 的节拍事件,这里根据节拍,设置时间跨度上的Text 值

4)新建脚本,根据时间跨度上的 Text的 Value来动态设置 GUI 文本,脚本挂在到场景中,并对应赋值

  1. //----------------------------------------------
  2. // Koreographer
  3. // Copyright © 2014-2020 Sonic Bloom, LLC
  4. //----------------------------------------------
  5. using UnityEngine;
  6. namespace SonicBloom.Koreo.Demos
  7. {
  8. [AddComponentMenu("Koreographer/Demos/UI Message Setter")]
  9. public class UIMessageSetter : MonoBehaviour
  10. {
  11. [EventID]
  12. public string eventID;
  13. public GUIStyle style;
  14. KoreographyEvent curTextEvent;
  15. void Start()
  16. {
  17. // Register for Koreography Events. This sets up the callback.
  18. Koreographer.Instance.RegisterForEventsWithTime(eventID, UpdateText);
  19. }
  20. void OnDestroy()
  21. {
  22. // Sometimes the Koreographer Instance gets cleaned up before hand.
  23. // No need to worry in that case.
  24. if (Koreographer.Instance != null)
  25. {
  26. Koreographer.Instance.UnregisterForAllEvents(this);
  27. }
  28. }
  29. void OnGUI()
  30. {
  31. if (curTextEvent != null)
  32. {
  33. // Use the entire screen size as the draw surface. Draw location is determined by
  34. // the GUIStyle.
  35. GUI.Box(new Rect(0,0, Screen.width, Screen.height), curTextEvent.GetTextValue(), style);
  36. }
  37. }
  38. void UpdateText(KoreographyEvent evt, int sampleTime, int sampleDelta, DeltaSlice deltaSlice)
  39. {
  40. // Verify that we have Text in the Payload.
  41. if (evt.HasTextPayload())
  42. {
  43. // Set the text if we have a text event!
  44. // We can get multiple events called at the same time (if they overlap in the track).
  45. // In this case, we prefer the event with the most recent start sample.
  46. if (curTextEvent == null ||
  47. (evt != curTextEvent && evt.StartSample > curTextEvent.StartSample))
  48. {
  49. // Store for later use and comparison.
  50. curTextEvent = evt;
  51. }
  52. // Clear out the text if our event ended this musical frame.
  53. if (curTextEvent.EndSample < sampleTime)
  54. {
  55. // Remove so that the above timing logic works when the audio loops/jumps.
  56. curTextEvent = null;
  57. }
  58. }
  59. }
  60. }
  61. }

5)运行场景,效果如上

6、灯光随着节拍一闪一闪的效果

 

1)在场景中添加 几盏 SpotLight,布局

 

2)新建脚本,根据Koreographer.GetBeatTime() 获取拍子,然后对应开关等即可

  1. //----------------------------------------------
  2. // Koreographer
  3. // Copyright © 2014-2020 Sonic Bloom, LLC
  4. //----------------------------------------------
  5. using UnityEngine;
  6. namespace SonicBloom.Koreo.Demos
  7. {
  8. [AddComponentMenu("Koreographer/Demos/Tempo Switch")]
  9. public class TempoSwitch : MonoBehaviour
  10. {
  11. // These should be set up in the inspector.
  12. public Behaviour[] quarterNoteGroup;
  13. public Behaviour[] eighthNoteGroup;
  14. int lastQuarterNote = 0;
  15. int lastEighthNote = -1;
  16. void Update()
  17. {
  18. // The Demo song has a quarter note as it's beat value. This will get us the current
  19. // quarter note!
  20. int curQuarterNote = Mathf.FloorToInt(Koreographer.GetBeatTime());
  21. if (curQuarterNote != lastQuarterNote)
  22. {
  23. // Turn the group on when the beat is an even number.
  24. SwitchGroup(quarterNoteGroup, lastQuarterNote % 2 != 0);
  25. lastQuarterNote = curQuarterNote;
  26. }
  27. // The 'null' value asks Koreographer to look at the beat time of what it considers
  28. // the current "Main" song. These demos use a basic player with a single song and
  29. // define that as the Main song. Therefore there is no need to specify it. The
  30. // '2' parameter, tells Koreographer to divide each beat into 2 equal parts. As the
  31. // base beat value is 4, this will result in eighth notes.
  32. int curEighthNote = Mathf.FloorToInt(Koreographer.GetBeatTime(null, 2));
  33. if (curEighthNote != lastEighthNote)
  34. {
  35. SwitchGroup(eighthNoteGroup, lastEighthNote % 2 != 0);
  36. lastEighthNote = curEighthNote;
  37. }
  38. }
  39. void SwitchGroup(Behaviour[] behaviours, bool bGroupOn)
  40. {
  41. for (int i = 0; i < behaviours.Length; ++i)
  42. {
  43. behaviours[i].enabled = bGroupOn;
  44. }
  45. }
  46. }
  47. }

3)运行场景,效果如上

7、随着节拍颜色的动态变化

 

1)场景使用之前球和正方体的结合场景,新建GameObject物体,来监控颜色变化挂载物体

 2)同理新建 两个 Koreohgraphy Track(一个颜色骤变,一个颜色渐变),并设置到  Koreohgraphy 上

 

3)编辑 Koreohgraphy 对应 Koreohgraphy Track 的节拍事件,这里根据节拍,设置 Color 和 Gradient

 

5)新建脚本,根据节拍设置颜色,脚本挂在到场景中,并对应赋值

  1. //----------------------------------------------
  2. // Koreographer
  3. // Copyright © 2014-2020 Sonic Bloom, LLC
  4. //----------------------------------------------
  5. using UnityEngine;
  6. namespace SonicBloom.Koreo.Demos
  7. {
  8. [AddComponentMenu("Koreographer/Demos/Color Adjuster")]
  9. public class ColorAdjuster : MonoBehaviour
  10. {
  11. [EventID]
  12. public string eventID;
  13. public Renderer[] objectsToColor;
  14. void Start()
  15. {
  16. // Register for Koreography Events. This sets up the callback.
  17. Koreographer.Instance.RegisterForEventsWithTime(eventID, AdjustColor);
  18. }
  19. void OnDestroy()
  20. {
  21. // Sometimes the Koreographer Instance gets cleaned up before hand.
  22. // No need to worry in that case.
  23. if (Koreographer.Instance != null)
  24. {
  25. Koreographer.Instance.UnregisterForAllEvents(this);
  26. }
  27. }
  28. void AdjustColor(KoreographyEvent evt, int sampleTime, int sampleDelta, DeltaSlice deltaSlice)
  29. {
  30. // We have prepared two kinds of events that work with this system:
  31. // 1) OneOffs that store a Color.
  32. // 2) Spans that store a Gradient.
  33. // Ensure that we have the correct types before proceeding!
  34. if (evt.IsOneOff() && evt.HasColorPayload())
  35. {
  36. // This is a simple Color Payload.
  37. Color targetColor = evt.GetColorValue();
  38. ApplyColorToObjects(ref targetColor);
  39. }
  40. else if (!evt.IsOneOff() && evt.HasGradientPayload())
  41. {
  42. // Access the color specified at the current music-time. This is what
  43. // drives musical color animations from gradients!
  44. Color targetColor = evt.GetColorOfGradientAtTime(sampleTime);
  45. ApplyColorToObjects(ref targetColor);
  46. }
  47. }
  48. void ApplyColorToObjects(ref Color color)
  49. {
  50. for (int i = 0; i < objectsToColor.Length; ++i)
  51. {
  52. objectsToColor[i].material.color = color;
  53. }
  54. }
  55. }
  56. }

5)运行场景,效果如上

8、全部场景效果叠加起来,就是Demo的整体效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/118436
推荐阅读
相关标签
  

闽ICP备14008679号