From 6a98fd3c443bec2f50d411c6708c36a06b678bf4 Mon Sep 17 00:00:00 2001 From: mahdi bazei Date: Mon, 29 Sep 2025 23:58:33 +0330 Subject: [PATCH] Update ToggleController.cs add UnityEvent --- Toggle/Assets/Scripts/ToggleController.cs | 288 ++++++++++------------ 1 file changed, 136 insertions(+), 152 deletions(-) diff --git a/Toggle/Assets/Scripts/ToggleController.cs b/Toggle/Assets/Scripts/ToggleController.cs index b35cfc1..92ad8b4 100644 --- a/Toggle/Assets/Scripts/ToggleController.cs +++ b/Toggle/Assets/Scripts/ToggleController.cs @@ -1,158 +1,142 @@ -using System.Collections; +using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; +using UnityEngine.Events; -public class ToggleController : MonoBehaviour +public class ToggleController : MonoBehaviour { - public bool isOn; - - public Color onColorBg; - public Color offColorBg; - - public Image toggleBgImage; - public RectTransform toggle; - - public GameObject handle; - private RectTransform handleTransform; - - private float handleSize; - private float onPosX; - private float offPosX; - - public float handleOffset; - - public GameObject onIcon; - public GameObject offIcon; - - - public float speed; - static float t = 0.0f; - - private bool switching = false; - - - void Awake() - { - handleTransform = handle.GetComponent(); - RectTransform handleRect = handle.GetComponent(); - handleSize = handleRect.sizeDelta.x; - float toggleSizeX = toggle.sizeDelta.x; - onPosX = (toggleSizeX / 2) - (handleSize/2) - handleOffset; - offPosX = onPosX * -1; - - } - - - void Start() - { - if(isOn) - { - toggleBgImage.color = onColorBg; - handleTransform.localPosition = new Vector3(onPosX, 0f, 0f); - onIcon.gameObject.SetActive(true); - offIcon.gameObject.SetActive(false); - } - else - { - toggleBgImage.color = offColorBg; - handleTransform.localPosition = new Vector3(offPosX, 0f, 0f); - onIcon.gameObject.SetActive(false); - offIcon.gameObject.SetActive(true); - } - } - - void Update() - { - - if(switching) - { - Toggle(isOn); - } - } - - public void DoYourStaff() - { - Debug.Log(isOn); - } - - public void Switching() - { - switching = true; - } - - - - public void Toggle(bool toggleStatus) - { - if(!onIcon.active || !offIcon.active) - { - onIcon.SetActive(true); - offIcon.SetActive(true); - } - - if(toggleStatus) - { - toggleBgImage.color = SmoothColor(onColorBg, offColorBg); - Transparency (onIcon, 1f, 0f); - Transparency (offIcon, 0f, 1f); - handleTransform.localPosition = SmoothMove(handle, onPosX, offPosX); - } - else - { - toggleBgImage.color = SmoothColor(offColorBg, onColorBg); - Transparency (onIcon, 0f, 1f); - Transparency (offIcon, 1f, 0f); - handleTransform.localPosition = SmoothMove(handle, offPosX, onPosX); - } - - } - - - Vector3 SmoothMove(GameObject toggleHandle, float startPosX, float endPosX) - { - - Vector3 position = new Vector3 (Mathf.Lerp(startPosX, endPosX, t += speed * Time.deltaTime), 0f, 0f); - StopSwitching(); - return position; - } - - Color SmoothColor(Color startCol, Color endCol) - { - Color resultCol; - resultCol = Color.Lerp(startCol, endCol, t += speed * Time.deltaTime); - return resultCol; - } - - CanvasGroup Transparency (GameObject alphaObj, float startAlpha, float endAlpha) - { - CanvasGroup alphaVal; - alphaVal = alphaObj.gameObject.GetComponent(); - alphaVal.alpha = Mathf.Lerp(startAlpha, endAlpha, t += speed * Time.deltaTime); - return alphaVal; - } - - void StopSwitching() - { - if(t > 1.0f) - { - switching = false; - - t = 0.0f; - switch(isOn) - { - case true: - isOn = false; - DoYourStaff(); - break; - - case false: - isOn = true; - DoYourStaff(); - break; - } - - } - } - + public bool isOn; + + public Color onColorBg; + public Color offColorBg; + + public Image toggleBgImage; + public RectTransform toggle; + + public GameObject handle; + private RectTransform handleTransform; + + private float handleSize; + private float onPosX; + private float offPosX; + + public float handleOffset; + + public GameObject onIcon; + public GameObject offIcon; + + public float speed; + static float t = 0.0f; + + private bool switching = false; + + + [Header("Toggle Events")] + public UnityEvent OnToggleOn; + public UnityEvent OnToggleOff; + + void Awake() + { + handleTransform = handle.GetComponent(); + RectTransform handleRect = handle.GetComponent(); + handleSize = handleRect.sizeDelta.x; + float toggleSizeX = toggle.sizeDelta.x; + onPosX = (toggleSizeX / 2) - (handleSize / 2) - handleOffset; + offPosX = onPosX * -1; + } + + void Start() + { + if (isOn) + { + toggleBgImage.color = onColorBg; + handleTransform.localPosition = new Vector3(onPosX, 0f, 0f); + onIcon.gameObject.SetActive(true); + offIcon.gameObject.SetActive(false); + } + else + { + toggleBgImage.color = offColorBg; + handleTransform.localPosition = new Vector3(offPosX, 0f, 0f); + onIcon.gameObject.SetActive(false); + offIcon.gameObject.SetActive(true); + } + } + + void Update() + { + if (switching) + { + Toggle(isOn); + } + } + + public void Switching() + { + switching = true; + } + + public void Toggle(bool toggleStatus) + { + if (!onIcon.active || !offIcon.active) + { + onIcon.SetActive(true); + offIcon.SetActive(true); + } + + if (toggleStatus) + { + toggleBgImage.color = SmoothColor(onColorBg, offColorBg); + Transparency(onIcon, 1f, 0f); + Transparency(offIcon, 0f, 1f); + handleTransform.localPosition = SmoothMove(handle, onPosX, offPosX); + } + else + { + toggleBgImage.color = SmoothColor(offColorBg, onColorBg); + Transparency(onIcon, 0f, 1f); + Transparency(offIcon, 1f, 0f); + handleTransform.localPosition = SmoothMove(handle, offPosX, onPosX); + } + } + + Vector3 SmoothMove(GameObject toggleHandle, float startPosX, float endPosX) + { + Vector3 position = new Vector3(Mathf.Lerp(startPosX, endPosX, t += speed * Time.deltaTime), 0f, 0f); + StopSwitching(); + return position; + } + + Color SmoothColor(Color startCol, Color endCol) + { + return Color.Lerp(startCol, endCol, t += speed * Time.deltaTime); + } + + CanvasGroup Transparency(GameObject alphaObj, float startAlpha, float endAlpha) + { + CanvasGroup alphaVal = alphaObj.gameObject.GetComponent(); + alphaVal.alpha = Mathf.Lerp(startAlpha, endAlpha, t += speed * Time.deltaTime); + return alphaVal; + } + + void StopSwitching() + { + if (t > 1.0f) + { + switching = false; + t = 0.0f; + + if (isOn) + { + isOn = false; + OnToggleOff?.Invoke(); + } + else + { + isOn = true; + OnToggleOn?.Invoke(); + } + } + } }