Instantly share code, notes, and snippets.
Created
January 28, 2022 09:52
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save Anthelmed/4d5f55a50e1238f656a006d1bf83555e to your computer and use it in GitHub Desktop.
MainMenu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Unity.Mathematics; | |
using UnityEngine; | |
using UnityEngine.UIElements; | |
public class MainMenu : MonoBehaviour | |
{ | |
[SerializeField] private UIDocument mainMenuUIDocument; | |
[SerializeField] private Material buttonHoverMaterial; | |
[SerializeField] private CustomRenderTexture buttonCustomRenderTexture; | |
private Button _createButton; | |
private Button _joinButton; | |
private Button _optionsButton; | |
private Button _exitButton; | |
private List<Button> _hoverButtons = new(); | |
private List<Material> _hoverMaterials = new(); | |
private List<CustomRenderTexture> _hoveCustomRenderTextures = new(); | |
private List<HoverManipulator> _hoverManipulators = new(); | |
private static readonly int NoiseOffset = Shader.PropertyToID("_NoiseOffset"); | |
private void Awake() | |
{ | |
GetButtonReferences(); | |
InstantiateMaterialsAndTextures(); | |
} | |
private void OnEnable() | |
{ | |
BindManipulators(); | |
} | |
private void OnDisable() | |
{ | |
UnbindManipulators(); | |
} | |
private void Update() | |
{ | |
UpdateButtonHoverMaterial(); | |
} | |
private void GetButtonReferences() | |
{ | |
_createButton = mainMenuUIDocument.rootVisualElement.Q<Button>("Create-Room-Button"); | |
_joinButton = mainMenuUIDocument.rootVisualElement.Q<Button>("Join-Room-Button"); | |
_optionsButton = mainMenuUIDocument.rootVisualElement.Q<Button>("Options-Room-Button"); | |
_exitButton = mainMenuUIDocument.rootVisualElement.Q<Button>("Exit-Room-Button"); | |
_hoverButtons.Add(_createButton); | |
_hoverButtons.Add(_joinButton); | |
_hoverButtons.Add(_optionsButton); | |
_hoverButtons.Add(_exitButton); | |
} | |
private void InstantiateMaterialsAndTextures() | |
{ | |
for (var i = 0; i < _hoverButtons.Count; i++) | |
{ | |
var material = Instantiate(buttonHoverMaterial); | |
_hoverMaterials.Add(material); | |
material.SetFloat(NoiseOffset, i * 10); | |
var customRenderTexture = Instantiate(buttonCustomRenderTexture); | |
customRenderTexture.material = material; | |
_hoveCustomRenderTextures.Add(customRenderTexture); | |
customRenderTexture.Update(); | |
var background = new StyleBackground | |
{ | |
value = Background.FromRenderTexture(customRenderTexture) | |
}; | |
var button = _hoverButtons[i]; | |
button.style.backgroundImage = background; | |
} | |
} | |
private void BindManipulators() | |
{ | |
for (var i = 0; i < _hoverButtons.Count; i++) | |
{ | |
var manipulator = new HoverManipulator(); | |
_hoverManipulators.Add(manipulator); | |
var button = _hoverButtons[i]; | |
button.AddManipulator(manipulator); | |
} | |
} | |
private void UnbindManipulators() | |
{ | |
for (var i = 0; i < _hoverButtons.Count; i++) | |
{ | |
var manipulator = _hoverManipulators[i]; | |
var button = _hoverButtons[i]; | |
button.RemoveManipulator(manipulator); | |
} | |
} | |
private void UpdateButtonHoverMaterial() | |
{ | |
for (var i = 0; i < _hoverManipulators.Count; i++) | |
{ | |
var hoverManipulator = _hoverManipulators[i]; | |
if (!hoverManipulator.IsPointerOver) continue; | |
var material = _hoverMaterials[i]; | |
var customRenderTexture = _hoveCustomRenderTextures[i]; | |
material.SetFloat(NoiseOffset, material.GetFloat(NoiseOffset) + Time.deltaTime); | |
customRenderTexture.Update(); | |
return; | |
} | |
} | |
private class HoverManipulator: Manipulator | |
{ | |
private bool _isPointerOver; | |
public bool IsPointerOver => _isPointerOver; | |
protected override void RegisterCallbacksOnTarget() | |
{ | |
target.RegisterCallback<PointerEnterEvent>(OnPointerEnter); | |
target.RegisterCallback<PointerLeaveEvent>(OnPointerLeave); | |
} | |
protected override void UnregisterCallbacksFromTarget() | |
{ | |
target.UnregisterCallback<PointerEnterEvent>(OnPointerEnter); | |
target.UnregisterCallback<PointerLeaveEvent>(OnPointerLeave); | |
} | |
private void OnPointerEnter(PointerEnterEvent evt) | |
{ | |
_isPointerOver = true; | |
} | |
private void OnPointerLeave(PointerLeaveEvent evt) | |
{ | |
_isPointerOver = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment