MVC 案例学习笔记
注册事件,就是把事件加入到事件字典
发送事件,并携带参数,就是在事件字典中遍历找到具体的 controller 进行处理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System;
[RequireComponent(typeof(ObjectPool))]
[RequireComponent(typeof(Sound))]
[RequireComponent(typeof(StaticData))]
public class Game : MonoSingleton<Game> {
//全局访问
[HideInInspector]
public ObjectPool objectPool;
[HideInInspector]
public Sound sound;
[HideInInspector]
public StaticData staticData;
private void Start()
{
//别删
DontDestroyOnLoad(gameObject);
objectPool = ObjectPool.Instance;
sound = Sound.Instance;
staticData = StaticData.Instance;
//初始化注册startUpcontroller
RegisterController(Consts.E_StartUp, typeof(StartUpController));
//游戏启动
SendEvent(Consts.E_StartUp);
//跳转场景
Game.Instance.LoadLevel(1);
}
public void LoadLevel(int level) {
//发送退出场景事件
ScenesArgs e = new ScenesArgs()
{
//获取当前场景索引
scnesIndex = SceneManager.GetActiveScene().buildIndex
};
SendEvent(Consts.E_ExitScenes,e);
//发送加载新的场景事件
SceneManager.LoadScene(level,LoadSceneMode.Single);
}
//进入新场景
private void OnLevelWasLoaded(int level)
{
Debug.Log("进入新场景:"+ level);
//发送进入场景事件
ScenesArgs e = new ScenesArgs()
{//获取当前场景索引
scnesIndex = level
};
SendEvent(Consts.E_EnterScenes, e);
}
//发送事件
void SendEvent(string eventName, object data = null)
{
MVC.SendEvent(eventName, data);
}
//注册controler
void RegisterController(string eventName, Type controllerType)
{
MVC.RegisterController(eventName, controllerType);
}
}
常量
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Consts
{
//事件名字
public const string E_ExitScenes = "E_ExitScenes";//ScenesArgs
public const string E_EnterScenes = "E_EnterScenes";//ScenesArgs
public const string E_StartUp = "E_StartUp";
public const string E_EndGame = "E_EndGame";
public const string E_PauseGame = "E_PauseGame";
public const string E_ResumeGame = "E_ResumeGame";
/// <summary>
/// ****UI相关
/// </summary>
public const string E_UpdataDis = "E_UpdataDis";//DistanceArgs
public const string E_UpdateCoin = "E_UpdateCoin";//CoinArgs
public const string E_HitAddTime = "E_HitAddTime";
public const string E_HitItem = "E_HitItem"; //ItemArgs
//ui可以射门了
public const string E_HitGoalTrigger = "E_HitGoalTrigger";
public const string E_ClickGoalButton = "E_ClickGoalButton";
//进球事件,让goal + 1
public const string E_ShootGoal = "E_ShootGoal";
//结算事件
public const string E_FinalShowUI = "E_FinalShowUI";
//贿赂
public const string E_BriberyClick = "E_BriberyClick";//CoinArgs
//resumen播放完成,继续游戏
public const string E_ContinueGame = "E_ContinueGame";
//买道具
public const string E_BuyTools = "E_BuyTools";
//买足球
public const string E_BuyFootBall = "E_BuyFootBall";
//装备足球
public const string E_EquipFootBall = "E_EquipFootBall";
//买衣服
public const string E_BuyCloth = "E_BuyCloth";
//装备衣服
public const string E_EquipCloth = "E_EquipCloth";
//model名字
public const string M_GameModel = "M_GameModel";
//view名字
public const string V_PlayerMove = "V_PlayerMove";
public const string V_PlayerAnim = "V_PlayerAnim";
public const string V_Board = "V_Board";
public const string V_Pause = "V_Pause";
public const string V_Resume = "V_Resume";
public const string V_Dead = "V_Dead";
public const string V_FinalScore = "V_FinalScore";
public const string V_BuyTools = "V_BuyTools";
public const string V_MainMenu = "V_MainMenu";
public const string V_Shop = "V_Shop";
//
}
public enum InputDirection {
NULL,
Right,
Left,
Down,
Up
}
//吃到奖励物品类型
public enum ItemKind
{
ItemMagnet,
ItemMultiply,
ItemInvincible
}
Tag
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Tag {
public const string road = "Road";
public const string player = "Player";
public const string bigFence = "BigFence";
public const string smallFence = "SmallFence";
public const string block = "Block";
public const string smallBlock = "SmallBlock";
public const string beforeTrigger = "BeforeTrigger";
public const string magnetCollider = "MagnetCollider";
//射门的trigger
public const string beforeGoalTrigger = "BeforeGoalTrigger";
//拖尾的名字
public const string ball = "Ball";
//守门员
public const string goalkeeper = "Goalkeeper";
//球门
public const string ballDoor = "BallDoor";
}
StaticData
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class StaticData : MonoSingleton<StaticData>
{
Dictionary<int, FootBallInfo> m_FootBallData = new Dictionary<int, FootBallInfo>();
public List<Material> FootBallMat;
Dictionary<int, Dictionary<int, FootBallInfo>> m_PlayerClothData = new Dictionary<int, Dictionary<int, FootBallInfo>>();
public List<Material> PlayerCloth;
//初始化足球信息
void InitFootBall()
{
m_FootBallData.Add(0, new FootBallInfo() { Coin = 0, Material = FootBallMat[0] });
m_FootBallData.Add(1, new FootBallInfo() { Coin = 200, Material = FootBallMat[1] });
m_FootBallData.Add(2, new FootBallInfo() { Coin = 400, Material = FootBallMat[2] });
}
void InitPlayerCloth()
{
int t = 0;
for(int i = 0;i <3; i++)
{
for(int j = 0;j< 3;j++)
{
if (!m_PlayerClothData.ContainsKey(i))
{
m_PlayerClothData.Add(i, new Dictionary<int, FootBallInfo>());
}
//print(t * 200);
m_PlayerClothData[i].Add(j, new FootBallInfo { Coin = t * 200, Material = PlayerCloth[t] });
t++;
}
}
}
protected override void Awake()
{
base.Awake();
InitFootBall();
InitPlayerCloth();
}
public FootBallInfo GetFootballInfo(int i )
{
return m_FootBallData[i];
}
public FootBallInfo GetPlayerInfo(int i,int j)
{
return m_PlayerClothData[i][j];
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FootBallInfo {
public Material Material;
public int Coin;
}
args
public class BuyClothArgs
{
public int coin;
public BuyID id;
}
public class BuyToolsArgs
{
public ItemKind itemKind;
public int coin;
}
public class BuyClothArgs
{
public int coin;
public BuyID id;
}
public class CoinArgs
{
public int coin;
}
public class DistanceArgs
{
public int distance;
}
public class ItemArgs
{
public ItemKind kind;
public int hitCount;
}
public class PauseArgs
{
public int coin;
public int score;
public int distance;
}
public class ScenesArgs {
public int scnesIndex;
}