首页 > 其他分享 >【Unity项目实践】野怪生成系统

【Unity项目实践】野怪生成系统

时间:2022-10-31 14:06:34浏览次数:46  
标签:map 野怪 生成 Instance Unity DataManager ID SpawnPoints

分为两个部分,一个是获取野怪生成点的数据,二是生成野怪。

获取野怪生成点的数据

(1)通过放置物体来定位我们想要让其生成的位置,并且附加上一个脚本用来在编辑模式下显示物体。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class SpawnPoint : MonoBehaviour
{
Mesh mesh = null;
public int ID;

// Start is called before the first frame update
void Start()
{
this.mesh = this.GetComponent<MeshFilter>().sharedMesh;
}

// Update is called once per frame
void Update()
{

}

#if UNITY_EDITOR
private void OnDrawGizmos()
{
Vector3 pos = this.transform.position + Vector3.up * this.transform.localScale.y * .5f;
Gizmos.color = Color.red;

if (this.mesh != null)
Gizmos.DrawWireMesh(this.mesh, pos, this.transform.rotation, this.transform.localScale);

UnityEditor.Handles.color = Color.red;
UnityEditor.Handles.ArrowHandleCap(0, pos, this.transform.rotation, 1f, EventType.Repaint);
UnityEditor.Handles.Label(pos, "SpawnPoint:" + this.ID);
}
#endif
}

(2)写一个编辑器扩展脚本用于生成数据

 [MenuItem("Map Tools/Export SpawnPoints")]
public static void ExportSpawnPoints()
{
DataManager.Instance.Load();

Scene current = EditorSceneManager.GetActiveScene();
string currentScene = current.name;

if (current.isDirty)
{
EditorUtility.DisplayDialog("提示", "请先保存当前场景", "确定");
return;
}

if (DataManager.Instance.SpawnPoints == null)
DataManager.Instance.SpawnPoints = new Dictionary<int, Dictionary<int, SpawnPointDefine>>();

foreach (var map in DataManager.Instance.Maps)
{
string sceneFile = "Assets/Levels/" + map.Value.Resource + ".unity";
if (!System.IO.File.Exists(sceneFile))
{
Debug.LogWarningFormat("Scene {0} not existed!", sceneFile);
continue;
}
EditorSceneManager.OpenScene(sceneFile, OpenSceneMode.Single);

SpawnPoint[] spawnPoints = GameObject.FindObjectsOfType<SpawnPoint>();

if (!DataManager.Instance.SpawnPoints.ContainsKey(map.Value.ID))
{
DataManager.Instance.SpawnPoints[map.Value.ID] = new Dictionary<int, SpawnPointDefine>();
}

foreach (var sp in spawnPoints)
{
if (!DataManager.Instance.SpawnPoints[map.Value.ID].ContainsKey(sp.ID))
{
DataManager.Instance.SpawnPoints[map.Value.ID][sp.ID] = new SpawnPointDefine();
}

SpawnPointDefine def = DataManager.Instance.SpawnPoints[map.Value.ID][sp.ID];
def.ID = sp.ID;
def.MapID = map.Value.ID;
def.Position = GameObjectTool.WorldToLogicN(sp.transform.position);
def.Direction = GameObjectTool.WorldToLogicN(sp.transform.forward);
}
}

DataManager.Instance.SaveSpawnPoints();
EditorSceneManager.OpenScene("Assets/Levels/" + currentScene + ".unity");
EditorUtility.DisplayDialog("提示", "刷怪点导出完成", "确定");

}

然后点击运行即可生成。

【Unity项目实践】野怪生成系统_服务端

文件如下:

【Unity项目实践】野怪生成系统_野怪系统_02



生成野怪

野怪是生成在地图上的,并且对于不同的玩家,野怪都是一样的,所以野怪应该卸载地图加载的逻辑中,并且是在服务端定时生成的,和客户端解耦。

服务端总体的野怪生成逻辑如下图所示:

【Unity项目实践】野怪生成系统_数据_03

野怪生成的时候服务端的Log如下:

【Unity项目实践】野怪生成系统_服务端_04

标签:map,野怪,生成,Instance,Unity,DataManager,ID,SpawnPoints
From: https://blog.51cto.com/u_15639010/5809499

相关文章

  • Java-pdf模板制作流程-使用pdf 模板生成pdf文件
    Java使用pdf模板生成pdf文件——制作流程1.      使用工具adobeacrobatdc、word20152.      使用word绘制一个3*5的表格并保存,如下:3.     ......
  • 【备忘录】idea生成jar包,并编辑META-INF/MANIFEST.MF文件(兼容问题测试中)
    生成jar包步骤:        配置MAINIFEST.MF文件:(多个jar包之间用空格分隔,为兼容windows路径和unix路径需要写两份,\\和/)      生成jar包:  ......
  • java tomcat按天生成日志
    tomcat按天生成日志,亲测可行~1.安装cronolog安装方式一:shell>sudoyuminstallcronolog或shell>sudoapt-getinstallcronolog验证cronolog:shell>whichcronol......
  • js常用方法和一些封装(2) -- 随机数生成
    任何编程语言,随机数都是必不可少的,我在开发过程中,尤其在自己做一些小玩意的时候,就经常使用随机数,后来发现每次使用都懒得写,直接去网上搜一个,拿过来就用了。可是时间一长,发现......
  • JS快速生成多维数组
    newArray的使用如果newArray传入的参数只有一个Nuber类型的话,那么则默认初始化为指定长度的空数组如果传入多个元素,才会生成数组fill将数组按照指定元素进行填充map......
  • JavaDoc生成自己的API文档
    JavaDoc的注释参数@author作者名@version版本号@since需要的最早java版本@param 函数参数@return返回值@throws异常抛出情况 生成JavaDoc步骤如下代码......
  • 两个无限项求和问题(生成函数)
    对\(k=0\simK\)求\(\sum\limits_{i=0}^{\infty}p^i\dbinom{i}{k}\)。\[\begin{aligned}&\sum_{i=0}^{\infty}p^i\binom{i}{k}\\=&\sum_{i=0}^{\infty}p^i[x^k](1+x)......
  • 历史遗留の生成函数
    求\(\sum_{i=0}^{\infty}\binom{i}{K}p^i\),其中\(\binom{i}{K}=C_i^K\),\(1\leqK\leq10^9\),\(0<p<1\)。设\(A(x)\)为一多项式,\([x^k]A(x)\)表示这个多项式\(k\)次......
  • pdf 工具类 生成pdf 表单填充 表单合并 pdf转图片 pdf水印
    包<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency><dependency><groupId>commons-co......
  • 基于STK生成双行根数
    基于STK生成双行根数https://zhuanlan.zhihu.com/p/262933917 1、双行根数简介美国是空间监测网最完善的国家,其SSN监测系统通过全球16个地方的31台雷达或望远镜可以跟......