2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > unity(Koreographer实现官网的案例)

unity(Koreographer实现官网的案例)

时间:2019-01-16 22:17:40

相关推荐

unity(Koreographer实现官网的案例)

——个人笔记

不懂怎么操作,可以看看插件Koreographer介绍使用

实现球按节奏的跳动的脚本(轨迹要听音乐编辑):

public class BallTest : MonoBehaviour {private Rigidbody rigidbodyCom;public string eventID;public float jumpSpeed;void Start () {rigidbodyCom = GetComponent<Rigidbody>();Koreographer.Instance.RegisterForEvents(eventID,BallJump);}private void BallJump(KoreographyEvent koreographyEvent){Vector3 vel = rigidbodyCom.velocity;vel.y = jumpSpeed;rigidbodyCom.velocity = vel;}}

实现Cube根据节奏变大变小代码(轨迹编辑要使用负荷Curve,线段来实现):

public class CubeTest : MonoBehaviour {public string eventID;public float minScale = 0.5f;public float maxScale = 1.5f;void Start () {Koreographer.Instance.RegisterForEventsWithTime(eventID, ChangeCube);}private void ChangeCube(KoreographyEvent koreographyEvent, int sampleTime, int sampleDelta, DeltaSlice deltaSlice){if (koreographyEvent.HasCurvePayload()){float curveValue = koreographyEvent.GetValueOfCurveAtTime(sampleTime);transform.localScale = Vector3.one * Mathf.Lerp(minScale, maxScale, curveValue);}}}

按照四、八音符的灯光闪烁(不用使用到注册事件):

public class LightTest : MonoBehaviour {public Light[] quarterNoteGroup;//四分音符对应的灯组public Light[] eighthNoteGroup;//八分音符对应的灯组private int lastQuarterNote = 0;//上一个四分音符private int lastEigthNote = 0;//上一个八分音符void Update () {int curQuarterNote =Mathf.FloorToInt(Koreographer.GetBeatTime());if (curQuarterNote!=lastQuarterNote){//当前的四分音符与上一个四分音符不是同一个四分音符的时候//进行开灯关灯的操作。SwitchGroup(quarterNoteGroup, lastQuarterNote % 2 != 0);lastQuarterNote = curQuarterNote;}int curEighthNote= Mathf.FloorToInt(Koreographer.GetBeatTime(null,2));if (curEighthNote!=lastEigthNote){SwitchGroup(eighthNoteGroup, lastEigthNote % 2 != 0);lastEigthNote = curEighthNote;}}private void SwitchGroup(Light[] lights,bool ifOpen){for (int i = 0; i < lights.Length; i++){lights[i].enabled = ifOpen;}}}

按照节奏的粒子跳动效果:

public class ParticlesTest : MonoBehaviour {public string eventID;public float particlesPerBeat = 100;private ParticleSystem particleSystemCom;void Start () {particleSystemCom = GetComponent<ParticleSystem>();Koreographer.Instance.RegisterForEvents(eventID, CreateParticle);}private void CreateParticle(KoreographyEvent koreographyEvent){int particleCount=(int)(Koreographer.GetBeatTimeDelta()*particlesPerBeat);particleSystemCom.Emit(particleCount);}}

按照歌曲出现歌词(使用负荷Text):

public class TextTest : MonoBehaviour {public string eventID;private Text text;private KoreographyEvent curTextEvent;// Use this for initializationvoid Start () {text = GetComponent<Text>();Koreographer.Instance.RegisterForEventsWithTime(eventID, UpdateText);}private void UpdateText(KoreographyEvent koreographyEvent, int sampleTime, int sampleDelta, DeltaSlice deltaSlice){//判断当前事件是否有文本负荷if (koreographyEvent.HasTextPayload()){//当前存贮事件是否为空或者当前存贮事件不等于我们检测到的事件并且后边的事件为我们新检测到的事件if (curTextEvent==null||(koreographyEvent!=curTextEvent&&koreographyEvent.StartSample>curTextEvent.StartSample)){//文本的更新text.text = koreographyEvent.GetTextValue();curTextEvent = koreographyEvent;}//当前的音乐节奏执行拍子大于我们存贮事件的最后一拍的时候if (curTextEvent.EndSample < sampleTime){//滞空text.text = string.Empty;curTextEvent = null;}}}}

根据歌曲颜色渐变和突变的效果(渐变要使用负荷Gradient):

public class ColorJump : MonoBehaviour {public string eventID;public Renderer[] objectsToColor;void Start () {Koreographer.Instance.RegisterForEventsWithTime(eventID,AdjustColor);}private void AdjustColor(KoreographyEvent koreographyEvent,int sampleTime,int sampleDelta,DeltaSlice deltaSlice){if (koreographyEvent.HasColorPayload()){Color targetColor = koreographyEvent.GetColorValue();ApplyColorToOjects(targetColor);}else if (koreographyEvent.HasGradientPayload()){Color targetColor = koreographyEvent.GetColorOfGradientAtTime(sampleTime);ApplyColorToOjects(targetColor);}}private void ApplyColorToOjects(Color color){for (int i = 0; i < objectsToColor.Length; i++){objectsToColor[i].material.color = color;}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。