事件中心
This commit is contained in:
parent
727aaa84c7
commit
ae2237a011
@ -321,9 +321,189 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 93b431bb64dc18f4791c8adfd597c6ae, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!1 &999936484
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 999936486}
|
||||
- component: {fileID: 999936485}
|
||||
m_Layer: 0
|
||||
m_Name: Player
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!114 &999936485
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 999936484}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 34dba149204a3d34597e85785e9709a9, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!4 &999936486
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 999936484}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &1028328147
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 1028328149}
|
||||
- component: {fileID: 1028328148}
|
||||
m_Layer: 0
|
||||
m_Name: Other
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!114 &1028328148
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1028328147}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 00a39968241898f4e9123820fef98031, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!4 &1028328149
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1028328147}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &1652135856
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 1652135858}
|
||||
- component: {fileID: 1652135857}
|
||||
m_Layer: 0
|
||||
m_Name: Monster
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!114 &1652135857
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1652135856}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 5a2da7d3dbd5b5146b338dcc542e4b57, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!4 &1652135858
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1652135856}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &2134814743
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 2134814745}
|
||||
- component: {fileID: 2134814744}
|
||||
m_Layer: 0
|
||||
m_Name: Task
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!114 &2134814744
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2134814743}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 791aa05507e50f342b4607dafcd48cee, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!4 &2134814745
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 2134814743}
|
||||
serializedVersion: 2
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_ConstrainProportionsScale: 0
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1660057539 &9223372036854775807
|
||||
SceneRoots:
|
||||
m_ObjectHideFlags: 0
|
||||
m_Roots:
|
||||
- {fileID: 963194228}
|
||||
- {fileID: 705507995}
|
||||
- {fileID: 999936486}
|
||||
- {fileID: 1652135858}
|
||||
- {fileID: 1028328149}
|
||||
- {fileID: 2134814745}
|
||||
|
8
Assets/Scripts/ProjectBase/Event.meta
Normal file
8
Assets/Scripts/ProjectBase/Event.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98d05e51aa6aded4a981cffec9cde225
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
70
Assets/Scripts/ProjectBase/Event/EventCenter.cs
Normal file
70
Assets/Scripts/ProjectBase/Event/EventCenter.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.Windows;
|
||||
|
||||
/// <summary>
|
||||
/// 事件中心
|
||||
/// 1.字典 2.委托 3.观察者设计模式
|
||||
/// </summary>
|
||||
public class EventCenter : BaseManager<EventCenter>
|
||||
{
|
||||
//key ---事件名字(比如:怪物死亡,玩家死亡,通关 等等)
|
||||
//value --- 对应 监听这个事件 对应的委托函数
|
||||
private Dictionary<string , UnityAction<object>> eventDic = new Dictionary<string , UnityAction<object>>();
|
||||
|
||||
/// <summary>
|
||||
/// 添加事件监听
|
||||
/// </summary>
|
||||
/// <param name="name">事件的名字</param>
|
||||
/// <param name="action">准备用来处理时间的委托函数</param>
|
||||
public void AddEventListener(string name,UnityAction<object> action)
|
||||
{
|
||||
//有没有对应的事件监听
|
||||
//有的情况
|
||||
if(eventDic.ContainsKey(name))
|
||||
{
|
||||
eventDic[name] += action;//已经有事件就往后排
|
||||
}
|
||||
//没有的情况
|
||||
else
|
||||
{
|
||||
eventDic.Add(name, action);//没有就加一个
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 减去事件监听
|
||||
/// 事件有加就有减
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="action"></param>
|
||||
public void RemoveEventListener(string name, UnityAction<object> action)
|
||||
{
|
||||
if(eventDic.ContainsKey(name))
|
||||
{
|
||||
eventDic[name] -= action;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 事件触发
|
||||
/// </summary>
|
||||
/// <param name="name">哪一个名字的事件触发了</param>
|
||||
public void EvennTrigger(string name,object info)
|
||||
{
|
||||
if(eventDic.ContainsKey(name))
|
||||
{
|
||||
eventDic[name].Invoke(info);//有就触发事件
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空事件中心,主要用在场景切换时
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
eventDic.Clear();
|
||||
}
|
||||
}
|
11
Assets/Scripts/ProjectBase/Event/EventCenter.cs.meta
Normal file
11
Assets/Scripts/ProjectBase/Event/EventCenter.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b05d78ac414aebc4b81bd00463599a85
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
27
Assets/Scripts/ProjectBase/Event/Monster.cs
Normal file
27
Assets/Scripts/ProjectBase/Event/Monster.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Monster : MonoBehaviour
|
||||
{
|
||||
public int type = 1;
|
||||
public string name1 = "123123";
|
||||
void Start()
|
||||
{
|
||||
// Dead();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if(Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Dead();
|
||||
}
|
||||
}
|
||||
void Dead()
|
||||
{
|
||||
Debug.Log("怪物死亡");
|
||||
//触发事件
|
||||
EventCenter.GetInstance().EvennTrigger("MonsterDead",this);
|
||||
}
|
||||
}
|
11
Assets/Scripts/ProjectBase/Event/Monster.cs.meta
Normal file
11
Assets/Scripts/ProjectBase/Event/Monster.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5a2da7d3dbd5b5146b338dcc542e4b57
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
16
Assets/Scripts/ProjectBase/Event/Other.cs
Normal file
16
Assets/Scripts/ProjectBase/Event/Other.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Other : MonoBehaviour
|
||||
{
|
||||
void Start()
|
||||
{
|
||||
EventCenter.GetInstance().AddEventListener("MonsterDead", OtherWaitMonsterDeadDo);
|
||||
}
|
||||
|
||||
public void OtherWaitMonsterDeadDo(object info)
|
||||
{
|
||||
Debug.Log("其他各个对象要做的事");
|
||||
}
|
||||
}
|
11
Assets/Scripts/ProjectBase/Event/Other.cs.meta
Normal file
11
Assets/Scripts/ProjectBase/Event/Other.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 00a39968241898f4e9123820fef98031
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
17
Assets/Scripts/ProjectBase/Event/Player.cs
Normal file
17
Assets/Scripts/ProjectBase/Event/Player.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
|
||||
void Start()
|
||||
{
|
||||
EventCenter.GetInstance().AddEventListener("MonsterDead",MonsterDeadDo);
|
||||
}
|
||||
|
||||
public void MonsterDeadDo(object info)
|
||||
{
|
||||
Debug.Log("鯤소삿돤쉽쟨" +(info as Monster).name1);
|
||||
}
|
||||
}
|
11
Assets/Scripts/ProjectBase/Event/Player.cs.meta
Normal file
11
Assets/Scripts/ProjectBase/Event/Player.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34dba149204a3d34597e85785e9709a9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
17
Assets/Scripts/ProjectBase/Event/Task.cs
Normal file
17
Assets/Scripts/ProjectBase/Event/Task.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Task : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
EventCenter.GetInstance().AddEventListener("MonsterDead", TaskWaitMonsterDeadDo);
|
||||
}
|
||||
|
||||
public void TaskWaitMonsterDeadDo(object info)
|
||||
{
|
||||
Debug.Log("ÈÎÎñ¼Ç¼");
|
||||
}
|
||||
}
|
11
Assets/Scripts/ProjectBase/Event/Task.cs.meta
Normal file
11
Assets/Scripts/ProjectBase/Event/Task.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 791aa05507e50f342b4607dafcd48cee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
x
Reference in New Issue
Block a user