Effect:
Production of ShaderGraph:
The uv number in the shader and the uv number of the Sprite map can be combined to produce the effect.
Go directly to the code:
using System. Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TurnDirEffectController : MonoBehaviour {<!-- --> public RectTransform effectRrectTf; public Material mat; public int currentLevel = -1; public float totalTime; public float currentSpeed; public Texture2D[] textures; public float[] mainColorAlpha; public bool isEffectOver; public int trunValue = -2; public Button[] testBtn; public Sprite dayimg; public Sprite nightimg; void Start() {<!-- --> Application.targetFrameRate = 24; StopTween(); testBtn[0].onClick.AddListener(() => Btn1()); testBtn[1].onClick.AddListener(() => {<!-- --> Btn2(); }); testBtn[2].onClick.AddListener(() => {<!-- --> Btn3(); }); testBtn[3].onClick.AddListener(() => {<!-- --> Btn4(); }); testBtn[4].onClick.AddListener(() => {<!-- --> Btn5(); }); } public void StopTween() {<!-- --> trunValue = -2; mat.SetFloat("_alpha", 0); totalTime = 0; } float time; float time1; float time2; float time3; private DayOrNightMode mo; void Update() {<!-- --> #if UNITY_EDITOR if (Input. GetKeyDown(KeyCode. Z)) {<!-- --> mo = DayOrNightMode. Day; Turn_new2(0, 0, mo);//Left 1 } if (Input. GetKeyDown(KeyCode. X)) {<!-- --> mo = DayOrNightMode. Day; Turn_new2(1, 0, mo);//right 1 } if (Input. GetKeyDown(KeyCode. C)) {<!-- --> mo = DayOrNightMode. Day; Turn_new2(0, 1, mo);//Left 2 } if (Input. GetKeyDown(KeyCode. V)) {<!-- --> mo = DayOrNightMode. Day; Turn_new2(1, 1, mo);//right 2 } if (Input. GetKeyDown(KeyCode. B)) {<!-- --> trunValue = -2; } #endif //time + = Time.deltaTime; //time1 + = Time.deltaTime; //time2 + = Time.deltaTime; //time3 + = Time.deltaTime; //if (time > 5 & amp; & amp; time < 11) //{<!-- --> // Turn_new2(-1, 1, DayOrNightMode. Day); // time = 11; //} //if (time1 > 11 & amp; & amp; time1 < 17) //{<!-- --> // Turn_new2(1, 1, DayOrNightMode.Day); // Right 1 // time1 = 17; //} //if (time2 > 17 & amp; & amp; time2 < 25) //{<!-- --> // Turn_new2(-1, 2, DayOrNightMode.Day); // Left 2 // time2 = 25; //} //if (time3 > 25 & amp; & amp; time3 < 33) //{<!-- --> // Turn_new2(1, 2, DayOrNightMode.Day); // Right 2 // //time3 = 33; //} //if (time3 > 33) //{<!-- --> // Btn5(); //} if (isEffectOver) {<!-- --> return; } totalTime + = Time.deltaTime * currentSpeed; //Judge at the end of each cycle if (totalTime >= 48f) {<!-- --> if (trunValue == -2) {<!-- --> totalTime = 0; effectRrectTf.gameObject.SetActive(false); isEffectOver = true; } else {<!-- --> totalTime = 0; if(trunValue == 0) {<!-- --> effectRrectTf.localScale = new Vector3(-1, 1, 1); } if (trunValue == 1) {<!-- --> effectRrectTf.localScale = new Vector3(1, 1, 1); } } //1 second 30 frames, each frame time 100/3=33; float picturePreSecont = 33; if (currentLevel == -1) {<!-- --> speedTye = SpeedType. normal; currentSpeed = picturePreSecont / 1; } else if (currentLevel == 0) {<!-- --> speedTye = SpeedType. high; currentSpeed = picturePreSecont / 1.5f; } else if (currentLevel == 1) {<!-- --> speedTye = SpeedType. low; currentSpeed = picturePreSecont / 2.5f; } OnDayOrNight(mo, currentLevel); } totalTime %= 48; mat.SetFloat("_timer", totalTime); } public void naviLightMessage(string data) {<!-- --> //data=0-1-1 The first switch navigation 0=off, 1=on The second means left and right 0=left 1=right The third means fast and slow 0=fast 1=slow Debug.Log("receiveNaviMessage params=" + data); string[] value = data. Split('-'); if (int. Parse(value[0])==0) {<!-- --> trunValue = -2; } else if(int. Parse(value[0]) == 1) {<!-- --> Turn_new2(int.Parse(value[1]), int.Parse(value[2]), DayOrNightMode.Day); } } public void Turn_new2(int value, int speedValue, DayOrNightMode mode) {<!-- --> //Application.targetFrameRate = 24; currentLevel = speedValue; if (trunValue == value) {<!-- --> //MR_Debug.LogI(null, "TrunValue==value and return" + value); return; } trunValue = value; if (trunValue == -2) {<!-- --> return; } //DayNight(mode); if (trunValue == 0) {<!-- --> effectRrectTf.localScale = new Vector3(-1, 1, 1); } if (trunValue == 1) {<!-- --> effectRrectTf.localScale = new Vector3(1, 1, 1); } //effectRrectTf.localScale = new Vector3(trunValue, 1, 1); //currentLevel = speedValue; totalTime = 0; //0 normal 1 high 2 low //1 second 30 frames, each frame time 100/3=33; float picturePreSecont = 33; if (currentLevel == -1) {<!-- --> speedTye = SpeedType. normal; currentSpeed = picturePreSecont / 1; } else if (currentLevel == 0) {<!-- --> speedTye = SpeedType. high; currentSpeed = picturePreSecont / 1.5f; } else if (currentLevel == 1) {<!-- --> speedTye = SpeedType. low; currentSpeed = picturePreSecont / 2.5f; } //Replay OnDayOrNight(mode, speedValue); isEffectOver = false; effectRrectTf.gameObject.SetActive(true); } private void OnDayOrNight(DayOrNightMode mode, int speedValue) {<!-- --> //Replay // totalTime = 0; mat.SetFloat("_alpha", 1); //Change the texture and transparency if (mode == DayOrNightMode. Day) {<!-- --> //if (speedValue == 0) //{<!-- --> // mat. SetTexture("_MainTex", textures[0]); //} //else if(speedValue == 1) //{<!-- --> // mat. SetTexture("_MainTex", textures[1]); //} mat.SetTexture("_MainTex", textures[0]); if (speedType == SpeedType.low) {<!-- --> mat.SetFloat("_alpha", mainColorAlpha[1]); } else if(speedType == SpeedType.high) {<!-- --> mat.SetFloat("_alpha", mainColorAlpha[0]); } } else if (mode == DayOrNightMode. Night) {<!-- --> //if (speedValue == 0) //{<!-- --> // mat. SetTexture("_MainTex", textures[2]); //} //else if (speedValue == 1) //{<!-- --> // mat. SetTexture("_MainTex", textures[3]); //} mat.SetTexture("_MainTex", textures[1]); if (speedType == SpeedType.low) {<!-- --> mat.SetFloat("_alpha", mainColorAlpha[1]); } else if (speedType == SpeedType. high) {<!-- --> mat.SetFloat("_alpha", mainColorAlpha[0]); } } } private void Btn1() {<!-- --> Turn_new2(0, 0, DayOrNightMode. Day); } private void Btn2() {<!-- --> Turn_new2(1, 0, DayOrNightMode. Day); } private void Btn3() {<!-- --> Turn_new2(0, 1, DayOrNightMode.Day);//Left 2 } private void Btn4() {<!-- --> Turn_new2(1, 1, DayOrNightMode.Day);//right 2 } private void Btn5() {<!-- --> trunValue = -2; } private void DayNight(DayOrNightMode mode) {<!-- --> if(mode== DayOrNightMode. Day) {<!-- --> transform.GetComponent<Image>().sprite = dayimg; } else if(mode == DayOrNightMode. Night) {<!-- --> transform.GetComponent<Image>().sprite = nightimg; } } public enum SpeedType {<!-- --> normal, high, low } public SpeedType speedTye; public enum DayOrNightMode {<!-- --> Day, Night, } }
Code binding:
Here is the sprite image: