我以前使用过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) 的简单塔防游戏的更多相关文章

  1. VS2013配合EgretVS开发简单塔防游戏

    VS2013配合EgretVS开发简单塔防游戏(1) - 环境配置 VS2013配合EgretVS开发简单塔防游戏(2) – 原型设计 VS2013配合EgretVS开发简单塔防游戏(3) – 精灵动 ...

  2. Unity简单塔防游戏的开发——敌人移动路径的创建及移动

    软件工程综合实践专题第一次作业 Unity呢是目前一款比较火热的三维.二维动画以及游戏的开发引擎,我也由于一些原因开始接触并喜爱上了这款开发引擎,下面呢是我在学习该引擎开发小项目时编写的一些代码的脚本 ...

  3. 使用Unity创建塔防游戏(Part2)

    How to Create a Tower Defense Game in Unity – Part 2 原文地址:https://www.raywenderlich.com/107529/unity ...

  4. 使用Unity创建塔防游戏(Part1)

    How to Create a Tower Defense Game in Unity - Part1 原文作者:Barbara Reichart 文章原译:http://www.cnblogs.co ...

  5. 使用unity创建塔防游戏(原译)(part1)

    塔防游戏非常地受欢迎,木有什么能比看着自己的防御毁灭邪恶的入侵者更爽的事了. 在这个包含两部分的教程中,你将使用Unity创建一个塔防游戏. 你将会学到如何: 创建一波一波的敌人 使敌人随着路标移动 ...

  6. 使用Unity创建塔防游戏(Part3)—— 项目总结

    之前我们完成了使用Unity创建塔防游戏这个小项目,在这篇文章里,我们对项目中学习到的知识进行一次总结. Part1的地址:http://www.cnblogs.com/lcxBlog/p/60759 ...

  7. Unity User Group 北京站图文报道:《Unity3D VR游戏与应用开发》

    很高兴,能有机会回报Unity技术社区:我和雨松MOMO担任UUG北京站的负责人, 组织Unity技术交流和分享活动. 本次北京UUG活动场地–微软大厦 成功的UUG离不开默默无闻的付出:提前2小时到 ...

  8. Unity塔防游戏开发

    Unity3D塔防开发流程 配置环境及场景搭建编程语言:C#,略懂些许设计模式,如果不了解设计模式,BUG More开发工具:Unity3D编辑器.Visual Studio编译器开发建议:了解Uni ...

  9. 【Unity】6.4 Transform--移动、旋转和缩放游戏对象

    分类:Unity.C#.VS2015 创建日期:2016-04-20 一.简介 Unity引擎提供了丰富的组件和类库,为游戏开发提供了非常大的便利,熟练掌握和使用这些API,对于游戏开发的效率提高很重 ...

随机推荐

  1. Angular表单控件需要类型和实际值类型不一致时实现双向绑定

    适用Angular版本为:>=2.本文同样适用于Ionic这类的基于Angular实现的框架. 本文的思路也适用于控件显示的值和实际的值不一样时实现双向绑定. 1. 问题描述 在使用md2的da ...

  2. windows调试工具列表

    摘自windbg帮助文档(windbg中输入.hh): Debugging Tools for Windows (安装WinDbg后这些工具都会安装在目录C:\Program Files (x86)\ ...

  3. 【WeX5学习】 后端服务之访问数据库表

    WeX5是跨段移动开发框架,将H5的标签封装成组件,实现可视化.组件化快速开发.实现一次开发,多端(iOS.安卓和微信)运行.WeX5的IDE基于Eclipse,提供了一个完全可视化.组件化.拖拽式开 ...

  4. runtime实现对象存储型数据库——LHDB

    前言 最近在GitHub上看了一份关于基于runtime封装的对象存储型数据库的开源代码,觉得非常值得分享记录一下,在IOS中对数据库的操作一般通过CoreData和SQLite,CoreData 虽 ...

  5. salesforce零基础学习(七十五)浅谈SOSL(Salesforce Object Search Language)

    在工作中,我们更多操作的是一个表的对象,所以我们对SOQL的使用很多.但是有时候,我们需要对几个表进行查询操作,类似salesforce的全局搜索功能,这时,使用SOQL没法满足功能了,我们就需要使用 ...

  6. 一步一步学习Vue(六)

    本篇继续介绍vue-router,我们需要要完成这样个demo:<分页显示文章列表>:这里我们以博客园首页列表为例简化处理: 按照上图框选所示,简单分为蓝色部分文章组件(ArticleIt ...

  7. SQL注入的各种类型的检测方式

    #SQL注入各个类型检测方式 http://127.0.0.1/day6/1.php?id=1 union select 1,name,pass from admin 数字型 数字型不用特意加字符,直 ...

  8. 计算机网络之应用层_part -1

    应用层协议原理 一.网络应用程序体系结构 网络应用程序体系结构是由程序研发者设计的,规定了如何在各种端系统中组织该应用程序,主要流行的有两种: 1.客户--服务器体系结构: 有一个总是打开的主机(称为 ...

  9. 第五章之S5PV210将u-boot.bin从SD卡中搬到DDR中

    1,在完成上一节的memory初始化后,接下来在arch/arm/cpu/armv7/start.S的160行:如下图 2,跳转到arch/arm/lib/board.c下的board_init_f函 ...

  10. Linux入门基础知识

    注:内容系兄弟连Linux教程(百度传课:史上最牛的Linux视频教程)的学习笔记. Linux入门基础知识 1. Unix和Linux发展历史 二者就像父子关系,当然Unix是老爹.1965年,MI ...