Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
288 changes: 136 additions & 152 deletions Toggle/Assets/Scripts/ToggleController.cs
Original file line number Diff line number Diff line change
@@ -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>();
RectTransform handleRect = handle.GetComponent<RectTransform>();
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<CanvasGroup>();
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>();
RectTransform handleRect = handle.GetComponent<RectTransform>();
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<CanvasGroup>();
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();
}
}
}
}