Unity 5.3.5f1 (32-bit) 的简单塔防游戏
我以前使用过unity但是第一次写这么全面的塔防小游戏。我以后会陆续的将我跟过的一些项目的心得经验与体会发表出来希望各位能人能够给出评价,我在此感激各位的批评与赞扬。另外我只是一个学生学艺不精,粗制滥造还请看不过去的大神放过................0.0................................
首先是路径的管理设置
using UnityEngine;
using System.Collections; namespace RayGame{ public class PathManager : MonoBehaviour { //路点数组
public GameObject[] Path1;
public GameObject[] Path2; /// <summary>
/// 返回路点
/// </summary>
/// <returns>The node.</returns>
/// <param name="pathId">路径索引.</param>
/// <param name="nodeIndex">路点索引</param>
public GameObject getNode(int pathId,int nodeIndex){
if(pathId == ){
//超出路径数组范围,返回 Null
if (nodeIndex >= Path1.Length) {
return null;
} else {
//返回相应路点游戏对象
return Path1 [nodeIndex];
}
}else{
if (nodeIndex >= Path2.Length) {
return null;
} else {
return Path2 [nodeIndex];
}
}
}
} }
然后是游戏玩家管理以及设置
// Authors:Liu Yong
// Email: <raygenes@gmail.com>
// QQ:1931195500 using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
namespace RayGame{ public class GameManager : MonoBehaviour {
Animator canvasAnim;
//最大生命值
public int totalLife = 20;
//最大金钱值
public int totalGold = 200;
//当前生命值
int curLife = 0;
//当前金币
int curGold = 0; UIManager uiMgr; public int[] towerPrices;
void Awake(){
uiMgr = GameObject.Find ("UIManager").GetComponent<UIManager> ();
canvasAnim = uiMgr.GetComponent<Animator> ();
} void Start(){
DataInit ();
UIInit ();
} //数据初始化
void DataInit(){
curLife = totalLife;
curGold = totalGold;
} //UI界面初始化
void UIInit(){
uiMgr.SetHeartValue (curLife);
uiMgr.SetGoldValue (curGold);
} //添加金钱
public void addGold(int num){
curGold += num;
uiMgr.SetGoldValue (curGold);
} public void useGold(int num){
curGold -= num;
uiMgr.SetGoldValue (curGold);
} //增加生命值
public void addLife(int num){
curLife += num;
uiMgr.SetHeartValue (curLife);
} public void useLife(int num){
curLife -= num;
uiMgr.SetHeartValue (curLife);
} public bool HasEnoughLife(int num){
return curLife - num >= 0;
} public bool HasEnoughGold(int num){
return curGold - num >= 0;
}
public void ShowButton(){
//触发动画切换
canvasAnim.SetTrigger ("ShowStartButton");
} public void EnterGame(){
SceneManager.LoadScene ("Game2");
} } }
怪物的管理及设置
// Authors:Liu Yong
// Email: <raygenes@gmail.com>
// QQ:1931195500 using UnityEngine;
using System.Collections;
using System.Threading;
namespace RayGame{ public class MonsterManager : MonoBehaviour { //怪物资源路径
string monPath = "Prefabs/Monsters/Mon1";
//刷怪点
public Transform spawnPoint;
//刷新时间
//public float spawnInterval =3f; void Start()
{
for (int i=; i<= ; i++)
{
Spawn();
}
InvokeRepeating ("Start2",,);
} void Start2(){
for (int i=; i<= ; i++)
{
Spawn();
} }
void Spawn(){ //随机数生成,半闭半开区间
//加载ra物资源,并实例化游戏对象
GameObject mon = (GameObject)Instantiate (Resources.Load (monPath), spawnPoint.position,
Quaternion.identity);
//关联怪物移动组件
mon.AddComponent<MonsterMove> ();
mon.AddComponent<MonsterHealth> ();
mon.AddComponent<Rigidbody2D> ();
BoxCollider2D collider = mon.AddComponent<BoxCollider2D> ();
collider.isTrigger = true;
}
} }
攻击塔的管理及设置
// Authors:Liu Yong
// Email: <raygenes@gmail.com>
// QQ:1931195500 using UnityEngine;
using System.Collections; namespace RayGame{ public class TowerManager : MonoBehaviour { //造塔点
public GameObject[] TowerPoints; //建造过程中的图片
public Sprite[] TowerBuilding; //血条资源路径
string barPath = "Prefabs/UI/ProgressBar"; //造塔时间
public float buildingTime = 1f; //当前的造塔点
GameObject curTowerPoint = null;
//当前塔的类型
int curTowerId; void Awake(){
//遍历 所有造塔点
for (int i = 0; i < TowerPoints.Length; i++) {
GameObject tp = TowerPoints [i];
//关联碰撞盒
tp.AddComponent<CircleCollider2D> ();
//关联脚本
tp.AddComponent<TowerPoint> ();
}
} public void ShowBuilding(GameObject _towerPoint,int towerId){
//记住造塔点
curTowerPoint = _towerPoint;
//记住造塔类型
curTowerId = towerId;
//获取渲染组件
SpriteRenderer spRender = _towerPoint.GetComponent<SpriteRenderer> ();
//更改精灵图片
spRender.sprite = TowerBuilding [towerId - 1];
//显示建造过程
ShowProgress (_towerPoint);
} void ShowProgress(GameObject towerPoint){
GameObject barObj = (GameObject)Instantiate (Resources.Load (barPath),
Vector3.zero,Quaternion.identity);
//ProgressBar组件
ProgressBar bar = barObj.GetComponent<ProgressBar> ();
//挂载血条
bar.SetBarParent (towerPoint.transform);
//激活进度条
bar.SetActive (true,buildingTime,gameObject);
} public void ProgressEnd(){
Debug.Log ("进度条结束");
BuildTower (curTowerPoint,curTowerId,1);
} /// <summary>
/// 造塔
/// </summary>
/// <param name="towerPoint">造塔点</param>
/// <param name="type">塔类型</param>
/// <param name="level">塔的等级</param>
public void BuildTower(GameObject towerPoint,int type,int level){
//按照规则拼接路径
string towerPath = "Prefabs/Towers/Tower"+type+"/Tower"+type+"_"+level;
Debug.Log ("show path "+towerPath);
//实例化塔对象
GameObject tower = (GameObject)Instantiate (Resources.Load (towerPath),
towerPoint.transform.position, Quaternion.identity);
Tower tw = tower.AddComponent<Tower> ();
tw.SetTowerType (type);
tw.TowerInit (); //删除造塔点
Destroy (towerPoint);
} } }
UImanger的管理及设置
// Authors:Liu Yong
// Email: <raygenes@gmail.com>
// QQ:1931195500 using UnityEngine;
using System.Collections;
using UnityEngine.UI; namespace RayGame{ public class UIManager : MonoBehaviour { string panelPath = "Prefabs/UI/TowerPanel"; //指向当前打开的面板
GameObject curPanel = null; //指向点击的当前的造塔点
public GameObject curTowerPoint = null; public Text heartValue;
public Text goldValue;
public Text waveValue; /// <summary>
/// 在指定位置显示面板
/// </summary>
/// <param name="pos">指定的位置</param>
public void ShowTowerPanel(Vector3 pos,GameObject towerPoint){
if(curPanel != null){
Destroy (curPanel);
}
curPanel = (GameObject)Instantiate (Resources.Load (panelPath),
pos,Quaternion.identity); //记录当前点击的造塔点
curTowerPoint = towerPoint;
} public void CloseTowerPanel(){
Destroy (curPanel);
} public void SetHeartValue(int val){
heartValue.text = val.ToString ();
} public void SetGoldValue(int val){
goldValue.text = val.ToString ();
} public void SetWaveValue(int val){
waveValue.text = val.ToString ();
}
} }
另外其实还需要大量的动态图片,路径点设置,路径选择。各种绑定的工作没有办法在这上面演示。想要完整的文件私下qq邮箱我1170826169@qq.com
以上地图所具备的组件已经都完成了。接下来就是塔防,买塔,怪物移动,怪物受到攻击以及打死怪赚钱等等行为的代码
Unity 5.3.5f1 (32-bit) 的简单塔防游戏的更多相关文章
- VS2013配合EgretVS开发简单塔防游戏
VS2013配合EgretVS开发简单塔防游戏(1) - 环境配置 VS2013配合EgretVS开发简单塔防游戏(2) – 原型设计 VS2013配合EgretVS开发简单塔防游戏(3) – 精灵动 ...
- Unity简单塔防游戏的开发——敌人移动路径的创建及移动
软件工程综合实践专题第一次作业 Unity呢是目前一款比较火热的三维.二维动画以及游戏的开发引擎,我也由于一些原因开始接触并喜爱上了这款开发引擎,下面呢是我在学习该引擎开发小项目时编写的一些代码的脚本 ...
- 使用Unity创建塔防游戏(Part2)
How to Create a Tower Defense Game in Unity – Part 2 原文地址:https://www.raywenderlich.com/107529/unity ...
- 使用Unity创建塔防游戏(Part1)
How to Create a Tower Defense Game in Unity - Part1 原文作者:Barbara Reichart 文章原译:http://www.cnblogs.co ...
- 使用unity创建塔防游戏(原译)(part1)
塔防游戏非常地受欢迎,木有什么能比看着自己的防御毁灭邪恶的入侵者更爽的事了. 在这个包含两部分的教程中,你将使用Unity创建一个塔防游戏. 你将会学到如何: 创建一波一波的敌人 使敌人随着路标移动 ...
- 使用Unity创建塔防游戏(Part3)—— 项目总结
之前我们完成了使用Unity创建塔防游戏这个小项目,在这篇文章里,我们对项目中学习到的知识进行一次总结. Part1的地址:http://www.cnblogs.com/lcxBlog/p/60759 ...
- Unity User Group 北京站图文报道:《Unity3D VR游戏与应用开发》
很高兴,能有机会回报Unity技术社区:我和雨松MOMO担任UUG北京站的负责人, 组织Unity技术交流和分享活动. 本次北京UUG活动场地–微软大厦 成功的UUG离不开默默无闻的付出:提前2小时到 ...
- Unity塔防游戏开发
Unity3D塔防开发流程 配置环境及场景搭建编程语言:C#,略懂些许设计模式,如果不了解设计模式,BUG More开发工具:Unity3D编辑器.Visual Studio编译器开发建议:了解Uni ...
- 【Unity】6.4 Transform--移动、旋转和缩放游戏对象
分类:Unity.C#.VS2015 创建日期:2016-04-20 一.简介 Unity引擎提供了丰富的组件和类库,为游戏开发提供了非常大的便利,熟练掌握和使用这些API,对于游戏开发的效率提高很重 ...
随机推荐
- mysql主从数据库设置备忘
[mysqld] binlog-do-db = databasename1 binlog-do-db = databasename2 binlog-do-db = databasename3 -- 且 ...
- MySQL触发器更新和插入操作
一.触发器概念 触发器(trigger):监视某种情况,并触发某种操作,它是提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是手工启动 ...
- Oracle 11g OCM 考试大纲
考试大纲共分9部分. 一.Server Configuration 服务器配置 1 Create the database 创建数据库 2 Determine and set sizing p ...
- Mybatis Dynamic Query 简单筛选
在框架中,筛选描述类有两种(FilterDescriptor, FilterGroupDescriptor),这里我们主要举例来说明FilterDescriptor用法. FilterDescript ...
- 【JAVASCRIPT】React + Redux
摘要 Redux 数据流图 View 层由React 控制, 根据state 变化 刷新渲染组件,作用是根据更新的数据重新渲染组件 Stroe 层其实就是state存储器,作用是更新数据 Dispat ...
- Fiddler基础使用一之捕获https请求
fiddler抓包工具: http协议cookieFiddler是一个调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的"进出"Fiddle ...
- C# 对xml进行操作
一:xml的基本操作 (1)获得xml文件中的数据 //创建xml文档对象 XmlDocument xmlDoc = new XmlDocument(); //将指定xml文件加载xml文档对象上 x ...
- c++文件编译的一些说明
1,头文件只在于预处理阶段用于完全包含该头文件的内容,每个c文件是一个编译单元,类定义和类声明,变量和函数声明,类内联实现是内部链接,全局变量和函数的定义以及类外部实现是具有全局链接性,假设将所有c单 ...
- App 组件化/模块化之路——Android 框架组件(Android Architecture Components)使用指南
面对越来越复杂的 App 需求,Google 官方发布了Android 框架组件库(Android Architecture Components ).为开发者更好的开发 App 提供了非常好的样本. ...
- Hibernate 中Criteria Query查询详解【转】
当查询数据时,人们往往需要设置查询条件.在SQL或HQL语句中,查询条件常常放在where子句中.此外,Hibernate还支持Criteria查询(Criteria Query),这种查询方式把查询 ...