1
This commit is contained in:
35
Unity/Scripts/Components/AutoInvoke.cs
Normal file
35
Unity/Scripts/Components/AutoInvoke.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
namespace BITKit
|
||||
{
|
||||
public class AutoInvoke : MonoBehaviour
|
||||
{
|
||||
public UnityEvent onAwake = new();
|
||||
public UnityEvent onStart = new();
|
||||
public UnityEvent onEnabled = new();
|
||||
public UnityEvent onDisabled = new();
|
||||
public UnityEvent onDestory = new();
|
||||
void Awake()
|
||||
{
|
||||
onAwake.Invoke();
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
onStart.Invoke();
|
||||
}
|
||||
void OnEnabled()
|
||||
{
|
||||
onEnabled.Invoke();
|
||||
}
|
||||
void OnDisable()
|
||||
{
|
||||
onDisabled.Invoke();
|
||||
}
|
||||
void OnDestroy()
|
||||
{
|
||||
onDestory.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
17
Unity/Scripts/Components/AutoSetLayer.cs
Normal file
17
Unity/Scripts/Components/AutoSetLayer.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class AutoSetLayer : MonoBehaviour
|
||||
{
|
||||
public int layer;
|
||||
void Start()
|
||||
{
|
||||
GetComponentsInChildren<Transform>(true).ForEach(x=>
|
||||
{
|
||||
x.gameObject.layer=layer;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
14
Unity/Scripts/Components/ClipBoardHelper.cs
Normal file
14
Unity/Scripts/Components/ClipBoardHelper.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class ClipBoardHelper : MonoBehaviour
|
||||
{
|
||||
public string clip;
|
||||
public void Excute()
|
||||
{
|
||||
UnityEngine.GUIUtility.systemCopyBuffer = clip;
|
||||
}
|
||||
}
|
||||
}
|
40
Unity/Scripts/Components/EffectPlayer.cs
Normal file
40
Unity/Scripts/Components/EffectPlayer.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Cysharp.Threading.Tasks;
|
||||
namespace BITKit
|
||||
{
|
||||
public class EffectPlayer : MonoBehaviour
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SerializeField] float duration;
|
||||
[Header(Constant.Header.Components)]
|
||||
[SerializeField] ParticleSystem[] particleSystems;
|
||||
[SerializeField] Behaviour[] behaviours;
|
||||
void Start()
|
||||
{
|
||||
particleSystems.ForEach(x => x.Stop());
|
||||
behaviours.ForEach(x => x.enabled = false);
|
||||
}
|
||||
public async void Excute()
|
||||
{
|
||||
particleSystems.ForEach(x =>
|
||||
{
|
||||
x.time = 0;
|
||||
x.Play(true);
|
||||
});
|
||||
behaviours.ForEach(x =>
|
||||
{
|
||||
x.enabled = true;
|
||||
});
|
||||
await UniTask.Delay(System.TimeSpan.FromSeconds(duration));
|
||||
if (BehaviourHelper.Actived)
|
||||
{
|
||||
behaviours.ForEach(x =>
|
||||
{
|
||||
x.enabled = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
15
Unity/Scripts/Components/ExcuteCommand.cs
Normal file
15
Unity/Scripts/Components/ExcuteCommand.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Sirenix.OdinInspector;
|
||||
namespace BITKit
|
||||
{
|
||||
public class ExcuteCommand : MonoBehaviour
|
||||
{
|
||||
public string command;
|
||||
public void Excute()
|
||||
{
|
||||
Data.Set("Cmd", command);
|
||||
}
|
||||
}
|
||||
}
|
25
Unity/Scripts/Components/ExcuteOnBehaviour.cs
Normal file
25
Unity/Scripts/Components/ExcuteOnBehaviour.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
namespace BITKit
|
||||
{
|
||||
public class ExcuteOnBehaviour : MonoBehaviour
|
||||
{
|
||||
public UnityEvent onAwake = new();
|
||||
public UnityEvent onStart = new();
|
||||
public UnityEvent onDestroy = new();
|
||||
void Awake()
|
||||
{
|
||||
onAwake.Invoke();
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
onStart.Invoke();
|
||||
}
|
||||
void OnDestroy()
|
||||
{
|
||||
onDestroy.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
21
Unity/Scripts/Components/ExcuteOnStart.cs
Normal file
21
Unity/Scripts/Components/ExcuteOnStart.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
namespace BITKit
|
||||
{
|
||||
public class NewBehaviourScript : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
10
Unity/Scripts/Components/Format/FormatData.cs
Normal file
10
Unity/Scripts/Components/Format/FormatData.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public abstract class FormatData : Provider<string>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
24
Unity/Scripts/Components/Format/FormatDataToKeyValue.cs
Normal file
24
Unity/Scripts/Components/Format/FormatDataToKeyValue.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class FormatDataToKeyValue : FormatData
|
||||
{
|
||||
[SubclassSelector, SerializeReference] public References key;
|
||||
public Provider output;
|
||||
Dictionary<string, string> dictionary = new();
|
||||
public override string Get()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
public override void Set(string value)
|
||||
{
|
||||
WWWForm form = new();
|
||||
form.AddField("key", key);
|
||||
form.AddField("value", value);
|
||||
|
||||
output?.Set(form);
|
||||
}
|
||||
}
|
||||
}
|
20
Unity/Scripts/Components/IfGreater.cs
Normal file
20
Unity/Scripts/Components/IfGreater.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class IfGreater : Provider<float>
|
||||
{
|
||||
public float value;
|
||||
public bool inverse;
|
||||
public Provider<bool> output;
|
||||
public override float Get()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
public override void Set(float t)
|
||||
{
|
||||
output?.Set(t > value ? inverse ? false : true : false);
|
||||
}
|
||||
}
|
||||
}
|
20
Unity/Scripts/Components/JsonToJObject.cs
Normal file
20
Unity/Scripts/Components/JsonToJObject.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
namespace BITKit
|
||||
{
|
||||
public class JsonToJObject : Provider<string>
|
||||
{
|
||||
public Provider output;
|
||||
public override string Get()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
public override void Set(string t)
|
||||
{
|
||||
output.Set(JObject.Parse(t));
|
||||
}
|
||||
}
|
||||
}
|
17
Unity/Scripts/Components/LoopEvent.cs
Normal file
17
Unity/Scripts/Components/LoopEvent.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
namespace BITKit
|
||||
{
|
||||
public class LoopEvent : MonoBehaviour
|
||||
{
|
||||
public UnityEvent[] events;
|
||||
int currentIndex;
|
||||
public void Excute()
|
||||
{
|
||||
events[currentIndex].Invoke();
|
||||
currentIndex = ++currentIndex % events.Length;
|
||||
}
|
||||
}
|
||||
}
|
19
Unity/Scripts/Components/ReadFile.cs
Normal file
19
Unity/Scripts/Components/ReadFile.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Sirenix.OdinInspector;
|
||||
using System.IO;
|
||||
namespace BITKit
|
||||
{
|
||||
public class ReadFile : MonoBehaviour
|
||||
{
|
||||
[FilePath(AbsolutePath = true)]
|
||||
public string path;
|
||||
public Provider output;
|
||||
[Button]
|
||||
public void Excute()
|
||||
{
|
||||
output.Set(File.ReadAllText(path));
|
||||
}
|
||||
}
|
||||
}
|
46
Unity/Scripts/Components/ReadFromTextAsset.cs
Normal file
46
Unity/Scripts/Components/ReadFromTextAsset.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
#endif
|
||||
namespace BITKit
|
||||
{
|
||||
public class ReadFromTextAsset : BITAction
|
||||
{
|
||||
public TextAsset asset;
|
||||
public Provider output;
|
||||
public TranslateSO translateSO;
|
||||
public override void Excute()
|
||||
{
|
||||
BITAppForUnity.ThrowIfNotPlaying();
|
||||
var value = asset.text;
|
||||
if (translateSO)
|
||||
{
|
||||
value = translateSO.Get(value);
|
||||
}
|
||||
output.Set<string>(value);
|
||||
}
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.CustomEditor(typeof(ReadFromTextAsset))]
|
||||
public class ReadFromTextAssetInspector : BITInspector<ReadFromTextAsset>
|
||||
{
|
||||
public override VisualElement CreateInspectorGUI()
|
||||
{
|
||||
CreateSubTitle("TextAsset Reader");
|
||||
FillDefaultInspector(root, serializedObject, true);
|
||||
var excute = root.Create<Button>();
|
||||
|
||||
//excute.bindingPath = nameof(ReadFromTextAsset.Excute);
|
||||
|
||||
excute.text = "读取文本";
|
||||
|
||||
excute.clicked += agent.Excute;
|
||||
return root;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
78
Unity/Scripts/Components/SaveToFile.cs
Normal file
78
Unity/Scripts/Components/SaveToFile.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Sirenix.OdinInspector;
|
||||
using System.IO;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using BITKit.IO;
|
||||
namespace BITKit
|
||||
{
|
||||
public class SaveToFile : Provider, IAction
|
||||
{
|
||||
public bool isPersistentData;
|
||||
public bool crypto;
|
||||
[SerializeReference, SubclassSelector] public References extensions;
|
||||
[SerializeReference, SubclassSelector] public References path;
|
||||
[SerializeReference, SubclassSelector] public NameProvider nameProvider;
|
||||
|
||||
public void Excute()
|
||||
{
|
||||
string path;
|
||||
if (isPersistentData)
|
||||
{
|
||||
path = PathHelper.GetFolderPath(Utility.Path.persistentDataPath, this.path.Get());
|
||||
}
|
||||
else
|
||||
{
|
||||
path = PathHelper.GetFolderPath(Utility.Path.dataPath, this.path);
|
||||
}
|
||||
BITApp.Open(path);
|
||||
}
|
||||
|
||||
public override async void Set<T>(T obj)
|
||||
{
|
||||
await UniTask.SwitchToThreadPool();
|
||||
|
||||
var path = GetPath();
|
||||
BIT4Log.Log<SaveToFile>($"正在保存文件为:{path}");
|
||||
switch (obj)
|
||||
{
|
||||
case string _string:
|
||||
try
|
||||
{
|
||||
if (crypto)
|
||||
{
|
||||
_string = GZipHelper.GZipCompressString(_string);
|
||||
}
|
||||
File.WriteAllText(path, _string);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogWarning(e);
|
||||
}
|
||||
break;
|
||||
case BITAssets assets:
|
||||
assets.Build(path);
|
||||
break;
|
||||
case byte[] _bytes:
|
||||
File.WriteAllBytes(path, _bytes);
|
||||
break;
|
||||
}
|
||||
}
|
||||
string GetPath()
|
||||
{
|
||||
string path;
|
||||
var fileName = nameProvider.GetName(null, extensions);
|
||||
if (isPersistentData)
|
||||
{
|
||||
path = PathHelper.GetFilePath(Utility.Path.persistentDataPath, this.path.Get(), fileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
path = PathHelper.GetFilePath(Utility.Path.dataPath, this.path, fileName);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
18
Unity/Scripts/Components/SetColor.cs
Normal file
18
Unity/Scripts/Components/SetColor.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class SetColor : Provider<Color>
|
||||
{
|
||||
public MeshRenderer meshRenderer;
|
||||
public override Color Get()
|
||||
{
|
||||
return meshRenderer.sharedMaterial.color;
|
||||
}
|
||||
public override void Set(Color t)
|
||||
{
|
||||
meshRenderer.sharedMaterial.color = t; ;
|
||||
}
|
||||
}
|
||||
}
|
44
Unity/Scripts/Components/SetRenderScale.cs
Normal file
44
Unity/Scripts/Components/SetRenderScale.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public class SetRenderScale : Provider<float>
|
||||
{
|
||||
public UniversalRenderPipelineAsset asset;
|
||||
public override float Get()
|
||||
{
|
||||
return asset.renderScale;
|
||||
}
|
||||
public override void Set(float t)
|
||||
{
|
||||
asset.renderScale = t;
|
||||
}
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.CustomEditor(typeof(SetRenderScale))]
|
||||
public class SetRenderScaleInspector : BITInspector<SetRenderScale>
|
||||
{
|
||||
public override VisualElement CreateInspectorGUI()
|
||||
{
|
||||
FillDefaultInspector(root, serializedObject, true);
|
||||
|
||||
var slider = root.Create<Slider>();
|
||||
slider.SetValueWithoutNotify(agent.asset.renderScale);
|
||||
slider.label = "渲染倍数";
|
||||
slider.showInputField=true;
|
||||
slider.RegisterValueChangedCallback(OnValueUpdate);
|
||||
|
||||
return root;
|
||||
}
|
||||
void OnValueUpdate(ChangeEvent<float> newValueEvent)
|
||||
{
|
||||
BITAppForUnity.ThrowIfNotPlaying();
|
||||
agent.Set(newValueEvent.newValue);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
46
Unity/Scripts/Components/SetRotation.cs
Normal file
46
Unity/Scripts/Components/SetRotation.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class SetRotation : Provider
|
||||
{
|
||||
public Transform root;
|
||||
public bool isLocal;
|
||||
public Vector3 weight;
|
||||
public Vector3 defaultEulur;
|
||||
void Awake()
|
||||
{
|
||||
defaultEulur = isLocal ? transform.localEulerAngles : transform.eulerAngles;
|
||||
}
|
||||
public override void Set<T>(T obj)
|
||||
{
|
||||
switch (obj)
|
||||
{
|
||||
case float _float:
|
||||
_SetRotation(Quaternion.Euler(defaultEulur + weight * _float));
|
||||
break;
|
||||
case Vector3 _vector3:
|
||||
_SetRotation(Quaternion.Euler(_vector3));
|
||||
break;
|
||||
case Quaternion quaternion:
|
||||
_SetRotation(quaternion);
|
||||
break;
|
||||
}
|
||||
}
|
||||
void _SetRotation(Quaternion eulur)
|
||||
{
|
||||
if (root)
|
||||
{
|
||||
if (isLocal)
|
||||
{
|
||||
root.localRotation = eulur;
|
||||
}
|
||||
else
|
||||
{
|
||||
root.rotation = eulur;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
18
Unity/Scripts/Components/ShowDictionary.cs
Normal file
18
Unity/Scripts/Components/ShowDictionary.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class ShowDictionary : Provider<IDictionary<string, string>>
|
||||
{
|
||||
public IDictionary<string, string> dictionary = new Dictionary<string, string>();
|
||||
public override IDictionary<string, string> Get()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
public override void Set(IDictionary<string, string> t)
|
||||
{
|
||||
dictionary = t;
|
||||
}
|
||||
}
|
||||
}
|
45
Unity/Scripts/Components/ShowProfiler.cs
Normal file
45
Unity/Scripts/Components/ShowProfiler.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class ShowProfiler : BITBehavior, IDiagnostics
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SubclassSelector, SerializeReference] public References pingAddress;
|
||||
[Header(Constant.Header.Output)]
|
||||
public Provider fpsOutput;
|
||||
public Provider pingOutput;
|
||||
DeltaTimer timer = new();
|
||||
Ping ping;
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
public int frameRate;
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
timer.Update();
|
||||
frameRate = timer;
|
||||
fpsOutput.Set((string)timer);
|
||||
if (pingOutput is not null && (ping is null || ping.isDone))
|
||||
{
|
||||
if (ping is not null && ping.isDone)
|
||||
{
|
||||
pingOutput.Set(ping.time.ToString());
|
||||
}
|
||||
ping = new(pingAddress);
|
||||
}
|
||||
}
|
||||
public override string GetName()
|
||||
{
|
||||
return nameof(ShowProfiler);
|
||||
}
|
||||
public override object GetDiagnostics()
|
||||
{
|
||||
Dictionary<string, string> dictioanry = new();
|
||||
dictioanry.Add(nameof(fpsOutput), fpsOutput ? "有效" : "未定义");
|
||||
dictioanry.Add(nameof(pingOutput), pingOutput ? "有效" : "未定义");
|
||||
dictioanry.Add("FPS", timer);
|
||||
return dictioanry;
|
||||
}
|
||||
}
|
||||
}
|
18
Unity/Scripts/Components/ShowTime.cs
Normal file
18
Unity/Scripts/Components/ShowTime.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Sirenix.OdinInspector;
|
||||
namespace BITKit
|
||||
{
|
||||
public class ShowTime : BITBehavior
|
||||
{
|
||||
[SerializeReference,SubclassSelector]public References timeFormat;
|
||||
public Provider output;
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
output?.Set(timeFormat is not null ? DateTime.Now.ToString(timeFormat) : DateTime.Now.ToString());
|
||||
}
|
||||
}
|
||||
}
|
15
Unity/Scripts/Components/ShowVersion.cs
Normal file
15
Unity/Scripts/Components/ShowVersion.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Sirenix.OdinInspector;
|
||||
namespace BITKit
|
||||
{
|
||||
public class ShowVersion : MonoBehaviour
|
||||
{
|
||||
public Provider output;
|
||||
void Start()
|
||||
{
|
||||
output?.Set(Application.version);
|
||||
}
|
||||
}
|
||||
}
|
359
Unity/Scripts/Components/SimpleCameraLook.cs
Normal file
359
Unity/Scripts/Components/SimpleCameraLook.cs
Normal file
@@ -0,0 +1,359 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using Cinemachine;
|
||||
using Cinemachine.Utility;
|
||||
using TouchPhase = UnityEngine.InputSystem.TouchPhase;
|
||||
using UnityEngine.InputSystem.EnhancedTouch;
|
||||
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
|
||||
using BITKit.IO;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using Cysharp.Threading.Tasks;
|
||||
namespace BITKit
|
||||
{
|
||||
public class SimpleCameraLook : MonoBehaviour
|
||||
{
|
||||
private string fileName => Path.Combine(Utility.Path.persistentDataPath, gameObject.scene.name, $"{gameObject.GetInstanceID()}.bit");
|
||||
public enum State
|
||||
{
|
||||
Mouse,
|
||||
Touchscreen,
|
||||
}
|
||||
[Header(Constant.Header.Gameobjects)]
|
||||
public Transform cameraRoot;
|
||||
public CinemachineVirtualCamera virtualCamera;
|
||||
[Header(Constant.Header.Settings)]
|
||||
public TransformMode touchMode;
|
||||
[Header(Constant.Header.Settings)]
|
||||
public InputActionReference viewAction;
|
||||
public InputActionReference movementAction;
|
||||
public InputActionReference adsAction;
|
||||
public InputActionReference entryAction;
|
||||
public InputActionReference entryMoveAction;
|
||||
public InputActionGroup inputGroup = new();
|
||||
[Header(Constant.Header.Settings)]
|
||||
public float minEntrySqrMagnitude = 1;
|
||||
public LayerMask layerMask;
|
||||
[Header(Constant.Header.Settings)]
|
||||
public AnimationCurve scollCurve = new();
|
||||
public AnimationCurve moveCurve = new();
|
||||
public AnimationCurve touchMoveCurve = new();
|
||||
public Vector2 cameraDistanceLimit = new(1, 64);
|
||||
[Header(Constant.Header.Debug)]
|
||||
public int touchesCount;
|
||||
public bool isParallelVector;
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
public Vector3 lookInput;
|
||||
public Vector3 moveVector;
|
||||
public ValidHandle freeLooking = new();
|
||||
public ValidHandle moving = new();
|
||||
private Cinemachine3rdPersonFollow tpv;
|
||||
private Location startLocation;
|
||||
private Location savedLocation;
|
||||
private float savedDistance;
|
||||
private float startDistance;
|
||||
private CinemachineBrain _brain;
|
||||
public void Reset()
|
||||
{
|
||||
lookInput = startLocation.position.TransientRotationAxis();
|
||||
cameraRoot.position = startLocation;
|
||||
tpv.CameraDistance = startDistance;
|
||||
}
|
||||
public void DisableInput(bool disable)
|
||||
{
|
||||
inputGroup.allowInput.SetDisableElements(nameof(DisableInput), disable);
|
||||
}
|
||||
public void SetTouchMode(int index)
|
||||
{
|
||||
SetTouchMode((TransformMode)index);
|
||||
}
|
||||
public void SetTouchMode(TransformMode mode)
|
||||
{
|
||||
touchMode = mode;
|
||||
}
|
||||
public void Align(Transform target)
|
||||
{
|
||||
if (target.TryGetComponent<SimpleCameraLook>(out var x))
|
||||
{
|
||||
cameraRoot.position = x.cameraRoot.position;
|
||||
//lookInput = x.lookInput;
|
||||
lookInput = x.cameraRoot.rotation.eulerAngles.TransientRotationAxis();
|
||||
if (x.tpv)
|
||||
tpv.CameraDistance = x.tpv.CameraDistance;
|
||||
}
|
||||
else if (target.TryGetComponent<CinemachineVirtualCamera>(out var vCam))
|
||||
{
|
||||
cameraRoot.position = vCam.Follow.position;
|
||||
lookInput = vCam.LookAt.rotation.eulerAngles.TransientRotationAxis();
|
||||
var e = vCam.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
|
||||
if (e)
|
||||
{
|
||||
tpv.CameraDistance = e.CameraDistance;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
cameraRoot.position = target.position;
|
||||
lookInput = target.eulerAngles.TransientRotationAxis();
|
||||
}
|
||||
}
|
||||
public void SaveLocation()
|
||||
{
|
||||
savedLocation = new(cameraRoot);
|
||||
savedDistance = tpv.CameraDistance;
|
||||
BITAssets.Build(
|
||||
PathHelper.GetFilePath(fileName),
|
||||
new IAsset[]
|
||||
{
|
||||
new BITAsset(nameof(savedLocation),JsonUtility.ToJson(savedLocation)),
|
||||
new BITAsset(nameof(savedDistance),JsonConvert.SerializeObject(savedDistance)),
|
||||
}
|
||||
);
|
||||
}
|
||||
public void LoadLocation()
|
||||
{
|
||||
lookInput = savedLocation.rotation.eulerAngles.TransientRotationAxis();
|
||||
cameraRoot.position = savedLocation;
|
||||
tpv.CameraDistance = savedDistance;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
freeLooking.AddListener(OnFreeLook);
|
||||
tpv = virtualCamera.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
|
||||
|
||||
savedLocation = startLocation = new(cameraRoot);
|
||||
savedDistance = startDistance = tpv.CameraDistance;
|
||||
|
||||
inputGroup.RegisterCallback(viewAction, OnView);
|
||||
inputGroup.RegisterCallback(movementAction, OnMovement);
|
||||
inputGroup.RegisterCallback(adsAction, OnAds);
|
||||
inputGroup.RegisterCallback(entryAction, OnEntry);
|
||||
inputGroup.RegisterCallback(entryMoveAction, OnEntryMoveAction);
|
||||
|
||||
if (Camera.main != null) _brain = Camera.main.GetComponent<CinemachineBrain>();
|
||||
}
|
||||
|
||||
private async void Start()
|
||||
{
|
||||
await UniTask.SwitchToThreadPool();
|
||||
if (File.Exists(fileName))
|
||||
{
|
||||
savedLocation = BITAssets.Read<Location>(fileName, nameof(savedLocation));
|
||||
savedDistance = BITAssets.Read<float>(fileName, nameof(savedDistance));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
SetEnabled(true);
|
||||
lookInput = cameraRoot.eulerAngles.TransientRotationAxis();
|
||||
|
||||
freeLooking.RemoveDisableElements(this);
|
||||
|
||||
virtualCamera.enabled = true;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
SetEnabled(false);
|
||||
freeLooking.AddDisableElements(this);
|
||||
|
||||
virtualCamera.enabled = false;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
inputGroup.Dispose();
|
||||
}
|
||||
|
||||
private void SetEnabled(bool enabled)
|
||||
{
|
||||
//BIT4Log.Log<SimpleCameraLook>($"正在设置输入状态:{enabled}");
|
||||
BITAppForUnity.AllowTouchSupport.SetElements(this, enabled);
|
||||
inputGroup.allowInput.SetElements(this, enabled);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
//var debuger = DI.Get<IDebuger>();
|
||||
var playerConfig = PlayerConfig.singleton;
|
||||
var sensitivity = playerConfig.touchSensitivity;
|
||||
|
||||
//debuger.Log(nameof(Touch.activeTouches), Touch.activeTouches.Count.ToString());
|
||||
touchesCount = Touch.activeTouches.Count;
|
||||
|
||||
if (Mouse.current.leftButton.wasPressedThisFrame)
|
||||
{
|
||||
var ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
|
||||
if (Physics.Raycast(ray, out var raycast, 256, layerMask))
|
||||
{
|
||||
cameraRoot.position = raycast.point;
|
||||
}
|
||||
}
|
||||
|
||||
if (_brain)
|
||||
{
|
||||
inputGroup.allowInput.SetDisableElements(nameof(CinemachineBrain),virtualCamera != (CinemachineVirtualCamera)_brain.ActiveVirtualCamera);
|
||||
}
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
lookInput.x = Mathf.Clamp(lookInput.x, -80, 80);
|
||||
var newRotation = Quaternion.Euler(lookInput);
|
||||
cameraRoot.rotation = Quaternion.Lerp(cameraRoot.rotation, newRotation, 48 * Time.deltaTime);
|
||||
var offset = cameraRoot.localRotation * moveVector;
|
||||
cameraRoot.localPosition -= offset * Time.deltaTime;
|
||||
}
|
||||
|
||||
private void OnView(InputAction.CallbackContext context)
|
||||
{
|
||||
var playerConfig = PlayerConfig.singleton;
|
||||
var sensitivity = playerConfig.sensitivity * playerConfig.m_yaw;
|
||||
var delta = context.ReadValue<Vector2>();
|
||||
switch (context.control.device)
|
||||
{
|
||||
case Mouse mouse:
|
||||
if (freeLooking && !moving)
|
||||
{
|
||||
lookInput.x -= delta.y * sensitivity;
|
||||
lookInput.y += delta.x * sensitivity;
|
||||
}
|
||||
break;
|
||||
case Touchscreen touchscreen:
|
||||
/* lookInput.x -= delta.y * sensitivity;
|
||||
lookInput.y += delta.x * sensitivity; */
|
||||
switch (touchMode)
|
||||
{
|
||||
case TransformMode.Move:
|
||||
break;
|
||||
case TransformMode.Rotate:
|
||||
lookInput.x -= delta.y * sensitivity;
|
||||
lookInput.y += delta.x * sensitivity;
|
||||
moveVector = default;
|
||||
break;
|
||||
case TransformMode.Scale:
|
||||
break;
|
||||
}
|
||||
if (touchMode is TransformMode.Scale)
|
||||
{
|
||||
OnAds(context);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAds(InputAction.CallbackContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
BITAppForUnity.ThrowIfWindowNotFocus();
|
||||
switch (context.control.device)
|
||||
{
|
||||
case Mouse mouse:
|
||||
OnAds(context.ReadValue<Vector2>().y);
|
||||
break;
|
||||
case Touchscreen touchscreen:
|
||||
/* lookInput.x -= delta.y * sensitivity;
|
||||
lookInput.y += delta.x * sensitivity; */
|
||||
switch (touchMode)
|
||||
{
|
||||
case TransformMode.Move:
|
||||
break;
|
||||
case TransformMode.Rotate:
|
||||
moveVector = default;
|
||||
break;
|
||||
case TransformMode.Scale:
|
||||
OnAds(context.ReadValue<Vector2>().y);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (MouseNotOverGameViewException)
|
||||
{
|
||||
|
||||
}
|
||||
catch (System.Exception)
|
||||
{
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void OnAds(float delta)
|
||||
{
|
||||
var newDistance = tpv.CameraDistance -
|
||||
delta * Time.deltaTime * scollCurve.Evaluate(tpv.CameraDistance);
|
||||
tpv.CameraDistance = Mathf.Clamp(newDistance, cameraDistanceLimit.x, cameraDistanceLimit.y);
|
||||
}
|
||||
|
||||
private void OnFreeLook(bool active)
|
||||
{
|
||||
BITAppForUnity.AllowCursor.SetDisableElements(this, active);
|
||||
}
|
||||
|
||||
private void OnEntry(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context.started)
|
||||
{
|
||||
var delta = viewAction.action.ReadValue<Vector2>();
|
||||
if (delta.sqrMagnitude > minEntrySqrMagnitude)
|
||||
freeLooking.AddElement(this);
|
||||
}
|
||||
else if (context.canceled)
|
||||
{
|
||||
freeLooking.RemoveElement(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMovement(InputAction.CallbackContext context)
|
||||
{
|
||||
switch (context.control.device)
|
||||
{
|
||||
case Mouse mouse:
|
||||
if (moving && freeLooking)
|
||||
{
|
||||
moveVector = context.ReadValue<Vector2>() * moveCurve.Evaluate(tpv.CameraDistance);
|
||||
}
|
||||
break;
|
||||
case Touchscreen touchscreen:
|
||||
/* lookInput.x -= delta.y * sensitivity;
|
||||
lookInput.y += delta.x * sensitivity; */
|
||||
switch (touchMode)
|
||||
{
|
||||
case TransformMode.Move:
|
||||
moveVector = context.ReadValue<Vector2>() * moveCurve.Evaluate(tpv.CameraDistance);
|
||||
break;
|
||||
case TransformMode.Rotate:
|
||||
break;
|
||||
case TransformMode.Scale:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (context.canceled)
|
||||
{
|
||||
moveVector = default;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEntryMoveAction(InputAction.CallbackContext context)
|
||||
{
|
||||
|
||||
if (context.started)
|
||||
{
|
||||
moving.AddElement(this);
|
||||
}
|
||||
else if (context.canceled)
|
||||
{
|
||||
moving.RemoveElement(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
37
Unity/Scripts/Components/StopWatcher.cs
Normal file
37
Unity/Scripts/Components/StopWatcher.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class StopWatcher : MonoBehaviour
|
||||
{
|
||||
[SerializeReference, SubclassSelector] public References timeFormat;
|
||||
public bool autoStart;
|
||||
public Provider output;
|
||||
bool started;
|
||||
DateTime dateTime = new();
|
||||
void Start()
|
||||
{
|
||||
if (autoStart)
|
||||
{
|
||||
started = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (started)
|
||||
{
|
||||
dateTime = dateTime.AddSeconds(Time.deltaTime);
|
||||
var timeStr = dateTime.ToString();
|
||||
if (timeFormat is not null)
|
||||
{
|
||||
timeStr = dateTime.ToString(timeFormat);
|
||||
}
|
||||
output.Set(timeStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
21
Unity/Scripts/Components/StringProviders.cs
Normal file
21
Unity/Scripts/Components/StringProviders.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class StringProviders : Provider<string>
|
||||
{
|
||||
public Provider<string>[] providers;
|
||||
public override string Get()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
public override void Set(string t)
|
||||
{
|
||||
foreach (var provider in providers)
|
||||
{
|
||||
provider.Set(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
29
Unity/Scripts/Components/TranslateComponent.cs
Normal file
29
Unity/Scripts/Components/TranslateComponent.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Cysharp.Threading.Tasks;
|
||||
namespace BITKit
|
||||
{
|
||||
public sealed class TranslateComponent : Provider<string>
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
public TranslateSO so;
|
||||
public bool async;
|
||||
[Header(Constant.Header.Output)]
|
||||
public Provider output;
|
||||
[TextArea]
|
||||
public string result;
|
||||
public override string Get()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
public override async void Set(string t)
|
||||
{
|
||||
if (async)
|
||||
{
|
||||
await UniTask.SwitchToThreadPool();
|
||||
}
|
||||
output.Set(result = so.Get(t, false));
|
||||
}
|
||||
}
|
||||
}
|
37
Unity/Scripts/Components/WorldToScreenPoint.cs
Normal file
37
Unity/Scripts/Components/WorldToScreenPoint.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.UIElements;
|
||||
namespace BITKit
|
||||
{
|
||||
public class WorldToScreenPoint : MonoBehaviour
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SerializeField] Transform root;
|
||||
[SerializeField] Vector3 offset;
|
||||
[SerializeField] float activeDistance;
|
||||
[Header(Constant.Header.Events)]
|
||||
[SerializeField] private UnityEvent<bool> setHeaderEnabled;
|
||||
[SerializeField] private UnityEvent<Vector3> setHeaderPosition;
|
||||
Transform cameraTrans;
|
||||
void Start()
|
||||
{
|
||||
if (Camera.main != null) cameraTrans = Camera.main.transform;
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
var position = root.position + root.rotation * offset;
|
||||
|
||||
setHeaderPosition.Invoke(position);
|
||||
|
||||
if (activeDistance is not (0 or -1))
|
||||
{
|
||||
var distance = Vector3.Distance(position, cameraTrans.position);
|
||||
var _enabled = distance < activeDistance;
|
||||
setHeaderEnabled.Invoke(_enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user