/// <summary> /// 初始化状态机 /// </summary> private void InitFSM() { changeIntensity = new State("ChangeIntensity"); changeIntensity.OnEnter += (IState state) => { isReset = true; isAnimation = true; }; changeIntensity.OnUpdate += (float f) => { if (isAnimation) { if (isReset) { if (FadeTo(MaxIntensity)) { isReset = false; isAnimation = false; } } else { if (FadeTo(target)) { isReset = true; } } } else { target = Random.Range(0.3f, 0.7f); isAnimation = true; } }; changeColor = new State("ChangeColor"); changeColor.OnEnter += (IState state) => { isAnimation = false; }; changeColor.OnUpdate += (float f) => { if (isAnimation) { if (colorTimer >= 1f) { isAnimation = false; } else { colorTimer += Time.deltaTime * 1f; myLight.color = Color.Lerp(startColor, targetColor, colorTimer); } } else { float r = Random.Range(0f, 1f); float g = Random.Range(0f, 1f); float b = Random.Range(0f, 1f); targetColor = new Color(r, g, b); startColor = myLight.color; colorTimer = 0f; isAnimation = true; } }; color2Intensity = new Transition(changeColor, changeIntensity); color2Intensity.OnCheck += () => { return(!isChangeColor); }; changeColor.AddTransition(color2Intensity); intensity2color = new Transition(changeIntensity, changeColor); intensity2color.OnCheck += () => { return(isChangeColor); }; changeIntensity.AddTransition(intensity2color); open = new FSMMachine("Open", changeIntensity); open.AddState(changeColor); open.OnEnter += (IState state) => { myLight.intensity = MaxIntensity; }; close = new State("Close"); close.OnEnter += (IState state) => { myLight.intensity = 0f; }; open2Close = new Transition(open, close); open2Close.OnCheck += () => { return(!isOpen); }; open2Close.OnTransition += () => { return(FadeTo(0f)); }; open.AddTransition(open2Close); close2Open = new Transition(close, open); close2Open.OnCheck += () => { return(isOpen); }; close2Open.OnTransition += () => { return(FadeTo(MaxIntensity)); }; close.AddTransition(close2Open); lightFSM = new FSMMachine("LightFSM", open); lightFSM.AddState(close); }