fix:代码提交
This commit is contained in:
23
Assets/Scripts/Core/ActionBase.cs
Normal file
23
Assets/Scripts/Core/ActionBase.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.UI;
|
||||
using DG.Tweening;
|
||||
|
||||
public abstract class ActionBase : MonoBehaviour
|
||||
{
|
||||
public abstract void Init();
|
||||
public abstract void RegisterAction();
|
||||
public abstract void RemoveAction();
|
||||
|
||||
public T GetChildComponent<T>(string path) where T : MonoBehaviour
|
||||
{
|
||||
GameObject go = transform.Find(path).gameObject;
|
||||
T t = go.GetComponent<T>();
|
||||
if (t == null)
|
||||
{
|
||||
t = go.AddComponent<T>();
|
||||
}
|
||||
return t;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Core/ActionBase.cs.meta
Normal file
11
Assets/Scripts/Core/ActionBase.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a772b5c480e09141843b6ab5f8abb51
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
158
Assets/Scripts/Core/ActionCenter.cs
Normal file
158
Assets/Scripts/Core/ActionCenter.cs
Normal file
@@ -0,0 +1,158 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class ActionCenter : MonoBehaviour
|
||||
{
|
||||
private static ActionCenter _instance;
|
||||
public static ActionCenter Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
string name = "ActionCenter";
|
||||
GameObject obj = GameObject.Find(name);
|
||||
if (obj == null)
|
||||
{
|
||||
obj = new GameObject(name);
|
||||
_instance = obj.AddComponent<ActionCenter>();
|
||||
}
|
||||
else
|
||||
{
|
||||
_instance = obj.GetComponent<ActionCenter>();
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = obj.AddComponent<ActionCenter>();
|
||||
}
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
//场景中所有ActionBase对象列表
|
||||
private List<ActionBase> actionList = new List<ActionBase>();
|
||||
|
||||
//初始化,场景加载完成后调用
|
||||
public void Init()
|
||||
{
|
||||
ActionBase[] arr = Resources.FindObjectsOfTypeAll<ActionBase>();
|
||||
for (int i = 0; i < arr.Length; i++)
|
||||
{
|
||||
if (arr[i].gameObject.scene.buildIndex >= 0)
|
||||
{
|
||||
arr[i].Init();
|
||||
arr[i].RegisterAction();
|
||||
actionList.Add(arr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddAction(ActionBase action)
|
||||
{
|
||||
actionList.Add(action);
|
||||
action.Init();
|
||||
action.RegisterAction();
|
||||
}
|
||||
|
||||
public void RemoveAction(ActionBase action)
|
||||
{
|
||||
if (actionList.Contains(action))
|
||||
{
|
||||
actionList.Remove(action);
|
||||
}
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
for (int i = 0; i < actionList.Count; i++)
|
||||
{
|
||||
actionList[i].RemoveAction();
|
||||
}
|
||||
}
|
||||
|
||||
#region 执行事件
|
||||
|
||||
//执行事件
|
||||
public void DoAction(UnityAction action)
|
||||
{
|
||||
action?.Invoke();
|
||||
}
|
||||
|
||||
//执行事件
|
||||
public void DoAction<T>(UnityAction<T> action, T t)
|
||||
{
|
||||
action?.Invoke(t);
|
||||
}
|
||||
|
||||
//执行事件
|
||||
public void DoAction<T, U>(UnityAction<T, U> action, T t, U u)
|
||||
{
|
||||
action?.Invoke(t, u);
|
||||
}
|
||||
|
||||
//执行事件
|
||||
public void DoAction<T, U, V>(UnityAction<T, U, V> action, T t, U u, V v)
|
||||
{
|
||||
action?.Invoke(t, u, v);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//delay秒后,执行事件
|
||||
public void DoActionDelay(UnityAction action, float delay)
|
||||
{
|
||||
StartCoroutine(DoActionDelayCor(action, delay));
|
||||
}
|
||||
|
||||
IEnumerator DoActionDelayCor(UnityAction action, float delay)
|
||||
{
|
||||
yield return new WaitForSeconds(delay);
|
||||
action?.Invoke();
|
||||
}
|
||||
|
||||
|
||||
//delay秒后,执行事件
|
||||
public void DoActionDelay<T>(UnityAction<T> action, float delay, T t)
|
||||
{
|
||||
StartCoroutine(DoActionDelayCor(action, delay, t));
|
||||
}
|
||||
|
||||
IEnumerator DoActionDelayCor<T>(UnityAction<T> action, float delay, T t)
|
||||
{
|
||||
yield return new WaitForSeconds(delay);
|
||||
action?.Invoke(t);
|
||||
}
|
||||
|
||||
|
||||
//delay秒后,执行事件
|
||||
public void DoActionDelay<T, U>(UnityAction<T, U> action, float delay, T t, U u)
|
||||
{
|
||||
StartCoroutine(DoActionDelayCor(action, delay, t, u));
|
||||
}
|
||||
|
||||
IEnumerator DoActionDelayCor<T, U>(UnityAction<T, U> action, float delay, T t, U u)
|
||||
{
|
||||
yield return new WaitForSeconds(delay);
|
||||
action?.Invoke(t, u);
|
||||
}
|
||||
|
||||
|
||||
//delay秒后,执行事件
|
||||
public void DoActionDelay<T, U, V>(UnityAction<T, U, V> action, float delay, T t, U u, V v)
|
||||
{
|
||||
StartCoroutine(DoActionDelayCor(action, delay, t, u, v));
|
||||
}
|
||||
|
||||
IEnumerator DoActionDelayCor<T, U, V>(UnityAction<T, U, V> action, float delay, T t, U u, V v)
|
||||
{
|
||||
yield return new WaitForSeconds(delay);
|
||||
action?.Invoke(t, u, v);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
11
Assets/Scripts/Core/ActionCenter.cs.meta
Normal file
11
Assets/Scripts/Core/ActionCenter.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 676ee6ef05572ae488fa5f16644f4a70
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
446
Assets/Scripts/Core/EventListener.cs
Normal file
446
Assets/Scripts/Core/EventListener.cs
Normal file
@@ -0,0 +1,446 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.Events;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class EventListener
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 添加指针进入事件监听
|
||||
/// </summary>
|
||||
public static void AddPointerEnterListener(GameObject obj, UnityAction<PointerEventData> callback)
|
||||
{
|
||||
PointerEnterEvent trigger = obj.GetComponent<PointerEnterEvent>();
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger = obj.AddComponent<PointerEnterEvent>();
|
||||
}
|
||||
trigger.AddListener(callback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加指针离开事件监听
|
||||
/// </summary>
|
||||
public static void AddPointerExitListener(GameObject obj, UnityAction<PointerEventData> callback)
|
||||
{
|
||||
PointerExitEvent trigger = obj.GetComponent<PointerExitEvent>();
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger = obj.AddComponent<PointerExitEvent>();
|
||||
}
|
||||
trigger.AddListener(callback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加指针按下事件监听
|
||||
/// </summary>
|
||||
public static void AddPointerDownListener(GameObject obj, UnityAction<PointerEventData> callback)
|
||||
{
|
||||
PointerDownEvent trigger = obj.GetComponent<PointerDownEvent>();
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger = obj.AddComponent<PointerDownEvent>();
|
||||
}
|
||||
trigger.AddListener(callback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加指针抬起事件监听
|
||||
/// </summary>
|
||||
public static void AddPointerUpListener(GameObject obj, UnityAction<PointerEventData> callback)
|
||||
{
|
||||
PointerUpEvent trigger = obj.GetComponent<PointerUpEvent>();
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger = obj.AddComponent<PointerUpEvent>();
|
||||
}
|
||||
trigger.AddListener(callback);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加指针点击事件监听
|
||||
/// </summary>
|
||||
public static void AddPointerClickListener(GameObject obj, UnityAction<PointerEventData> callback)
|
||||
{
|
||||
PointerClickEvent trigger = obj.GetComponent<PointerClickEvent>();
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger = obj.AddComponent<PointerClickEvent>();
|
||||
}
|
||||
trigger.AddListener(callback);
|
||||
}
|
||||
|
||||
public static void AddPointerPressListener(GameObject obj, UnityAction<PointerEventData> callback)
|
||||
{
|
||||
PointerPressEvent trigger = obj.GetComponent<PointerPressEvent>();
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger = obj.AddComponent<PointerPressEvent>();
|
||||
}
|
||||
trigger.AddListener(callback);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加开始拖拽事件监听
|
||||
/// </summary>
|
||||
public static void AddBeginDragListener(GameObject obj, UnityAction<PointerEventData> callback)
|
||||
{
|
||||
BeginDragEvent trigger = obj.GetComponent<BeginDragEvent>();
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger = obj.AddComponent<BeginDragEvent>();
|
||||
}
|
||||
trigger.AddListener(callback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加拖拽事件监听
|
||||
/// </summary>
|
||||
public static void AddDragListener(GameObject obj, UnityAction<PointerEventData> callback)
|
||||
{
|
||||
DragEvent trigger = obj.GetComponent<DragEvent>();
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger = obj.AddComponent<DragEvent>();
|
||||
}
|
||||
trigger.AddListener(callback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加停止拖拽事件监听
|
||||
/// </summary>
|
||||
public static void AddEndDragListener(GameObject obj, UnityAction<PointerEventData> callback)
|
||||
{
|
||||
EndDragEvent trigger = obj.GetComponent<EndDragEvent>();
|
||||
if (trigger == null)
|
||||
{
|
||||
trigger = obj.AddComponent<EndDragEvent>();
|
||||
}
|
||||
trigger.AddListener(callback);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加鼠标按下事件监听
|
||||
/// </summary>
|
||||
public static void AddMouseDownListener(GameObject obj, UnityAction<GameObject> callback)
|
||||
{
|
||||
MousePointEvent tigger = obj.GetComponent<MousePointEvent>();
|
||||
if (tigger == null)
|
||||
{
|
||||
tigger = obj.AddComponent<MousePointEvent>();
|
||||
}
|
||||
tigger.AddMouseDownListener(callback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加鼠标抬起事件监听
|
||||
/// </summary>
|
||||
public static void AddMouseUpListener(GameObject obj, UnityAction<GameObject> callback)
|
||||
{
|
||||
MousePointEvent tigger = obj.GetComponent<MousePointEvent>();
|
||||
if (tigger == null)
|
||||
{
|
||||
tigger = obj.AddComponent<MousePointEvent>();
|
||||
}
|
||||
tigger.AddMouseUpListener(callback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加鼠标进入事件监听
|
||||
/// </summary>
|
||||
public static void AddMouseEnterListener(GameObject obj, UnityAction<GameObject> callback)
|
||||
{
|
||||
MousePointEvent tigger = obj.GetComponent<MousePointEvent>();
|
||||
if (tigger == null)
|
||||
{
|
||||
tigger = obj.AddComponent<MousePointEvent>();
|
||||
}
|
||||
tigger.AddMouseEnterListener(callback);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加鼠标离开事件监听
|
||||
/// </summary>
|
||||
public static void AddMouseExitListener(GameObject obj, UnityAction<GameObject> callback)
|
||||
{
|
||||
MousePointEvent tigger = obj.GetComponent<MousePointEvent>();
|
||||
if (tigger == null)
|
||||
{
|
||||
tigger = obj.AddComponent<MousePointEvent>();
|
||||
}
|
||||
tigger.AddMouseExitListener(callback);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//进入事件
|
||||
public class PointerEnterEvent : MonoBehaviour, IPointerEnterHandler
|
||||
{
|
||||
private UnityAction<PointerEventData> action;
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
if (action != null)
|
||||
{
|
||||
action.Invoke(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddListener(UnityAction<PointerEventData> callback)
|
||||
{
|
||||
action = callback;
|
||||
}
|
||||
}
|
||||
|
||||
//按下事件
|
||||
public class PointerDownEvent : MonoBehaviour, IPointerDownHandler
|
||||
{
|
||||
private UnityAction<PointerEventData> action;
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
if (action != null)
|
||||
{
|
||||
action.Invoke(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddListener(UnityAction<PointerEventData> callback)
|
||||
{
|
||||
action = callback;
|
||||
}
|
||||
}
|
||||
|
||||
//抬起事件
|
||||
public class PointerUpEvent : MonoBehaviour, IPointerUpHandler
|
||||
{
|
||||
private UnityAction<PointerEventData> action;
|
||||
|
||||
public void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
if (action != null)
|
||||
{
|
||||
action.Invoke(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddListener(UnityAction<PointerEventData> callback)
|
||||
{
|
||||
action = callback;
|
||||
}
|
||||
}
|
||||
|
||||
//离开事件
|
||||
public class PointerExitEvent : MonoBehaviour, IPointerExitHandler
|
||||
{
|
||||
private UnityAction<PointerEventData> action;
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
if (action != null)
|
||||
{
|
||||
action.Invoke(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddListener(UnityAction<PointerEventData> callback)
|
||||
{
|
||||
action = callback;
|
||||
}
|
||||
}
|
||||
|
||||
//点击事件
|
||||
public class PointerClickEvent : MonoBehaviour, IPointerClickHandler
|
||||
{
|
||||
private UnityAction<PointerEventData> action;
|
||||
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
if (action != null)
|
||||
{
|
||||
action.Invoke(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddListener(UnityAction<PointerEventData> callback)
|
||||
{
|
||||
action = callback;
|
||||
}
|
||||
}
|
||||
|
||||
public class PointerPressEvent : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
||||
{
|
||||
private bool isPress = false;
|
||||
private UnityAction<PointerEventData> action;
|
||||
private PointerEventData data;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (isPress && action != null)
|
||||
{
|
||||
action.Invoke(data);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
data = eventData;
|
||||
isPress = true;
|
||||
}
|
||||
|
||||
public void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
isPress = false;
|
||||
}
|
||||
|
||||
public void AddListener(UnityAction<PointerEventData> callback)
|
||||
{
|
||||
action = callback;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//开始拖拽事件
|
||||
public class BeginDragEvent : MonoBehaviour, IBeginDragHandler
|
||||
{
|
||||
private UnityAction<PointerEventData> action;
|
||||
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
if (action != null)
|
||||
{
|
||||
action.Invoke(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddListener(UnityAction<PointerEventData> callback)
|
||||
{
|
||||
action = callback;
|
||||
}
|
||||
}
|
||||
|
||||
//拖拽事件
|
||||
public class DragEvent : MonoBehaviour, IDragHandler
|
||||
{
|
||||
private UnityAction<PointerEventData> action;
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
if (action != null)
|
||||
{
|
||||
action.Invoke(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddListener(UnityAction<PointerEventData> callback)
|
||||
{
|
||||
action = callback;
|
||||
}
|
||||
}
|
||||
|
||||
//停止拖拽事件
|
||||
public class EndDragEvent : MonoBehaviour, IEndDragHandler
|
||||
{
|
||||
private UnityAction<PointerEventData> action;
|
||||
|
||||
public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
if (action != null)
|
||||
{
|
||||
action.Invoke(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddListener(UnityAction<PointerEventData> callback)
|
||||
{
|
||||
action = callback;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 鼠标操作事件
|
||||
/// </summary>
|
||||
public class MousePointEvent : MonoBehaviour
|
||||
{
|
||||
private UnityAction<GameObject> mouseDownAction;
|
||||
private UnityAction<GameObject> mouseUpAction;
|
||||
private UnityAction<GameObject> mouseEnterAction;
|
||||
private UnityAction<GameObject> mouseExitAction;
|
||||
|
||||
void OnMouseDown()
|
||||
{
|
||||
if (mouseDownAction != null)
|
||||
{
|
||||
mouseDownAction.Invoke(this.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
void OnMouseUp()
|
||||
{
|
||||
if (mouseUpAction != null)
|
||||
{
|
||||
mouseUpAction.Invoke(this.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
void OnMouseEnter()
|
||||
{
|
||||
if (mouseEnterAction != null)
|
||||
{
|
||||
mouseEnterAction.Invoke(this.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
void OnMouseExit()
|
||||
{
|
||||
if (mouseExitAction != null)
|
||||
{
|
||||
mouseExitAction.Invoke(this.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddMouseDownListener(UnityAction<GameObject> callback)
|
||||
{
|
||||
mouseDownAction = callback;
|
||||
}
|
||||
|
||||
public void AddMouseUpListener(UnityAction<GameObject> callback)
|
||||
{
|
||||
mouseUpAction = callback;
|
||||
}
|
||||
|
||||
public void AddMouseEnterListener(UnityAction<GameObject> callback)
|
||||
{
|
||||
mouseEnterAction = callback;
|
||||
}
|
||||
|
||||
public void AddMouseExitListener(UnityAction<GameObject> callback)
|
||||
{
|
||||
mouseExitAction = callback;
|
||||
}
|
||||
}
|
||||
|
||||
11
Assets/Scripts/Core/EventListener.cs.meta
Normal file
11
Assets/Scripts/Core/EventListener.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 273071f04b6f4b14ca18e0130f072bbc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
302
Assets/Scripts/Core/PoolManager.cs
Normal file
302
Assets/Scripts/Core/PoolManager.cs
Normal file
@@ -0,0 +1,302 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// 对象池管理类,日常使用一下两个方法即可
|
||||
/// 取对象Spawn(name/Prefab),回收对象Unspawn(name/Prefab)
|
||||
/// </summary>
|
||||
public class PoolManager : MonoBehaviour
|
||||
{
|
||||
private static readonly object lockObj = new object();
|
||||
|
||||
private static PoolManager _instance;
|
||||
|
||||
public static PoolManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
lock (lockObj)
|
||||
{
|
||||
PoolManager[] objs = FindObjectsOfType<PoolManager>();
|
||||
if (objs != null)
|
||||
{
|
||||
for (int i = 0; i < objs.Length; i++)
|
||||
{
|
||||
DestroyImmediate(objs[i].gameObject);
|
||||
}
|
||||
}
|
||||
GameObject go = new GameObject("PoolManager");
|
||||
DontDestroyOnLoad(go);
|
||||
_instance = go.AddComponent<PoolManager>();
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
//预制ID-对象池 集合
|
||||
private Dictionary<int, ObjectPool> poolDic = new Dictionary<int, ObjectPool>();
|
||||
//对象ID-对象池ID 集合
|
||||
private Dictionary<int, int> itemDic = new Dictionary<int, int>();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 取对象,通过对象实例
|
||||
/// </summary>
|
||||
public GameObject Spawn(GameObject prefab)
|
||||
{
|
||||
if (!poolDic.ContainsKey(prefab.GetInstanceID()))
|
||||
{
|
||||
CreateNewPool(prefab);
|
||||
}
|
||||
GameObject item = poolDic[prefab.GetInstanceID()].Spawn();
|
||||
if (!itemDic.ContainsKey(item.GetInstanceID()))
|
||||
{
|
||||
itemDic.Add(item.GetInstanceID(), prefab.GetInstanceID());
|
||||
}
|
||||
else
|
||||
{
|
||||
itemDic[item.GetInstanceID()] = prefab.GetInstanceID();
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 回收对象
|
||||
/// </summary>
|
||||
public void Unspawn(GameObject go)
|
||||
{
|
||||
if (itemDic.ContainsKey(go.GetInstanceID()))
|
||||
{
|
||||
int id = itemDic[go.GetInstanceID()];
|
||||
if (poolDic.ContainsKey(id))
|
||||
{
|
||||
poolDic[id].Unspawn(go);
|
||||
itemDic.Remove(go.GetInstanceID());
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("回收对象失败 : " + go.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 回收某个对象池中所有对象
|
||||
/// </summary>
|
||||
public void ClearPool(GameObject prefab)
|
||||
{
|
||||
int id = prefab.GetInstanceID();
|
||||
if (poolDic.ContainsKey(id))
|
||||
{
|
||||
poolDic[id].UnspawnAll();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("清空对象池失败 : " + prefab.name);
|
||||
}
|
||||
|
||||
List<int> list = new List<int>();
|
||||
foreach (int key in itemDic.Keys)
|
||||
{
|
||||
if (itemDic[key] == id)
|
||||
{
|
||||
list.Add(key);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
itemDic.Remove(list[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 回收所有对象池
|
||||
/// </summary>
|
||||
public void ClearAll()
|
||||
{
|
||||
foreach (ObjectPool pool in poolDic.Values)
|
||||
{
|
||||
pool.UnspawnAll();
|
||||
}
|
||||
itemDic.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个新的对象池
|
||||
/// </summary>
|
||||
public void CreateNewPool(GameObject prefab)
|
||||
{
|
||||
if (poolDic.ContainsKey(prefab.GetInstanceID()))
|
||||
{
|
||||
Debug.LogError("无法创建新的对象池,当前对象池已存在 : " + prefab.name);
|
||||
return;
|
||||
}
|
||||
ObjectPool pool = new ObjectPool(prefab);
|
||||
poolDic.Add(prefab.GetInstanceID(), pool);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个新的对象池,并初始化
|
||||
/// </summary>
|
||||
public void CreateNewPool(GameObject prefab, int initSize)
|
||||
{
|
||||
if (poolDic.ContainsKey(prefab.GetInstanceID()))
|
||||
{
|
||||
Debug.LogError("无法创建新的对象池,当前对象池已存在 : " + prefab.name);
|
||||
return;
|
||||
}
|
||||
ObjectPool pool = new ObjectPool(prefab);
|
||||
pool.InitPool(initSize);
|
||||
poolDic.Add(prefab.GetInstanceID(), pool);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个新的对象池,并初始化,同时设定对象池最大容量
|
||||
/// </summary>
|
||||
public void CreateNewPool(GameObject prefab, int initSize, int maxSize)
|
||||
{
|
||||
if (poolDic.ContainsKey(prefab.GetInstanceID()))
|
||||
{
|
||||
Debug.LogError("无法创建新的对象池,当前对象池已存在 : " + prefab.name);
|
||||
return;
|
||||
}
|
||||
ObjectPool pool = new ObjectPool(prefab, maxSize);
|
||||
pool.InitPool(initSize);
|
||||
poolDic.Add(prefab.GetInstanceID(), pool);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对象池-子池
|
||||
/// </summary>
|
||||
public class ObjectPool
|
||||
{
|
||||
//对象池元素
|
||||
public GameObject prefab;
|
||||
|
||||
//对象池最大容量,此值小于等于0时,对象池动态增加,不设上限
|
||||
private int maxSize;
|
||||
|
||||
//对象池名称
|
||||
public string Name
|
||||
{
|
||||
get { return prefab.name; }
|
||||
}
|
||||
|
||||
//对象池当前容量
|
||||
private int CurrentSize
|
||||
{
|
||||
get { return activeObjectList.Count + inactiveObjectList.Count; }
|
||||
}
|
||||
|
||||
private List<GameObject> activeObjectList = new List<GameObject>(); //已用对象列表
|
||||
private List<GameObject> inactiveObjectList = new List<GameObject>(); //待用对象列表
|
||||
|
||||
public ObjectPool(GameObject obj)
|
||||
{
|
||||
this.prefab = obj;
|
||||
maxSize = -1;
|
||||
}
|
||||
|
||||
public ObjectPool(GameObject obj, int maxCount)
|
||||
{
|
||||
this.prefab = obj;
|
||||
maxSize = maxCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化对象池
|
||||
/// </summary>
|
||||
public void InitPool(int initSize)
|
||||
{
|
||||
if (maxSize > 0 && initSize > maxSize)
|
||||
{
|
||||
Debug.LogError("对象池初始容量不能超过其最大容量!!");
|
||||
for (int i = 0; i < maxSize; i++)
|
||||
{
|
||||
GameObject go = GameObject.Instantiate(prefab);
|
||||
go.name = prefab.name;
|
||||
go.SetActive(false);
|
||||
inactiveObjectList.Add(go);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < initSize; i++)
|
||||
{
|
||||
GameObject go = GameObject.Instantiate(prefab);
|
||||
go.name = prefab.name;
|
||||
go.SetActive(false);
|
||||
inactiveObjectList.Add(go);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取对象
|
||||
/// </summary>
|
||||
public GameObject Spawn()
|
||||
{
|
||||
if (inactiveObjectList.Count == 0)
|
||||
{
|
||||
if (maxSize > 0 && CurrentSize >= maxSize)
|
||||
{
|
||||
Debug.LogError("警告:对象池 " + Name + " 当前容量已达到最大设定值 : " + maxSize);
|
||||
GameObject go = activeObjectList[0];
|
||||
return go;
|
||||
}
|
||||
else
|
||||
{
|
||||
GameObject go = GameObject.Instantiate(prefab);
|
||||
go.SetActive(true);
|
||||
activeObjectList.Add(go);
|
||||
return go;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GameObject go = inactiveObjectList[0];
|
||||
go.SetActive(true);
|
||||
inactiveObjectList.RemoveAt(0);
|
||||
activeObjectList.Add(go);
|
||||
return go;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 回收对象
|
||||
/// </summary>
|
||||
public void Unspawn(GameObject go)
|
||||
{
|
||||
if (activeObjectList.Contains(go))
|
||||
{
|
||||
go.SetActive(false);
|
||||
activeObjectList.Remove(go);
|
||||
inactiveObjectList.Add(go);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("回收对象异常,当前对象不在对象列表中!!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 回收池中所有对象
|
||||
/// </summary>
|
||||
public void UnspawnAll()
|
||||
{
|
||||
//回收修改
|
||||
int count = activeObjectList.Count;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (activeObjectList.Count >= 1)
|
||||
{
|
||||
Unspawn(activeObjectList[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Core/PoolManager.cs.meta
Normal file
11
Assets/Scripts/Core/PoolManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8908e1646c88df14a8dc814b707d228e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
309
Assets/Scripts/Core/WebService.cs
Normal file
309
Assets/Scripts/Core/WebService.cs
Normal file
@@ -0,0 +1,309 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.UI;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class WebService : MonoBehaviour
|
||||
{
|
||||
private static WebService _instance;
|
||||
public static WebService Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
string name = "WebService";
|
||||
GameObject obj = GameObject.Find(name);
|
||||
if (obj == null)
|
||||
{
|
||||
obj = new GameObject(name);
|
||||
_instance = obj.AddComponent<WebService>();
|
||||
}
|
||||
else
|
||||
{
|
||||
_instance = obj.GetComponent<WebService>();
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = obj.AddComponent<WebService>();
|
||||
}
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
//网络图片资源缓存
|
||||
private Dictionary<int, Texture2D> textureCache = new Dictionary<int, Texture2D>();
|
||||
//图片下载
|
||||
private Queue<LoadTextureTask> textureQueue = new Queue<LoadTextureTask>();
|
||||
//图片加载完成
|
||||
private bool isTextureQueueFinish = true;
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
DontDestroyOnLoad(this.gameObject);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//获取本地数据
|
||||
public void GetLocalData<T>(Action<T> callback, string code = null) where T : class
|
||||
{
|
||||
StartCoroutine(GetLocalDataCor(callback, code));
|
||||
}
|
||||
|
||||
IEnumerator GetLocalDataCor<T>(Action<T> callback, string code) where T : class
|
||||
{
|
||||
string name = "JsonData/" + typeof(T).Name;
|
||||
if (code != null)
|
||||
{
|
||||
name += "_" + code;
|
||||
}
|
||||
TextAsset textAsset = Resources.Load<TextAsset>(name);
|
||||
yield return new WaitForEndOfFrame();
|
||||
T t = null;
|
||||
if (textAsset != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
t = JsonUtility.FromJson<T>(textAsset.text);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError("Json反序列化异常, type = " + typeof(T) + ", Exception : " + e.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogErrorFormat("{0}.json is not found.", typeof(T).Name);
|
||||
}
|
||||
if (callback != null)
|
||||
{
|
||||
callback.Invoke(t);
|
||||
}
|
||||
}
|
||||
|
||||
public void GetText(string url, Action<string> callback)
|
||||
{
|
||||
StartCoroutine(GetTextCor(url, callback));
|
||||
}
|
||||
|
||||
IEnumerator GetTextCor(string url, Action<string> callback)
|
||||
{
|
||||
UnityWebRequest unityWeb = UnityWebRequest.Get(url);
|
||||
unityWeb.downloadHandler = new DownloadHandlerBuffer();
|
||||
unityWeb.SetRequestHeader("Authorization", "tenantCode=CZFHSK&accessToken=52060d8c71d0e4a2d97c1b900acc8b1fa3a6928d");
|
||||
|
||||
yield return unityWeb.SendWebRequest();
|
||||
if (unityWeb.result != UnityWebRequest.Result.ConnectionError && unityWeb.result != UnityWebRequest.Result.ProtocolError)
|
||||
{
|
||||
callback?.Invoke(unityWeb.downloadHandler.text);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Web请求失败, Error : " + unityWeb.error);
|
||||
}
|
||||
unityWeb.Dispose();
|
||||
}
|
||||
|
||||
//Get通信
|
||||
public void Get<T>(string url, Action<T> callback) where T : class
|
||||
{
|
||||
StartCoroutine(GetCor(url, callback));
|
||||
}
|
||||
|
||||
IEnumerator GetCor<T>(string url, Action<T> callback) where T : class
|
||||
{
|
||||
UnityWebRequest unityWeb = UnityWebRequest.Get(url);
|
||||
unityWeb.downloadHandler = new DownloadHandlerBuffer();
|
||||
unityWeb.SetRequestHeader("Authorization", "tenantCode=CZFHSK&accessToken=52060d8c71d0e4a2d97c1b900acc8b1fa3a6928d");
|
||||
|
||||
yield return unityWeb.SendWebRequest();
|
||||
T t = null;
|
||||
if (unityWeb.result != UnityWebRequest.Result.ConnectionError && unityWeb.result != UnityWebRequest.Result.ProtocolError)
|
||||
{
|
||||
try
|
||||
{
|
||||
t = JsonUtility.FromJson<T>(unityWeb.downloadHandler.text);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError("Json反序列化异常, type = " + typeof(T) + ", Exception : " + e.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Web请求失败, type = " + typeof(T) + ", Error : " + unityWeb.error);
|
||||
}
|
||||
if (callback != null)
|
||||
{
|
||||
callback.Invoke(t);
|
||||
}
|
||||
unityWeb.Dispose();
|
||||
}
|
||||
|
||||
//Post通信
|
||||
public void Post<T>(string url, object obj, Action<T> callback) where T : class
|
||||
{
|
||||
StartCoroutine(PostCor(url, obj, callback));
|
||||
}
|
||||
|
||||
IEnumerator PostCor<T>(string url, object obj, Action<T> callback) where T : class
|
||||
{
|
||||
UnityWebRequest unityWeb = new UnityWebRequest(url, "POST");
|
||||
if (obj != null)
|
||||
{
|
||||
string json = JsonUtility.ToJson(obj);
|
||||
byte[] data = System.Text.Encoding.UTF8.GetBytes(json);
|
||||
unityWeb.uploadHandler = new UploadHandlerRaw(data);
|
||||
}
|
||||
|
||||
unityWeb.downloadHandler = new DownloadHandlerBuffer();
|
||||
unityWeb.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
|
||||
yield return unityWeb.SendWebRequest();
|
||||
T t = null;
|
||||
if (unityWeb.result != UnityWebRequest.Result.ConnectionError && unityWeb.result != UnityWebRequest.Result.ProtocolError)
|
||||
{
|
||||
try
|
||||
{
|
||||
t = JsonUtility.FromJson<T>(unityWeb.downloadHandler.text);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError("Json反序列化异常, type = " + typeof(T) + ", Exception : " + e.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Web请求失败, type = " + typeof(T) + ", Error : " + unityWeb.error);
|
||||
}
|
||||
|
||||
if (callback != null)
|
||||
{
|
||||
callback.Invoke(t);
|
||||
}
|
||||
unityWeb.Dispose();
|
||||
}
|
||||
|
||||
//加载图片资源
|
||||
public void GetTexture(string url, RawImage image, bool isCache = true)
|
||||
{
|
||||
if (url.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int code = url.GetHashCode();
|
||||
//缓存数据
|
||||
if (textureCache.ContainsKey(code))
|
||||
{
|
||||
image.gameObject.SetActive(true);
|
||||
image.texture = textureCache[code];
|
||||
return;
|
||||
}
|
||||
//加入队列
|
||||
LoadTextureTask task = new LoadTextureTask(url, image, isCache);
|
||||
textureQueue.Enqueue(task);
|
||||
//下载图片
|
||||
if (isTextureQueueFinish)
|
||||
{
|
||||
StartCoroutine(GetTextureCor());
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator GetTextureCor()
|
||||
{
|
||||
isTextureQueueFinish = false;
|
||||
LoadTextureTask task = textureQueue.Dequeue();
|
||||
UnityWebRequest unityWeb = UnityWebRequestTexture.GetTexture(task.url);
|
||||
yield return unityWeb.SendWebRequest();
|
||||
if (unityWeb.result != UnityWebRequest.Result.ConnectionError && unityWeb.result != UnityWebRequest.Result.ProtocolError)
|
||||
{
|
||||
Texture2D texture = ((DownloadHandlerTexture)unityWeb.downloadHandler).texture;
|
||||
//Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
|
||||
task.image.gameObject.SetActive(true);
|
||||
task.image.texture = texture;
|
||||
//缓存
|
||||
if (task.cache)
|
||||
{
|
||||
if (textureCache.ContainsKey(task.url.GetHashCode()))
|
||||
{
|
||||
textureCache[task.url.GetHashCode()] = texture;
|
||||
}
|
||||
else
|
||||
{
|
||||
textureCache.Add(task.url.GetHashCode(), texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("图片加载失败失败, url : " + task.url + " error : " + unityWeb.error);
|
||||
}
|
||||
unityWeb.Dispose();
|
||||
//递归
|
||||
if (textureQueue.Count > 0)
|
||||
{
|
||||
StartCoroutine(GetTextureCor());
|
||||
}
|
||||
else
|
||||
{
|
||||
isTextureQueueFinish = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//获取AssetBundle
|
||||
public void GetAssetBundle(string url, Action<AssetBundle> callback)
|
||||
{
|
||||
StartCoroutine(GetAssetBundleCor(url, callback));
|
||||
}
|
||||
|
||||
IEnumerator GetAssetBundleCor(string url, Action<AssetBundle> callback)
|
||||
{
|
||||
UnityWebRequest unityWeb = UnityWebRequestAssetBundle.GetAssetBundle(url);
|
||||
yield return unityWeb.SendWebRequest();
|
||||
AssetBundle bundle = null;
|
||||
if (unityWeb.result != UnityWebRequest.Result.ConnectionError && unityWeb.result != UnityWebRequest.Result.ProtocolError)
|
||||
{
|
||||
bundle = DownloadHandlerAssetBundle.GetContent(unityWeb);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Web请求失败, type = AssetBundle, Error : " + unityWeb.error);
|
||||
}
|
||||
if (callback != null)
|
||||
{
|
||||
callback.Invoke(bundle);
|
||||
}
|
||||
unityWeb.Dispose();
|
||||
}
|
||||
|
||||
//判断是否在WebGL平台运行
|
||||
public static bool IsRunningInWebGL()
|
||||
{
|
||||
return Application.platform == RuntimePlatform.WebGLPlayer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
//图片加载任务
|
||||
public class LoadTextureTask
|
||||
{
|
||||
public string url;
|
||||
public RawImage image;
|
||||
public bool cache;
|
||||
|
||||
public LoadTextureTask(string _url, RawImage _image, bool _cache)
|
||||
{
|
||||
url = _url;
|
||||
image = _image;
|
||||
cache = _cache;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Core/WebService.cs.meta
Normal file
11
Assets/Scripts/Core/WebService.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0e679bd9f5777b04bb36e9b3402a0f9e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user