任务:编写底层逻辑,增加一些有用的工具类
1.新增单例模式单例类模板,可以广泛用在同时间只有一个存在在场景中的各种管理器
2.新增事件框架
3.新增状态模式的状态类和状态管理员类的类模板
4.新增进入式触发器
5.新增互动式触发器(涉及玩家类的更改,暂缓)
*.以上均为底层代码,目前无法测试,如果发现问题请联系开发者,如无必要,请勿修改
我是每天上班提醒小助手,今天你上班了吗?😺
This commit is contained in:
parent
d5c382fbca
commit
876778ccb0
@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 81dbcde0f90df4e9ba9ca2794490e57a
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fd02c799f3f5c4c83b2fc26c105a3821
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f597f19f656ba56eae4f6a3a7cc528f4
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 48e08dc33330d11e9d4a1b246c52e4f6
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed09910c0094cb27be8f3ca264680da3
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc355dd4cf1e6173beaeb22c2858cbe1
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
11
Assets/Scripts/乐/Player.cs.meta
Normal file
11
Assets/Scripts/乐/Player.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9628f347b6b3d014e8f23504f7bca533
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
31
Assets/Scripts/勍/EntryTrigger.cs
Normal file
31
Assets/Scripts/勍/EntryTrigger.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 进入式触发器的基类,继承自事件,每一个进入式触发器都必须继承这个类
|
||||
/// </summary>
|
||||
public class EntryTrigger : Event
|
||||
{
|
||||
private Collider2D m_collider;
|
||||
|
||||
void Start()
|
||||
{
|
||||
//如果没有挂载碰撞盒,则报错
|
||||
if(!TryGetComponent<Collider2D>(out m_collider))
|
||||
{
|
||||
Debug.LogError(this.GetType() + gameObject.name + "没有挂载碰撞盒,请检查!");
|
||||
}
|
||||
//如果不是触发器,则报错
|
||||
if(!m_collider.isTrigger)
|
||||
{
|
||||
Debug.LogError(this.GetType() + gameObject.name + "碰撞盒没有设置为触发器,请检查!");
|
||||
}
|
||||
}
|
||||
|
||||
virtual public void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
//如果玩家进入碰撞盒则触发事件
|
||||
if(other.TryGetComponent<Player>(out Player player)) OnCall();
|
||||
}
|
||||
}
|
11
Assets/Scripts/勍/EntryTrigger.cs.meta
Normal file
11
Assets/Scripts/勍/EntryTrigger.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a08702285d1216941b48b6d42e06bf82
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
24
Assets/Scripts/勍/Event.cs
Normal file
24
Assets/Scripts/勍/Event.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 事件基类,事件需要继承这个类
|
||||
/// </summary>
|
||||
public class Event : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件触发
|
||||
/// </summary>
|
||||
public virtual void OnCall(){}
|
||||
|
||||
/// <summary>
|
||||
/// 要手动终止事件的时候调用
|
||||
/// </summary>
|
||||
public virtual void CancleEvent(){ OnCancle(); }
|
||||
|
||||
/// <summary>
|
||||
/// 事件结束时触发
|
||||
/// </summary>
|
||||
protected virtual void OnCancle(){}
|
||||
}
|
11
Assets/Scripts/勍/Event.cs.meta
Normal file
11
Assets/Scripts/勍/Event.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 17fed7225ff8045449129f8106e9d559
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
23
Assets/Scripts/勍/StateBase.cs
Normal file
23
Assets/Scripts/勍/StateBase.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 状态模式中,单个状态的父类,所有的状态都需要继承自这个类。注意,这里是三级继承关系,这个是最底层的类,再往上是某个具
|
||||
/// 体状态体系的父类,继承于这个类,最上层才是要实现功能的具体状态类,继承自中间层,只需要完善虚函数即可
|
||||
/// </summary>
|
||||
public class StateBase : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// 进入状态时调用
|
||||
/// </summary>
|
||||
public virtual void Enter(){}
|
||||
/// <summary>
|
||||
/// 状态结束时调用
|
||||
/// </summary>
|
||||
public virtual void End(){}
|
||||
/// <summary>
|
||||
/// 状态中每一帧调用
|
||||
/// </summary>
|
||||
public virtual void StateUpdate(){}
|
||||
}
|
11
Assets/Scripts/勍/StateBase.cs.meta
Normal file
11
Assets/Scripts/勍/StateBase.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2aae34169cb209342bdfae44b74a6761
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
41
Assets/Scripts/勍/StateMachineBase.cs
Normal file
41
Assets/Scripts/勍/StateMachineBase.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 状态模式的状态管理员基类,每一组状态的管理员都必须继承这个类
|
||||
/// </summary>
|
||||
public class StateMachineBase : MonoBehaviour
|
||||
{
|
||||
protected StateBase currentState;
|
||||
protected bool isBegin = false;
|
||||
/// <summary>
|
||||
/// 改变状态的时候调用
|
||||
/// </summary>
|
||||
/// <param name="newState"></param>
|
||||
public void ChangeState(StateBase newState)
|
||||
{
|
||||
isBegin = false;
|
||||
if (currentState != null)
|
||||
{
|
||||
currentState.End();
|
||||
}
|
||||
currentState = newState;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 状态的每一帧调用,需要手动安排到主程序的Update中,切记!
|
||||
/// </summary>
|
||||
public void StateUpdate()
|
||||
{
|
||||
if (currentState != null)
|
||||
{
|
||||
if (!isBegin)
|
||||
{
|
||||
currentState.Enter();
|
||||
isBegin = true;
|
||||
}
|
||||
currentState.StateUpdate();
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/勍/StateMachineBase.cs.meta
Normal file
11
Assets/Scripts/勍/StateMachineBase.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 570a7bc236b60d84195606d29616a6e6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
40
Assets/Scripts/勍/UnitySingleton.cs
Normal file
40
Assets/Scripts/勍/UnitySingleton.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 单例模式类模板
|
||||
/// </summary>
|
||||
public class UnitySingleton<T> : MonoBehaviour
|
||||
where T : Component
|
||||
{
|
||||
private static T m_instance;
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_instance == null)
|
||||
{
|
||||
m_instance = FindObjectOfType<T>();
|
||||
if (m_instance == null)
|
||||
{
|
||||
Debug.LogError("缺少 " + typeof(T) + " 这个单例,没法在场景中找到");
|
||||
}
|
||||
}
|
||||
return m_instance;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
if (m_instance == null)
|
||||
{
|
||||
m_instance = this as T;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/勍/UnitySingleton.cs.meta
Normal file
11
Assets/Scripts/勍/UnitySingleton.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8d3368bff7857324580bba4842015865
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -15,9 +15,6 @@ EditorUserSettings:
|
||||
value: 224247031146466b011b0b2b1e301034131a112d25292824620d3207f5e53136d2f539a9c2223e31290eea2f4b1a2e0be50f0c05d7050306101af4011fc0321202cc1bd654dd1115df00
|
||||
flags: 0
|
||||
RecentlyUsedScenePath-3:
|
||||
value: 22424703114646680e0b0227036cdafbfb5831243c3d3204283a097df7ee3d2cfb
|
||||
flags: 0
|
||||
RecentlyUsedScenePath-4:
|
||||
value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d
|
||||
flags: 0
|
||||
RecentlyUsedScenePath-4:
|
||||
|
Loading…
x
Reference in New Issue
Block a user