Unity中关于游戏制作的代码架构书写

本代码架构一个由四篇代码组成,用于单机小游戏的开发
首先是Message脚本 用于消息的存放,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Message
 {
   //类型   
  public byte Type;  
    //命令  
  public int Command;    
       //参数 
  public object Content;
  public Message()    
   {   
   } 
  public Message(byte type,int command,object content)  
  {  
    Type = type;  
    Command = command;  
    Content = content; 
  }
  }
  //消息类型
public class MessageType
{
public static byte Type_Audio = 1;  
  public static byte Type_UI = 2;        
  public static int Audio_playSound = 100;    
  public static int Audio_PlayMusic = 101;    
  public static int Audio_StopMusic = 102;    
  public static int Audio_ChangeVolume = 103;
  public static int UI_ShowPanel = 200;    
  //增加分数    
  public static int UI_AddScore = 201;    
  public static int UI_ShowShop = 202;    
  }

然后是MonoBase脚本 用于用于控制组件发送消息,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonoBase : MonoBehaviour
{
 //发送消息    
 public void SendCustomMessage(Message msg)  
 {     
 MessageCenter.SendMessage(msg);  
 }   
 public void SendCustomMessage(byte type,int command,object content)   
{    
    MessageCenter.SendMessage(type ,command,content); 
}
   //接收消息
 public virtual void  ReceiveMessage(Message message)
  {  
  }
 }

然后是 消息中心,用于接收monobase传来的消息,并且向下发给管理层脚本,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MessageCenter
{
  //管理类集合   
   public static List<MonoBase> Managers = new List<MonoBase>();
      //发送消息   
  public static void SendMessage(Message msg) 
  {
    foreach (MonoBase mb in Managers)      
     {        
    mb.ReceiveMessage(msg);  
     }  
  }  
 public static void SendMessage(byte type,int command,object content)  
 {
  Message msg = new Message(type ,command,content);    
  SendMessage(msg);
  }
  }

然后再是管理层用于接收MassageCenter传下的消息和向monobase 发送消息,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ManagerBase<T> : MonoBase where T :MonoBase
{
public static T Instance;
//管理的消息接收者   
public List<MonoBase> ReceiveList = new List<MonoBase>();
//当前管理类接收的消息类型
protected byte messageType;
protected virtual void Awake()     
 {
Instance = this as T;        
   //设置消息类型        
messageType = SetMessageType();        
   //将当前的管理类添加到消息中心列表中       
MessageCenter.Managers.Add(this);
} 
//必须实现,返回当前管理类的类型
protected virtual byte SetMessageType()   
{
  return MessageType.Type_UI; 
}
//注册消息监听    
public void RegisterReceiver(MonoBase mb)     
{
if (!ReceiveList.Contains(mb))         
{            
ReceiveList.Add(mb);        
}    
}
 //接收到了消息,并向下分发消息
public override void ReceiveMessage(Message message)    
{
base.ReceiveMessage(message); 
//如果接收的消息类型不匹配,则不向下分发消息        
if (message.Type != messageType)
{
return;       
}        
foreach (MonoBase mb in ReceiveList)         
{           
 mb.ReceiveMessage(message);        
 }    
 }
 }

下面是对于代码的实际使用:
ScoreControl :这个脚本的大概意思就是 接收UIManager中发出的消息,然后判断是不是自己要接收的,如果是就获得message中Content的内容。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreControl :MonoBase{    
public Text text;  
 private int score = 0;
void Start()    {       
 //将当前类注册到UI管理类中,接收消息        
 UIManager.Instance.RegisterReceiver(this);    
 }    
 public override void ReceiveMessage(Message message)    
 {       
  base.ReceiveMessage(message);        
  if (message.Command== MessageType.UI_AddScore)         
  {           
   //添加分数           
    int add = (int)message.Content;            
    score += add;            
    text.text = score + "";       
     }    
     }
 }

然后就是上面提到的UIManager脚本了,这个脚本的大概作用就是接收MessageCenter发出的消息
给予判断是不是自己管辖的Monobase的,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIManager : ManagerBase<UIManager>
{
 //设定接收的消息类型    
  protected override byte SetMessageType() 
  {
   return MessageType.Type_UI;  
  }
  }

然后就是要触发的脚本,这里大概意思就是,当玩家碰到金币时,就发送个消息让相关UI来执行,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoinControl :MonoBase
{    
private void OnTriggerEnter(Collider other)    
{
if (other.tag =="Player")        
 {
  //加分
   SendCustomMessage(MessageType.Type_UI,MessageType.UI_AddScore,1);
    Destroy(gameObject );
 }
}   
}

最后配上一副结构图,来方便理解:

结构图
好了,就写到这了!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
制作Unity 2D拼图游戏的全过程大致如下: 1. 创建项目并导入资源 在Unity创建新项目,导入所需的图片资源。 2. 创建场景 在Unity创建一个新的场景,并将所需的图片资源拖放到场景。 3. 切割图片 使用Unity的Sprite Editor工具将图片切割成多个拼图块,确保它们的尺寸相同。 4. 创建拼图块 创建一个空对象,将切割后的拼图块添加为其子对象,为每个拼图块添加一个Box Collider 2D组件,并将其设置为触发器。 5. 创建拼图板 创建一个新的空对象,将其命名为“拼图板”,并将其设置在场景适当的位置。为拼图板添加一个Box Collider 2D组件,将其设置为触发器。 6. 编写拼图块拖动代码 编写脚本,以便用户可以拖动拼图块。使用OnMouseDrag()方法来检测鼠标拖动事件,并使用transform.position属性来移动拼图块。 7. 编写碰撞检测代码 编写脚本,以便检测当拼图块进入拼图板时的碰撞事件。使用OnTriggerEnter2D()方法来检测碰撞事件,并将拼图块的位置设置为拼图板上的位置。 8. 编写胜利条件检测代码 编写脚本,以便检测当所有拼图块都被正确放置时的胜利条件。使用一个计数器来跟踪正确放置的拼图块数量,并在所有拼图块都被正确放置时发出胜利消息。 以上是Unity 2D拼图游戏的基本流程,具体的代码实现可以根据自己的需求进行编写。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值