更改文件架构
This commit is contained in:
73
Packages/Common~/Scripts/UX/SubMenu/SubMenu.cs
Normal file
73
Packages/Common~/Scripts/UX/SubMenu/SubMenu.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using UnityEngine.Events;
|
||||
namespace BITKit
|
||||
{
|
||||
public struct NewSubMenu
|
||||
{
|
||||
public string name;
|
||||
public Vector2 position;
|
||||
public IEnumerable<SubAction> actions;
|
||||
}
|
||||
public struct SubAction
|
||||
{
|
||||
public string name;
|
||||
public UnityAction action;
|
||||
}
|
||||
public class SubMenu : BITBehavior
|
||||
{
|
||||
[Header(Constant.Header.Components)]
|
||||
public UIDocument document;
|
||||
Label label;
|
||||
VisualElement context;
|
||||
VisualElement root;
|
||||
void Start()
|
||||
{
|
||||
Data.AddListener<IEnumerable<SubAction>>(OnAction);
|
||||
Data.AddListener<NewSubMenu>(OnContextMenu);
|
||||
root = document.rootVisualElement;
|
||||
label = root.Q<Label>("Label");
|
||||
context = root.Q("Context");
|
||||
SetActive(false);
|
||||
root.RegisterCallback<PointerLeaveEvent>(x =>
|
||||
{
|
||||
SetActive(false);
|
||||
});
|
||||
}
|
||||
void OnContextMenu(NewSubMenu contextMenu)
|
||||
{
|
||||
label.text = contextMenu.name;
|
||||
OnAction(contextMenu.actions);
|
||||
root.style.left = contextMenu.position.x - 4f;
|
||||
root.style.top = contextMenu.position.y - 4f;
|
||||
}
|
||||
void OnAction(IEnumerable<SubAction> actions)
|
||||
{
|
||||
SetActive(true);
|
||||
actions.ForEach(x =>
|
||||
{
|
||||
var button = new Button();
|
||||
button.text = x.name;
|
||||
button.clicked += () =>
|
||||
{
|
||||
x.action.Invoke();
|
||||
SetActive(false);
|
||||
};
|
||||
context.Add(button);
|
||||
});
|
||||
}
|
||||
public override void SetActive(bool active)
|
||||
{
|
||||
if (active)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Clear();
|
||||
}
|
||||
document.rootVisualElement.SetActive(active);
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/UX/SubMenu/SubMenu.cs.meta
Normal file
11
Packages/Common~/Scripts/UX/SubMenu/SubMenu.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c9e12554cacfd7944957a480f6d62e93
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user