using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static PerformanceAdjusting;
//using TMPro;
//using UIFramework;
//using System;
//using DG.Tweening;
//using UniRx;
public class ToggleController
{
private List _toggles;
private List
private int _lastIndex;
public ToggleController(List<Toggle> toggles)
{
_toggles = toggles;
_lastIndex = 0;
}
public void AddListener(FunctionID id)
{
for (int i = 0; i < _toggles.Count; i++)
{
ToggleID j = (ToggleID)i;
_toggles[i].onValueChanged.AddListener((bool isOn) =>
{
// 发送消息
SuperSportNative.SetInt((int)id, DataCenter.toggleMapping[id][j]);
// 切换元素状态 文本字体颜色
HandleToggle(j);
});
}
}
private void HandleToggle(ToggleID index)
{
//foreach (Text txt in _toggles)
//{
//}
}
}
class SuperSportNative
{
public static void SetInt(int id, int param)
{
}
}
class DataCenter
{
public static Dictionary<FunctionID, Dictionary<ToggleID, int>> toggleMapping =
new Dictionary<FunctionID, Dictionary<ToggleID, int>>
{
{ FunctionID.ID1, new Dictionary<ToggleID, int>
{
{ ToggleID.Toggle_0, 5 },
{ ToggleID.Toggle_1, 6 },
{ ToggleID.Toggle_2, 7 },
}
},
{ FunctionID.ID2, new Dictionary<ToggleID, int>
{
{ ToggleID.Toggle_0, 3 },
{ ToggleID.Toggle_1, 6 },
{ ToggleID.Toggle_2, 1 },
}
}
};
}
public class PerformanceAdjusting : MonoBehaviour
{
[SerializeField] private List _performance1;
[SerializeField] private List _performance2;文章来源:https://www.toymoban.com/news/detail-823185.html
private ToggleController _controller1;
private ToggleController _controller2;
public enum FunctionID
{
ID1,
ID2
}
public enum ToggleID
{
Toggle_0,
Toggle_1,
Toggle_2,
Toggle_3,
Toggle_4
}
public Dictionary<FunctionID, Dictionary<ToggleID, int>> toggleMapping;
private void Awake()
{
toggleMapping = new Dictionary<FunctionID, Dictionary<ToggleID, int>>
{
{ FunctionID.ID1, new Dictionary<ToggleID, int>
{
{ ToggleID.Toggle_0, 5 },
{ ToggleID.Toggle_1, 6 },
{ ToggleID.Toggle_2, 7 },
}
},
{ FunctionID.ID2, new Dictionary<ToggleID, int>
{
{ ToggleID.Toggle_0, 3 },
{ ToggleID.Toggle_1, 6 },
{ ToggleID.Toggle_2, 1 },
}
}
};
_controller1 = new ToggleController(_performance1);
_controller2 = new ToggleController(_performance2);
_controller1.AddListener(FunctionID.ID1);
}
性能调节
//[SerializeField]
//private GameObject m_PerformanceAdjusting;
性能调节 前后轴输出功率Toggle
//[SerializeField]
//private List<Toggle> m_FrontRearAxleOutputPowerToggles;
记录当前前后轴输出功率按钮位置
//private int m_FrontRearAxleOutputPowerIndex;
性能调节 前后轴输出功率比文本显示
//[SerializeField]
//private TextMeshProUGUI m_FrontRearAxleOutputPowerTxt;
//private string txt_Ratio;
性能调节 动力响应Toggle
//[SerializeField]
//private List<Toggle> m_DynamicResponseToggles;
记录当前动力响应按钮位置
//private int m_DynamicResponseIndex;
性能调节 动力响应Text
//[SerializeField]
//private List<TextMeshProUGUI> m_DynamicResponseTxts;
性能调节 制动反馈Toggle
//[SerializeField]
//private List<Toggle> m_BrakeFeedbackToggles;
记录当前按钮位置
//private int m_BrakeFeedbackIndex;
性能调节 制动反馈Text
//[SerializeField]
//private List<TextMeshProUGUI> m_BrakeFeedbackTxts;
性能调节 转向手感Toggle
//[SerializeField]
//private List<Toggle> m_SteeringFeelToggles;
记录当前转向手感按钮位置
//private int m_SteeringFeelIndex;
性能调节 转向手感Text
//[SerializeField]
//private List<TextMeshProUGUI> m_SteeringFeelTxts;
性能调节 悬架阻尼Toggle
//[SerializeField]
//private List<Toggle> m_SuspensionDampingToggles;
记录当前按钮位置
//private int m_SuspensionDampingIndex;
性能调节 悬架阻尼Text
//[SerializeField]
//private List<TextMeshProUGUI> m_SuspensionDampingTxts;
性能调节 车身稳定系统Toggle
//[SerializeField]
//private List<Toggle> m_BodyStabilizationSystemToggles;
记录当前按钮位置
//private int m_BodyStabilizationSystemIndex;
性能调节 车身稳定系统Text
//[SerializeField]
//private List<TextMeshProUGUI> m_BodyStabilizationSystemTxts;
性能调节 能量回收系统Toggle
//[SerializeField]
//private List<Toggle> m_EnergyRecoverySystemsToggles;
记录当前按钮位置
//private int m_EnergyRecoverySystemsIndex;
性能调节 能量回收系统Text
//[SerializeField]
//private List<TextMeshProUGUI> m_EnergyRecoverySystemsTxts;
性能调节 超强冷却系统Toggle
//[SerializeField]
//private List<Toggle> m_SuperCoolingSystemToggles;
记录当前按钮位置
//private int m_SuperCoolingSystemIndex;
性能调节 超强冷却系统Text
//[SerializeField]
//private List<TextMeshProUGUI> m_SuperCoolingSystemTxts;
性能调节 恢复为大师调校Button
//[SerializeField]
//private Button m_ReturnToMasterTuningBtn;
性能调节 弹窗其他区域点击关闭弹窗 Mask Button
//[SerializeField]
//private Button m_MaskCloseBtn;
//private static readonly string TAG = "PerformanceAdjusting";
//void Start()
//{
// //动态添加监听 前后轴输出功率
// for (int i = 0; i < m_FrontRearAxleOutputPowerToggles.Count; i++)
// {-
// int index = i;
// m_FrontRearAxleOutputPowerToggles[i].onValueChanged.AddListener((bool isOn) => { FrontRearAxleOutputPowerToggleAction(m_FrontRearAxleOutputPowerToggles[index], isOn, index); });
// }
// //动态添加监听 动力响应、转向手感、悬架阻尼、车身稳定系统
// for (int i = 0; i < m_DynamicResponseToggles.Count; i++)
// {
// int index = i;
// m_DynamicResponseToggles[i].onValueChanged.AddListener((bool isOn) => { AdjustingToggleAction(m_DynamicResponseToggles[index], isOn, index, m_DynamicResponseTxts, Convert.ToInt32(SignalEnum.performanceadjusting_dynamicresponse), index + 1, m_DynamicResponseIndex); });
// m_SteeringFeelToggles[i].onValueChanged.AddListener((bool isOn) => { AdjustingToggleAction(m_SteeringFeelToggles[index], isOn, index, m_SteeringFeelTxts, Convert.ToInt32(SignalEnum.performanceadjusting_steeringfeel), index + 1, m_SteeringFeelIndex); });
// m_SuspensionDampingToggles[i].onValueChanged.AddListener((bool isOn) => { AdjustingToggleAction(m_SuspensionDampingToggles[index], isOn, index, m_SuspensionDampingTxts, Convert.ToInt32(SignalEnum.performanceadjusting_suspensiondamping), index + 1, m_SuspensionDampingIndex); });
// m_BodyStabilizationSystemToggles[i].onValueChanged.AddListener((bool isOn) => { AdjustingToggleAction(m_BodyStabilizationSystemToggles[index], isOn, index, m_BodyStabilizationSystemTxts, Convert.ToInt32(SignalEnum.performanceadjusting_esp), index, m_BodyStabilizationSystemIndex); });
// }
// //动态添加监听 制动反馈
// for (int i = 0; i < m_BrakeFeedbackToggles.Count; i++)
// {
// int index = i;
// m_BrakeFeedbackToggles[i].onValueChanged.AddListener((bool isOn) => { BrakeFeedbackToggleAction(m_BrakeFeedbackToggles[index], isOn, index, m_BrakeFeedbackTxts, Convert.ToInt32(SignalEnum.performanceadjusting_brakefeedback), m_BrakeFeedbackIndex); });
// }
// //动态添加监听 能量回收系统
// for (int i = 0; i < m_EnergyRecoverySystemsToggles.Count; i++)
// {
// int index = i;
// m_EnergyRecoverySystemsToggles[i].onValueChanged.AddListener((bool isOn) => { EnergyRecoverySystemsToggleAction(m_EnergyRecoverySystemsToggles[index], isOn, index, m_EnergyRecoverySystemsTxts, Convert.ToInt32(SignalEnum.performanceadjusting_energyrecoverysystems), m_EnergyRecoverySystemsIndex); });
// }
// //动态添加监听 超强冷却系统
// for (int i = 0; i < m_SuperCoolingSystemToggles.Count; i++)
// {
// int index = i;
// m_SuperCoolingSystemToggles[i].onValueChanged.AddListener((bool isOn) => { SuperCoolingSystemToggleAction(m_SuperCoolingSystemToggles[index], isOn, index, m_SuperCoolingSystemTxts, Convert.ToInt32(SignalEnum.performanceadjusting_supercoolingsystem), m_SuperCoolingSystemIndex); });
// }
// //点击恢复为大师调校按钮
// m_ReturnToMasterTuningBtn.onClick.AddListener(() =>
// {
// Log.I(TAG, "m_ReturnToMasterTuningBtn Button Clicked!");
// AliOSJSNI.CafCallJS("clickEngine");
// });
// //点击弹窗以外区域关闭当前弹窗
// m_MaskCloseBtn.onClick.AddListener(() =>
// {
// Log.I(TAG, "m_MaskCloseBtn Button Clicked!");
// AliOSJSNI.CafCallJS("clickMaskCloseBtn");
// m_PerformanceAdjusting.SetActive(false);
// });
//}
/ <summary>
/ 性能调节 前后轴输出功率按钮监听
/ </summary>
/ <param name="toggle">待点击按钮</param>
/ <param name="isOn">是否点击</param>
/ <param name="index">选中第几个</param>
/ <param name="check">选中后保存选中结果</param>
//void FrontRearAxleOutputPowerToggleAction(Toggle toggle, bool isOn, int index)
//{
// if (isOn)
// {
// TxtFrontRearAxleOutputPower(index);
// m_FrontRearAxleOutputPowerTxt.text = txt_Ratio;
// Debug.Log("当前后轴:" + m_FrontRearAxleOutputPowerIndex);
// EAfterTwoSeconds(m_FrontRearAxleOutputPowerToggles,index);
// }
//}
/ <summary>
/ 性能调节 前后轴输出功率比分数值
/ </summary>
/ <param name="index">选中第几个按钮</param>
//private void TxtFrontRearAxleOutputPower(int index)
//{
// switch (index)
// {
// case 0:
// txt_Ratio = "0 / 100";
// SuperSportNative.SetInt(Convert.ToInt32(SignalEnum.performanceadjusting_frontrearaxleoutputpower), 100);
// m_FrontRearAxleOutputPowerIndex = 100;
// break;
// case 1:
// txt_Ratio = "10 / 90";
// SuperSportNative.SetInt(Convert.ToInt32(SignalEnum.performanceadjusting_frontrearaxleoutputpower), 90);
// m_FrontRearAxleOutputPowerIndex = 90;
// break;
// case 2:
// txt_Ratio = "20 / 80";
// SuperSportNative.SetInt(Convert.ToInt32(SignalEnum.performanceadjusting_frontrearaxleoutputpower), 80);
// m_FrontRearAxleOutputPowerIndex = 80;
// break;
// case 3:
// txt_Ratio = "30 / 70";
// SuperSportNative.SetInt(Convert.ToInt32(SignalEnum.performanceadjusting_frontrearaxleoutputpower), 70);
// m_FrontRearAxleOutputPowerIndex = 70;
// break;
// case 4:
// txt_Ratio = "40 / 60";
// SuperSportNative.SetInt(Convert.ToInt32(SignalEnum.performanceadjusting_frontrearaxleoutputpower), 60);
// m_FrontRearAxleOutputPowerIndex = 60;
// break;
// case 5:
// txt_Ratio = "50 / 50";
// SuperSportNative.SetInt(Convert.ToInt32(SignalEnum.performanceadjusting_frontrearaxleoutputpower), 50);
// m_FrontRearAxleOutputPowerIndex = 50;
// break;
// case 6:
// txt_Ratio = "60 / 40";
// SuperSportNative.SetInt(Convert.ToInt32(SignalEnum.performanceadjusting_frontrearaxleoutputpower), 40);
// m_FrontRearAxleOutputPowerIndex = 40;
// break;
// case 7:
// txt_Ratio = "70 / 30";
// SuperSportNative.SetInt(Convert.ToInt32(SignalEnum.performanceadjusting_frontrearaxleoutputpower), 30);
// m_FrontRearAxleOutputPowerIndex = 30;
// break;
// case 8:
// txt_Ratio = "80 / 20";
// SuperSportNative.SetInt(Convert.ToInt32(SignalEnum.performanceadjusting_frontrearaxleoutputpower), 20);
// m_FrontRearAxleOutputPowerIndex = 20;
// break;
// case 9:
// txt_Ratio = "90 / 10";
// SuperSportNative.SetInt(Convert.ToInt32(SignalEnum.performanceadjusting_frontrearaxleoutputpower), 10);
// m_FrontRearAxleOutputPowerIndex = 10;
// break;
// case 10:
// txt_Ratio = "100 / 0";
// SuperSportNative.SetInt(Convert.ToInt32(SignalEnum.performanceadjusting_frontrearaxleoutputpower), 1);
// m_FrontRearAxleOutputPowerIndex = 1;
// break;
// default:
// break;
// }
//}
/ <summary>
/ 调节后 动力响应、转向手感、悬架阻尼、车身稳定系统 对应文本变化
/ </summary>
/ <param name="toggle">待点击按钮</param>
/ <param name="isOn">是否点击</param>
/ <param name="index">选中第几个</param>
/ <param name="textMeshProUGUIs">具体哪一个调节的文本</param>
/ <param name="id">下发信号对应的id号</param>
/ <param name="param">下发信号对应的信号值</param>
/ <param name="check">选中后保存选中结果</param>
//void AdjustingToggleAction(Toggle toggle, bool isOn, int index, List<TextMeshProUGUI> textMeshProUGUIs, int id, int param, int check)
//{
// if (isOn)
// {
// if (m_BodyStabilizationSystemToggles[0].isOn == true || m_BodyStabilizationSystemToggles[1].isOn == true)
// {
// Debug.Log("警告弹框!");
// //m_ESPWarningPopUpWindow = AssetDatabase.LoadAssetAtPath("Assets/UIFramework/Prefab/ESPWarningPopUpWindow.prefab", typeof(GameObject)) as GameObject;
// //m_ESPWarningPopUpWindow = Instantiate(m_ESPWarningPopUpWindow);
// //m_BodyStabilizationSystemToggles[0].SetIsOnWithoutNotify(false);
// //m_BodyStabilizationSystemToggles[1].SetIsOnWithoutNotify(false);
// //m_BodyStabilizationSystemToggles[2].isOn = true;
// UIRoot.Show<ESPWarningPopUpWindow>();
// }
// switch (index)
// {
// case 0:
// textMeshProUGUIs[0].fontSize = 28;
// textMeshProUGUIs[1].fontSize = 22;
// textMeshProUGUIs[2].fontSize = 22;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 255);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[2].color = new Color32(255, 255, 255, 128);
// check = index;
// SuperSportNative.SetInt(id, param);
// break;
// case 1:
// textMeshProUGUIs[0].fontSize = 22;
// textMeshProUGUIs[1].fontSize = 28;
// textMeshProUGUIs[2].fontSize = 22;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 255);
// textMeshProUGUIs[2].color = new Color32(255, 255, 255, 128);
// SuperSportNative.SetInt(id, param);
// break;
// case 2:
// textMeshProUGUIs[0].fontSize = 22;
// textMeshProUGUIs[1].fontSize = 22;
// textMeshProUGUIs[2].fontSize = 28;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[2].color = new Color32(255, 255, 255, 255);
// SuperSportNative.SetInt(id, param);
// break;
// default:
// break;
// }
// }
//}
/ <summary>
/ 制动反馈 对应文本变化
/ </summary>
/ <param name="toggle">待点击按钮</param>
/ <param name="isOn">是否点击</param>
/ <param name="index">选中第几个</param>
/ <param name="textMeshProUGUIs">具体哪一个调节的文本</param>
/ <param name="id">下发信号对应的id号</param>
/ <param name="check">选中后保存选中结果</param>
//void BrakeFeedbackToggleAction(Toggle toggle, bool isOn, int index, List<TextMeshProUGUI> textMeshProUGUIs, int id, int check)
//{
// if (isOn)
// {
// switch (index)
// {
// case 0:
// textMeshProUGUIs[0].fontSize = 28;
// textMeshProUGUIs[1].fontSize = 22;
// textMeshProUGUIs[2].fontSize = 22;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 255);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[2].color = new Color32(255, 255, 255, 128);
// SuperSportNative.SetInt(id, 1);
// break;
// case 1:
// textMeshProUGUIs[0].fontSize = 22;
// textMeshProUGUIs[1].fontSize = 28;
// textMeshProUGUIs[2].fontSize = 22;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 255);
// textMeshProUGUIs[2].color = new Color32(255, 255, 255, 128);
// SuperSportNative.SetInt(id, 0);
// break;
// case 2:
// textMeshProUGUIs[0].fontSize = 22;
// textMeshProUGUIs[1].fontSize = 22;
// textMeshProUGUIs[2].fontSize = 28;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[2].color = new Color32(255, 255, 255, 255);
// SuperSportNative.SetInt(id, 2);
// break;
// default:
// break;
// }
// }
//}
/ <summary>
/ 能量回收系统
/ </summary>
/ <param name="toggle">待点击按钮</param>
/ <param name="isOn">是否点击</param>
/ <param name="index">选中第几个</param>
/ <param name="textMeshProUGUIs">具体哪一个调节的文本</param>
/ <param name="id">下发信号对应的id号</param>
/ <param name="check">选中后保存选中结果</param>
//void EnergyRecoverySystemsToggleAction(Toggle toggle, bool isOn, int index, List<TextMeshProUGUI> textMeshProUGUIs, int id, int check)
//{
// if (isOn)
// {
// switch (index)
// {
// case 0:
// textMeshProUGUIs[0].fontSize = 28;
// textMeshProUGUIs[1].fontSize = 22;
// textMeshProUGUIs[2].fontSize = 22;
// textMeshProUGUIs[3].fontSize = 22;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 255);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[2].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[3].color = new Color32(255, 255, 255, 128);
// SuperSportNative.SetInt(id, 5);
// break;
// case 1:
// textMeshProUGUIs[0].fontSize = 22;
// textMeshProUGUIs[1].fontSize = 28;
// textMeshProUGUIs[2].fontSize = 22;
// textMeshProUGUIs[3].fontSize = 22;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 255);
// textMeshProUGUIs[2].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[3].color = new Color32(255, 255, 255, 128);
// SuperSportNative.SetInt(id, 0);
// break;
// case 2:
// textMeshProUGUIs[0].fontSize = 22;
// textMeshProUGUIs[1].fontSize = 22;
// textMeshProUGUIs[2].fontSize = 28;
// textMeshProUGUIs[3].fontSize = 22;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[2].color = new Color32(255, 255, 255, 255);
// textMeshProUGUIs[3].color = new Color32(255, 255, 255, 128);
// SuperSportNative.SetInt(id, 1);
// break;
// case 3:
// textMeshProUGUIs[0].fontSize = 22;
// textMeshProUGUIs[1].fontSize = 22;
// textMeshProUGUIs[2].fontSize = 22;
// textMeshProUGUIs[3].fontSize = 28;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[2].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[3].color = new Color32(255, 255, 255, 255);
// SuperSportNative.SetInt(id, 2);
// break;
// default:
// break;
// }
// }
//}
/ <summary>
/ 超强冷却系统
/ </summary>
/ <param name="toggle">待点击按钮</param>
/ <param name="isOn">是否点击</param>
/ <param name="index">选中第几个</param>
/ <param name="textMeshProUGUIs">具体哪一个调节的文本</param>
/ <param name="id">下发信号对应的id号</param>
/ <param name="check">选中后保存选中结果</param>
//void SuperCoolingSystemToggleAction(Toggle toggle, bool isOn, int index, List<TextMeshProUGUI> textMeshProUGUIs, int id, int check)
//{
// if (isOn)
// {
// switch (index)
// {
// case 0:
// textMeshProUGUIs[0].fontSize = 28;
// textMeshProUGUIs[1].fontSize = 22;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 255);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 128);
// SuperSportNative.SetInt(id, 1);
// break;
// case 1:
// textMeshProUGUIs[0].fontSize = 22;
// textMeshProUGUIs[1].fontSize = 28;
// textMeshProUGUIs[0].color = new Color32(255, 255, 255, 128);
// textMeshProUGUIs[1].color = new Color32(255, 255, 255, 255);
// SuperSportNative.SetInt(id, 0);
// break;
// default:
// break;
// }
// }
//}
两秒之后再次获取,如果值不相等则按钮重置
//void EAfterTwoSeconds(List<Toggle> toggle,int index)
//{
// DOTween.Kill("EAfterTwoSeconds");
// float timeCount = 0;
// DOTween.To(() => timeCount, a => timeCount = a, 1, 2).OnComplete(() =>
// {
// DataCenter.FrontRearAxleOutputPower.Subscribe(i =>
// {
// Debug.Log("延迟2秒:" + m_FrontRearAxleOutputPowerIndex);
// if (m_FrontRearAxleOutputPowerIndex == i)
// {
// toggle[index].isOn = true;
// }
// else
// {
// Debug.Log("不相等:" + m_FrontRearAxleOutputPowerIndex);
// toggle[index].transform.parent.GetComponent<ToggleGroup>().SetAllTogglesOff();
// toggle[index].isOn = false;
// }
// }).AddTo(this);
// }).SetId("EAfterTwoSeconds");
//}
}文章来源地址https://www.toymoban.com/news/detail-823185.html
到了这里,关于toggle封装的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!