基础内容
必要插件安装 缓动曲线和动画基础 ElementFolder,Track与其次级模块,PathNode重构
This commit is contained in:
200
Assets/Modern UI Pack/Scripts/Switch/SwitchManager.cs
Normal file
200
Assets/Modern UI Pack/Scripts/Switch/SwitchManager.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[RequireComponent(typeof(Animator))]
|
||||
[RequireComponent(typeof(Button))]
|
||||
public class SwitchManager : MonoBehaviour, IPointerEnterHandler
|
||||
{
|
||||
// Events
|
||||
[SerializeField] public SwitchEvent onValueChanged = new SwitchEvent();
|
||||
public UnityEvent OnEvents = new UnityEvent();
|
||||
public UnityEvent OffEvents = new UnityEvent();
|
||||
|
||||
// Saving
|
||||
public bool saveValue = true;
|
||||
public string switchTag = "Switch";
|
||||
|
||||
// Settings
|
||||
public bool isOn = true;
|
||||
public bool invokeAtStart = true;
|
||||
public bool enableSwitchSounds = false;
|
||||
public bool useHoverSound = true;
|
||||
public bool useClickSound = true;
|
||||
|
||||
// Resources
|
||||
public Animator switchAnimator;
|
||||
public Button switchButton;
|
||||
public AudioSource soundSource;
|
||||
|
||||
// Audio
|
||||
public AudioClip hoverSound;
|
||||
public AudioClip clickSound;
|
||||
|
||||
[System.Serializable]
|
||||
public class SwitchEvent : UnityEvent<bool> { }
|
||||
|
||||
bool isInitialized = false;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (switchAnimator == null) { switchAnimator = gameObject.GetComponent<Animator>(); }
|
||||
if (switchButton == null)
|
||||
{
|
||||
switchButton = gameObject.GetComponent<Button>();
|
||||
switchButton.onClick.AddListener(AnimateSwitch);
|
||||
|
||||
if (enableSwitchSounds && useClickSound)
|
||||
{
|
||||
switchButton.onClick.AddListener(delegate
|
||||
{
|
||||
soundSource.PlayOneShot(clickSound);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (saveValue) { GetSavedData(); }
|
||||
else
|
||||
{
|
||||
if (gameObject.activeInHierarchy) { StopCoroutine("DisableAnimator"); }
|
||||
if (gameObject.activeInHierarchy) { StartCoroutine("DisableAnimator"); }
|
||||
|
||||
switchAnimator.enabled = true;
|
||||
|
||||
if (isOn) { switchAnimator.Play("On Instant"); }
|
||||
else { switchAnimator.Play("Off Instant"); }
|
||||
}
|
||||
|
||||
if (invokeAtStart && isOn ) { OnEvents.Invoke(); }
|
||||
else if (invokeAtStart && !isOn) { OffEvents.Invoke(); }
|
||||
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (isInitialized)
|
||||
{
|
||||
UpdateUI();
|
||||
}
|
||||
}
|
||||
|
||||
void GetSavedData()
|
||||
{
|
||||
if (gameObject.activeInHierarchy)
|
||||
{
|
||||
StopCoroutine("DisableAnimator");
|
||||
StartCoroutine("DisableAnimator");
|
||||
}
|
||||
|
||||
switchAnimator.enabled = true;
|
||||
|
||||
if (PlayerPrefs.GetString(switchTag + "Switch") == "" || !PlayerPrefs.HasKey(switchTag + "Switch"))
|
||||
{
|
||||
if (isOn) { switchAnimator.Play("Switch On"); PlayerPrefs.SetString(switchTag + "Switch", "true"); }
|
||||
else { switchAnimator.Play("Switch Off"); PlayerPrefs.SetString(switchTag + "Switch", "false"); }
|
||||
}
|
||||
else if (PlayerPrefs.GetString(switchTag + "Switch") == "true") { switchAnimator.Play("Switch On"); isOn = true; }
|
||||
else if (PlayerPrefs.GetString(switchTag + "Switch") == "false") { switchAnimator.Play("Switch Off"); isOn = false; }
|
||||
}
|
||||
|
||||
public void AnimateSwitch()
|
||||
{
|
||||
if (gameObject.activeInHierarchy)
|
||||
{
|
||||
StopCoroutine("DisableAnimator");
|
||||
StartCoroutine("DisableAnimator");
|
||||
}
|
||||
|
||||
switchAnimator.enabled = true;
|
||||
|
||||
if (isOn)
|
||||
{
|
||||
isOn = false;
|
||||
switchAnimator.Play("Switch Off");
|
||||
OffEvents.Invoke();
|
||||
|
||||
if (saveValue) { PlayerPrefs.SetString(switchTag + "Switch", "false"); }
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
isOn = true;
|
||||
switchAnimator.Play("Switch On");
|
||||
OnEvents.Invoke();
|
||||
|
||||
if (saveValue) { PlayerPrefs.SetString(switchTag + "Switch", "true"); }
|
||||
}
|
||||
|
||||
onValueChanged.Invoke(isOn);
|
||||
}
|
||||
|
||||
public void SetOn()
|
||||
{
|
||||
if (saveValue) { PlayerPrefs.SetString(switchTag + "Switch", "true"); }
|
||||
if (gameObject.activeInHierarchy)
|
||||
{
|
||||
StopCoroutine("DisableAnimator");
|
||||
StartCoroutine("DisableAnimator");
|
||||
}
|
||||
|
||||
isOn = true;
|
||||
|
||||
switchAnimator.enabled = true;
|
||||
switchAnimator.Play("Switch On");
|
||||
|
||||
OnEvents.Invoke();
|
||||
onValueChanged.Invoke(true);
|
||||
}
|
||||
|
||||
public void SetOff()
|
||||
{
|
||||
if (saveValue) { PlayerPrefs.SetString(switchTag + "Switch", "false"); }
|
||||
if (gameObject.activeInHierarchy)
|
||||
{
|
||||
StopCoroutine("DisableAnimator");
|
||||
StartCoroutine("DisableAnimator");
|
||||
}
|
||||
|
||||
isOn = false;
|
||||
|
||||
switchAnimator.enabled = true;
|
||||
switchAnimator.Play("Switch Off");
|
||||
|
||||
OffEvents.Invoke();
|
||||
onValueChanged.Invoke(false);
|
||||
}
|
||||
|
||||
public void UpdateUI()
|
||||
{
|
||||
if (gameObject.activeInHierarchy)
|
||||
{
|
||||
StopCoroutine("DisableAnimator");
|
||||
StartCoroutine("DisableAnimator");
|
||||
}
|
||||
|
||||
switchAnimator.enabled = true;
|
||||
|
||||
if (isOn && switchAnimator.gameObject.activeInHierarchy) { switchAnimator.Play("On Instant"); }
|
||||
else if (!isOn && switchAnimator.gameObject.activeInHierarchy) { switchAnimator.Play("Off Instant"); }
|
||||
}
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
if (enableSwitchSounds && useHoverSound && switchButton.interactable)
|
||||
{
|
||||
soundSource.PlayOneShot(hoverSound);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator DisableAnimator()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(0.5f);
|
||||
switchAnimator.enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Assets/Modern UI Pack/Scripts/Switch/SwitchManager.cs.meta
Normal file
18
Assets/Modern UI Pack/Scripts/Switch/SwitchManager.cs.meta
Normal file
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a058d8729a0602458c4c9045eeddbf6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 1de3e885959f3b84f9f25beca3a9503d, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Switch/SwitchManager.cs
|
||||
uploadId: 628721
|
||||
141
Assets/Modern UI Pack/Scripts/Switch/SwitchManagerEditor.cs
Normal file
141
Assets/Modern UI Pack/Scripts/Switch/SwitchManagerEditor.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
#if UNITY_EDITOR
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[CustomEditor(typeof(SwitchManager))]
|
||||
public class SwitchManagerEditor : Editor
|
||||
{
|
||||
private GUISkin customSkin;
|
||||
private SwitchManager switchTarget;
|
||||
private UIManagerSwitch tempUIM;
|
||||
private int currentTab;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
switchTarget = (SwitchManager)target;
|
||||
|
||||
try { tempUIM = switchTarget.GetComponent<UIManagerSwitch>(); }
|
||||
catch { }
|
||||
|
||||
if (EditorGUIUtility.isProSkin == true) { customSkin = MUIPEditorHandler.GetDarkEditor(customSkin); }
|
||||
else { customSkin = MUIPEditorHandler.GetLightEditor(customSkin); }
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
MUIPEditorHandler.DrawComponentHeader(customSkin, "Switch Top Header");
|
||||
|
||||
GUIContent[] toolbarTabs = new GUIContent[2];
|
||||
toolbarTabs[0] = new GUIContent("Content");
|
||||
toolbarTabs[1] = new GUIContent("Settings");
|
||||
|
||||
currentTab = MUIPEditorHandler.DrawTabs(currentTab, toolbarTabs, customSkin);
|
||||
|
||||
if (GUILayout.Button(new GUIContent("Content", "Content"), customSkin.FindStyle("Tab Content")))
|
||||
currentTab = 0;
|
||||
if (GUILayout.Button(new GUIContent("Settings", "Settings"), customSkin.FindStyle("Tab Settings")))
|
||||
currentTab = 1;
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
var onValueChanged = serializedObject.FindProperty("onValueChanged");
|
||||
var OnEvents = serializedObject.FindProperty("OnEvents");
|
||||
var OffEvents = serializedObject.FindProperty("OffEvents");
|
||||
var saveValue = serializedObject.FindProperty("saveValue");
|
||||
var switchTag = serializedObject.FindProperty("switchTag");
|
||||
var invokeAtStart = serializedObject.FindProperty("invokeAtStart");
|
||||
var isOn = serializedObject.FindProperty("isOn");
|
||||
var enableSwitchSounds = serializedObject.FindProperty("enableSwitchSounds");
|
||||
var useHoverSound = serializedObject.FindProperty("useHoverSound");
|
||||
var useClickSound = serializedObject.FindProperty("useClickSound");
|
||||
var soundSource = serializedObject.FindProperty("soundSource");
|
||||
var hoverSound = serializedObject.FindProperty("hoverSound");
|
||||
var clickSound = serializedObject.FindProperty("clickSound");
|
||||
|
||||
switch (currentTab)
|
||||
{
|
||||
case 0:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Events Header", 6);
|
||||
EditorGUILayout.PropertyField(onValueChanged, new GUIContent("On Value Changed"), true);
|
||||
EditorGUILayout.PropertyField(OnEvents, new GUIContent("On Events"), true);
|
||||
EditorGUILayout.PropertyField(OffEvents, new GUIContent("Off Events"), true);
|
||||
|
||||
if (enableSwitchSounds.boolValue == true && useHoverSound.boolValue == true)
|
||||
MUIPEditorHandler.DrawProperty(hoverSound, customSkin, "Hover Sound");
|
||||
|
||||
if (enableSwitchSounds.boolValue == true && useClickSound.boolValue == true)
|
||||
MUIPEditorHandler.DrawProperty(clickSound, customSkin, "Click Sound");
|
||||
|
||||
break;
|
||||
|
||||
case 1:
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "Options Header", 6);
|
||||
invokeAtStart.boolValue = MUIPEditorHandler.DrawToggle(invokeAtStart.boolValue, customSkin, "Invoke At Start");
|
||||
isOn.boolValue = MUIPEditorHandler.DrawToggle(isOn.boolValue, customSkin, "Is On");
|
||||
|
||||
GUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
GUILayout.Space(-3);
|
||||
enableSwitchSounds.boolValue = MUIPEditorHandler.DrawTogglePlain(enableSwitchSounds.boolValue, customSkin, "Enable Switch Sounds");
|
||||
GUILayout.Space(3);
|
||||
|
||||
if (enableSwitchSounds.boolValue == true)
|
||||
{
|
||||
MUIPEditorHandler.DrawProperty(soundSource, customSkin, "Sound Source");
|
||||
|
||||
useHoverSound.boolValue = MUIPEditorHandler.DrawToggle(useHoverSound.boolValue, customSkin, "Enable Hover Sound");
|
||||
useClickSound.boolValue = MUIPEditorHandler.DrawToggle(useClickSound.boolValue, customSkin, "Enable Click Sound");
|
||||
|
||||
if (useHoverSound.boolValue == true) { MUIPEditorHandler.DrawProperty(hoverSound, customSkin, "Hover Sound"); }
|
||||
if (useClickSound.boolValue == true) { MUIPEditorHandler.DrawProperty(clickSound, customSkin, "Click Sound"); }
|
||||
|
||||
if (switchTarget.soundSource == null) { EditorGUILayout.HelpBox("'Sound Source' is missing.", MessageType.Warning); }
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
GUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
GUILayout.Space(-3);
|
||||
saveValue.boolValue = MUIPEditorHandler.DrawTogglePlain(saveValue.boolValue, customSkin, "Save Value");
|
||||
GUILayout.Space(3);
|
||||
|
||||
if (saveValue.boolValue == true)
|
||||
{
|
||||
MUIPEditorHandler.DrawPropertyPlainCW(switchTag, customSkin, "Switch Tag:", 90);
|
||||
EditorGUILayout.HelpBox("Each switch should has its own unique tag.", MessageType.Info);
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
|
||||
MUIPEditorHandler.DrawHeader(customSkin, "UIM Header", 10);
|
||||
|
||||
if (tempUIM != null)
|
||||
{
|
||||
MUIPEditorHandler.DrawUIManagerConnectedHeader();
|
||||
tempUIM.overrideColors = MUIPEditorHandler.DrawToggle(tempUIM.overrideColors, customSkin, "Override Colors");
|
||||
|
||||
if (GUILayout.Button("Open UI Manager", customSkin.button))
|
||||
EditorApplication.ExecuteMenuItem("Tools/Modern UI Pack/Show UI Manager");
|
||||
|
||||
if (GUILayout.Button("Disable UI Manager Connection", customSkin.button))
|
||||
{
|
||||
if (EditorUtility.DisplayDialog("Modern UI Pack", "Are you sure you want to disable UI Manager connection with the object? " +
|
||||
"This operation cannot be undone.", "Yes", "Cancel"))
|
||||
{
|
||||
try { DestroyImmediate(tempUIM); }
|
||||
catch { Debug.LogError("<b>[Horizontal Selector]</b> Failed to delete UI Manager connection.", this); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else if (tempUIM == null) { MUIPEditorHandler.DrawUIManagerDisconnectedHeader(); }
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (Application.isPlaying == false) { this.Repaint(); }
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64c43837553166847bd23a630a8ca39d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 201717
|
||||
packageName: Modern UI Pack
|
||||
packageVersion: 5.5.19
|
||||
assetPath: Assets/Modern UI Pack/Scripts/Switch/SwitchManagerEditor.cs
|
||||
uploadId: 628721
|
||||
Reference in New Issue
Block a user