AR骑行人物控制代码重构&build混淆代码

This commit is contained in:
lishuo committed 2023-02-02 18:41:55 +08:00
1 parent 7822584cdd
commit 2c9440f656
78 files changed
+562 -8031

No files matched your search

@@ -4,168 +4,165 @@ using UnityEngine;
using System;
using System.Linq;
namespace Assets.Scenes.Ride.Scripts
public class GameEvent { string key { get; set; } }
public class EventQueueSystem : MonoSingleton<EventQueueSystem>
{
public class GameEvent { string key { get; set; } }
public delegate void EventDelegate<T>(T e) where T : GameEvent;
public class EventQueueSystem : MonoSingleton<EventQueueSystem>
private delegate void InternalEventDelegate(GameEvent e);
private Dictionary<Type, InternalEventDelegate> delegates = new Dictionary<Type, InternalEventDelegate>();
private Dictionary<Delegate, InternalEventDelegate> delegateLookup = new Dictionary<Delegate, InternalEventDelegate>();
private Dictionary<InternalEventDelegate, Delegate> delegateLookOnce = new Dictionary<InternalEventDelegate, Delegate>();
private Queue eventQueue = new Queue();
public bool bLimitQueueProcessing = false;
public float limitQueueTime = 1f;
//注册侦听事件(持续)
public static void AddListener<T>(EventDelegate<T> del) where T : GameEvent
{
public delegate void EventDelegate<T>(T e) where T : GameEvent;
private delegate void InternalEventDelegate(GameEvent e);
private Dictionary<Type, InternalEventDelegate> delegates = new Dictionary<Type, InternalEventDelegate>();
private Dictionary<Delegate, InternalEventDelegate> delegateLookup = new Dictionary<Delegate, InternalEventDelegate>();
private Dictionary<InternalEventDelegate, Delegate> delegateLookOnce = new Dictionary<InternalEventDelegate, Delegate>();
private Queue eventQueue = new Queue();
public bool bLimitQueueProcessing = false;
public float limitQueueTime = 1f;
//注册侦听事件(持续)
public static void AddListener<T>(EventDelegate<T> del) where T : GameEvent
if (!HasListener(del))
{
if (!HasListener(del))
{
Debug.Log(30);
Instance.AddDelegate(del);
}
}
//注册侦听事件(一次)
public static void AddListenerOnce<T>(EventDelegate<T> del) where T : GameEvent
{
var result = Instance.AddDelegate(del);
if (result != null)
Instance.delegateLookOnce[result] = del;
}
//判定侦听事件是否存在
public static bool HasListener<T>(EventDelegate<T> del) where T : GameEvent
{
return Instance.delegateLookup.Keys.Where(c => c.GetType() == del.GetType()).Any();
//return Instance.delegateLookup.ContainsKey(del);
}
//移除侦听事件
public static void RemoveListener<T>(EventDelegate<T> del) where T : GameEvent
{
if (Instance == null)
return;
if (Instance.delegateLookup.TryGetValue(del, out InternalEventDelegate eventDelegate))
{
if (Instance.delegates.TryGetValue(typeof(T), out InternalEventDelegate temp))
{
temp -= eventDelegate;
if (temp == null)
Instance.delegates.Remove(typeof(T));
else
Instance.delegates[typeof(T)] = temp;
}
Instance.delegateLookup.Remove(del);
keyList.Clear();
}
}
public static void RemoveAll()
{
if (Instance != null)
{
Instance.delegates.Clear();
Instance.delegateLookup.Clear();
Instance.delegateLookOnce.Clear();
}
}
private InternalEventDelegate AddDelegate<T>(EventDelegate<T> del) where T : GameEvent
{
if (delegateLookup.ContainsKey(del))
return null;
void eventDelegate(GameEvent e) => del((T)e);
delegateLookup[del] = eventDelegate;
if (delegates.TryGetValue(typeof(T), out InternalEventDelegate temp))
delegates[typeof(T)] = temp += eventDelegate;
else
delegates[typeof(T)] = eventDelegate;
return eventDelegate;
}
//单个事件触发
private static void TriggerEvent(GameEvent e)
{
var type = e.GetType();
if (Instance.delegates.TryGetValue(type, out InternalEventDelegate eventDelegate))
{
eventDelegate.Invoke(e);
//移除单一侦听
foreach (InternalEventDelegate item in Instance.delegates[type].GetInvocationList())
{
if (Instance.delegateLookOnce.TryGetValue(item, out Delegate temp))
{
Instance.delegates[type] -= item;
if (Instance.delegates[type] == null)
Instance.delegates.Remove(type);
Instance.delegateLookup.Remove(temp);
Instance.delegateLookOnce.Remove(item);
}
}
}
}
//外部调用的推入事件队列接口
public static void QueueEvent(GameEvent e)
{
if (!Instance.delegates.ContainsKey(e.GetType()))
return;
Instance.eventQueue.Enqueue(e);
}
private static Dictionary<string,GameEvent> keyList = new Dictionary<string, GameEvent>();
public static void QueueEventOnce(GameEvent e,string key)
{
if (!Instance.delegates.ContainsKey(e.GetType()))
return;
if (keyList.ContainsKey(key))
return;
keyList.Add(key,e);
Instance.eventQueue.Enqueue(e);
}
float timer = 0.0f;
//事件队列触发处理
void Update()
{
//float timer = 0.0f;
//while (eventQueue.Count > 0)
//{
// if (bLimitQueueProcessing)
// if (timer > limitQueueTime)
// return;
// var e = eventQueue.Dequeue() as GameEvent;
// TriggerEvent(e);
// if (bLimitQueueProcessing)
// timer += Time.deltaTime;
//}
timer += Time.deltaTime;
while (timer >= 2 && eventQueue.Count > 0)
{
var e = eventQueue.Dequeue() as GameEvent;
TriggerEvent(e);
timer = 0;
}
}
private void OnApplicationQuit()
{
RemoveAll();
eventQueue.Clear();
}
public static void ClearQueue()
{
if (Instance != null)
Instance.eventQueue.Clear();
Debug.Log(30);
Instance.AddDelegate(del);
}
}
}
//注册侦听事件(一次)
public static void AddListenerOnce<T>(EventDelegate<T> del) where T : GameEvent
{
var result = Instance.AddDelegate(del);
if (result != null)
Instance.delegateLookOnce[result] = del;
}
//判定侦听事件是否存在
public static bool HasListener<T>(EventDelegate<T> del) where T : GameEvent
{
return Instance.delegateLookup.Keys.Where(c => c.GetType() == del.GetType()).Any();
//return Instance.delegateLookup.ContainsKey(del);
}
//移除侦听事件
public static void RemoveListener<T>(EventDelegate<T> del) where T : GameEvent
{
if (Instance == null)
return;
if (Instance.delegateLookup.TryGetValue(del, out InternalEventDelegate eventDelegate))
{
if (Instance.delegates.TryGetValue(typeof(T), out InternalEventDelegate temp))
{
temp -= eventDelegate;
if (temp == null)
Instance.delegates.Remove(typeof(T));
else
Instance.delegates[typeof(T)] = temp;
}
Instance.delegateLookup.Remove(del);
keyList.Clear();
}
}
public static void RemoveAll()
{
if (Instance != null)
{
Instance.delegates.Clear();
Instance.delegateLookup.Clear();
Instance.delegateLookOnce.Clear();
}
}
private InternalEventDelegate AddDelegate<T>(EventDelegate<T> del) where T : GameEvent
{
if (delegateLookup.ContainsKey(del))
return null;
void eventDelegate(GameEvent e) => del((T)e);
delegateLookup[del] = eventDelegate;
if (delegates.TryGetValue(typeof(T), out InternalEventDelegate temp))
delegates[typeof(T)] = temp += eventDelegate;
else
delegates[typeof(T)] = eventDelegate;
return eventDelegate;
}
//单个事件触发
private static void TriggerEvent(GameEvent e)
{
var type = e.GetType();
if (Instance.delegates.TryGetValue(type, out InternalEventDelegate eventDelegate))
{
eventDelegate.Invoke(e);
//移除单一侦听
foreach (InternalEventDelegate item in Instance.delegates[type].GetInvocationList())
{
if (Instance.delegateLookOnce.TryGetValue(item, out Delegate temp))
{
Instance.delegates[type] -= item;
if (Instance.delegates[type] == null)
Instance.delegates.Remove(type);
Instance.delegateLookup.Remove(temp);
Instance.delegateLookOnce.Remove(item);
}
}
}
}
//外部调用的推入事件队列接口
public static void QueueEvent(GameEvent e)
{
if (!Instance.delegates.ContainsKey(e.GetType()))
return;
Instance.eventQueue.Enqueue(e);
}
private static Dictionary<string, GameEvent> keyList = new Dictionary<string, GameEvent>();
public static void QueueEventOnce(GameEvent e, string key)
{
if (!Instance.delegates.ContainsKey(e.GetType()))
return;
if (keyList.ContainsKey(key))
return;
keyList.Add(key, e);
Instance.eventQueue.Enqueue(e);
}
float timer = 0.0f;
//事件队列触发处理
void Update()
{
//float timer = 0.0f;
//while (eventQueue.Count > 0)
//{
// if (bLimitQueueProcessing)
// if (timer > limitQueueTime)
// return;
// var e = eventQueue.Dequeue() as GameEvent;
// TriggerEvent(e);
// if (bLimitQueueProcessing)
// timer += Time.deltaTime;
//}
timer += Time.deltaTime;
while (timer >= 2 && eventQueue.Count > 0)
{
var e = eventQueue.Dequeue() as GameEvent;
TriggerEvent(e);
timer = 0;
}
}
private void OnApplicationQuit()
{
RemoveAll();
eventQueue.Clear();
}
public static void ClearQueue()
{
if (Instance != null)
Instance.eventQueue.Clear();
}
}
@@ -473,5 +473,26 @@ namespace Assets.Scenes.Ride.Scripts
}
return startTime;
}
//截图
public static void CaptureCamera(Camera camera, Rect rect, string fileName)
{
byte[] bytes = CaptureCameraReturnByte(camera, rect);
System.IO.File.WriteAllBytes(fileName, bytes);
}
private static byte[] CaptureCameraReturnByte(Camera camera, Rect rect)
{
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
camera.targetTexture = rt;
camera.Render();
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
camera.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy(rt);
return screenShot.EncodeToJPG();
}
}
}
@@ -5,62 +5,60 @@ using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scenes.Ride.Scripts
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
private static T instance = null;
private static readonly object locker = new object();
private static bool bAppQuitting;
public static T Instance
{
private static T instance = null;
private static readonly object locker = new object();
private static bool bAppQuitting;
public static T Instance
get
{
get
if (bAppQuitting)
{
if (bAppQuitting)
{
instance = null;
return instance;
}
instance = null;
return instance;
}
lock (locker)
lock (locker)
{
if (instance == null)
{
instance = FindObjectOfType<T>();
if (FindObjectsOfType<T>().Length > 1)
{
Debug.LogError("不应该存在多个单例!");
return instance;
}
if (instance == null)
{
instance = FindObjectOfType<T>();
if (FindObjectsOfType<T>().Length > 1)
{
Debug.LogError("不应该存在多个单例!");
return instance;
}
if (instance == null)
{
var singleton = new GameObject();
instance = singleton.AddComponent<T>();
singleton.name = "(singleton)" + typeof(T);
singleton.hideFlags = HideFlags.None;
DontDestroyOnLoad(singleton);
}
else
DontDestroyOnLoad(instance.gameObject);
var singleton = new GameObject();
instance = singleton.AddComponent<T>();
singleton.name = "(singleton)" + typeof(T);
singleton.hideFlags = HideFlags.None;
DontDestroyOnLoad(singleton);
}
instance.hideFlags = HideFlags.None;
return instance;
else
DontDestroyOnLoad(instance.gameObject);
}
instance.hideFlags = HideFlags.None;
return instance;
}
}
}
private void Awake()
{
bAppQuitting = false;
}
private void Awake()
{
bAppQuitting = false;
}
private void OnDestroy()
{
bAppQuitting = true;
}
private void OnDestroy()
{
bAppQuitting = true;
}
}