更改文件架构
This commit is contained in:
99
Packages/Common~/Scripts/UX/Components/UXData.cs
Normal file
99
Packages/Common~/Scripts/UX/Components/UXData.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Sirenix.OdinInspector;
|
||||
using System.Threading;
|
||||
using UnityEngine.UIElements;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
#endif
|
||||
namespace BITKit.UX
|
||||
{
|
||||
public interface IDataComponent : IStart, IStop
|
||||
{
|
||||
void SetKey(string value);
|
||||
}
|
||||
[System.Serializable]
|
||||
public sealed class MonoDataComponent : MonoBehaviour, IDataComponent
|
||||
{
|
||||
public MonoBehaviour mono;
|
||||
IDataComponent component => mono as IDataComponent;
|
||||
public void OnStart()
|
||||
{
|
||||
component.OnStart();
|
||||
}
|
||||
|
||||
public void OnStop()
|
||||
{
|
||||
component.OnStop();
|
||||
}
|
||||
|
||||
public void SetKey(string value)
|
||||
{
|
||||
component.SetKey(value);
|
||||
}
|
||||
}
|
||||
public abstract class DataComponents : IDataComponent
|
||||
{
|
||||
protected string key;
|
||||
public virtual void OnStart()
|
||||
{
|
||||
|
||||
}
|
||||
public virtual void SetKey(string key)
|
||||
{
|
||||
this.key = key;
|
||||
}
|
||||
public virtual void OnStop()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
public class UXData : MonoBehaviour
|
||||
{
|
||||
[SerializeReference, SubclassSelector] public References key;
|
||||
[SerializeReference, SubclassSelector] public DataComponents dataParser;
|
||||
bool initialized;
|
||||
void OnEnable()
|
||||
{
|
||||
dataParser.SetKey(key);
|
||||
if (initialized)
|
||||
{
|
||||
dataParser.OnStart();
|
||||
}
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
if (initialized is false)
|
||||
{
|
||||
dataParser.OnStart();
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
void OnDisable()
|
||||
{
|
||||
if (initialized)
|
||||
{
|
||||
dataParser.OnStop();
|
||||
}
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.CustomEditor(typeof(UXData))]
|
||||
public class UXDataInspector : BITInspector<UXData>
|
||||
{
|
||||
public override VisualElement CreateInspectorGUI()
|
||||
{
|
||||
CreateSubTitle(Constant.Header.Settings);
|
||||
var key = root.Create<PropertyField>();
|
||||
CreateSubTitle(Constant.Header.Reference);
|
||||
var dataParser = root.Create<PropertyField>();
|
||||
|
||||
key.bindingPath = nameof(UXData.key);
|
||||
dataParser.bindingPath = nameof(UXData.dataParser);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/UX/Components/UXData.cs.meta
Normal file
11
Packages/Common~/Scripts/UX/Components/UXData.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae1f33a93b73bad40817685f6d13e097
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6667f2ea88571b54881d9d1118fc3e6f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "BITKit.UX.Components",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:a209c53514018594f9f482516f2a6781",
|
||||
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23",
|
||||
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
|
||||
"GUID:be17a8778dbfe454890ed8279279e153",
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:9400d40641bab5b4a9702f65bf5c6eb5"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d2db42e8a830b54fa78ebb94e912288
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,37 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using BITKit.UX;
|
||||
using UnityEngine.Networking;
|
||||
namespace BITKit.UX.Components
|
||||
{
|
||||
[System.Serializable]
|
||||
public class QRBuilder : StringComponent
|
||||
{
|
||||
public TranslateSO so;
|
||||
public UXImage image;
|
||||
public InitializationState state;
|
||||
protected override async void OnSet(string value)
|
||||
{
|
||||
state = InitializationState.Initializing;
|
||||
var url = so.Get(value);
|
||||
using (var request = UnityWebRequestTexture.GetTexture(url))
|
||||
{
|
||||
Debug.Log(url);
|
||||
await request.SendWebRequest();
|
||||
BITApp.CancellationTokenSource.Token.ThrowIfCancellationRequested();
|
||||
if (request.result is UnityWebRequest.Result.Success)
|
||||
{
|
||||
var texture = DownloadHandlerTexture.GetContent(request);
|
||||
image.Set(texture);
|
||||
state = InitializationState.Initialized;
|
||||
}
|
||||
else
|
||||
{
|
||||
state = InitializationState.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: da98aa1707942e742850cd8995ba7d90
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,108 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.Events;
|
||||
using BITKit.UX;
|
||||
namespace BITKit.UX.Components
|
||||
{
|
||||
public abstract class StringComponent : DataComponents
|
||||
{
|
||||
public override void OnStart()
|
||||
{
|
||||
Data.AddListener<string>(key, OnSet);
|
||||
}
|
||||
public override void OnStop()
|
||||
{
|
||||
Data.RemoveListender<string>(key, OnSet);
|
||||
}
|
||||
protected abstract void OnSet(string value);
|
||||
}
|
||||
[System.Serializable]
|
||||
public sealed class Text : StringComponent
|
||||
{
|
||||
public Provider output;
|
||||
protected override async void OnSet(string value)
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
output.Set(value);
|
||||
}
|
||||
}
|
||||
[System.Serializable]
|
||||
public sealed class TranslateText : StringComponent
|
||||
{
|
||||
ITranslator translator;
|
||||
public Provider output;
|
||||
public TranslateSO so;
|
||||
public string source;
|
||||
public override void OnStart()
|
||||
{
|
||||
base.OnStart();
|
||||
source = output.Get<string>();
|
||||
translator = DI.Get<ITranslator>();
|
||||
translator.OnChangeLanguage += OnTranslate;
|
||||
}
|
||||
protected override async void OnSet(string value)
|
||||
{
|
||||
source = value;
|
||||
value = so?.GetAt(value);
|
||||
await UniTask.SwitchToMainThread();
|
||||
output.Set(value);
|
||||
}
|
||||
public override void OnStop()
|
||||
{
|
||||
translator.OnChangeLanguage -= OnTranslate;
|
||||
}
|
||||
void OnTranslate(string langs)
|
||||
{
|
||||
OnSet(source);
|
||||
}
|
||||
}
|
||||
[System.Serializable]
|
||||
public sealed class Event : StringComponent
|
||||
{
|
||||
public UnityEvent<string> output;
|
||||
protected override async void OnSet(string value)
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
output.Invoke(value);
|
||||
}
|
||||
}
|
||||
[System.Serializable]
|
||||
public sealed class BoolEvent : DataComponents
|
||||
{
|
||||
public UnityEvent<bool> outputEvent;
|
||||
public UnityEvent ifTrueEvent;
|
||||
public UnityEvent ifFlaseEvent;
|
||||
public override void OnStart()
|
||||
{
|
||||
Data.AddListener<bool>(key, OnSetBool, true);
|
||||
}
|
||||
public override void OnStop()
|
||||
{
|
||||
Data.RemoveListender<bool>(key, OnSetBool);
|
||||
}
|
||||
async void OnSetBool(bool boolean)
|
||||
{
|
||||
try
|
||||
{
|
||||
await UniTask.SwitchToMainThread(BITApp.CancellationTokenSource.Token);
|
||||
outputEvent.Invoke(boolean);
|
||||
|
||||
if (boolean is true)
|
||||
ifTrueEvent.Invoke();
|
||||
else
|
||||
ifFlaseEvent.Invoke();
|
||||
}
|
||||
catch (System.OperationCanceledException)
|
||||
{
|
||||
|
||||
}
|
||||
catch (System.Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 02604813db464e84496818ce0a5fdc93
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user