我以前使用过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. java系统高并发解决方案(转载收藏)

    一个小型的网站,比如个人网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构.性能的要求都很简单,随着互联网业务的不断丰富,网站 ...

  2. java 一款可以与ssm框架完美整合的web报表控件

    硕正套件运行于客户端(浏览器),与应用服务器(Application Server)技术无关,所以能完全用于J2EE. ASP.Net.php等技术开发的Web应用产品中. 硕正套件部署于服务器,支持 ...

  3. css 子div自适应父div高度

    <div class="out"> <div class="a"></div> <div class="b& ...

  4. GitHub 入门教程

    一.前言 编程进阶的道路是坎坷的,没有任何捷径.这个时期只能是积累.吸收.学习.坚持,做到量的积累,到质的飞跃 古语有云:'书山有路,勤为径'.'不积跬步,无以至千里' 编程是一个动手实践性的学科,多 ...

  5. 基于.NET CORE微服务框架 -谈谈Cache中间件和缓存降级

    1.前言 surging受到不少.net同学的青睐,也提了不少问题,提的最多的是什么时候集成API 网关,在这里回答大家最近已经开始着手研发,应该在1,2个月内会有个初版API网关,其它像Token身 ...

  6. 如何快速查看github代码库中第一次commit的记录

    发现一个别人推荐的代码库用来学习源码, star星还不少,别人推荐从第一次commit开始阅读,于是试着去找commits的第一次 问题来了,这个代码库commits7855次,点击进入commits ...

  7. 4. leetcode 461. Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  8. Linux Shell 1 - Print from terminal

    Two ways to print info from terminal - echo & printf - Echo a. Exclamation mark is supported in ...

  9. 用 Node.js 把玩一番 Alfred Workflow

    插件地址(集成Github.掘金.知乎.淘宝等搜索) 作为 Mac 上常年位居神器榜第一位的软件来说,Alfred 给我们带来的便利是不言而喻的,其中 workflow(工作流) 功不可没,在它上面可 ...

  10. NYOJ--244--16进制的简单运算(C++控制输入输出)

    16进制的简单运算 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 现在给你一个16进制的加减法的表达式,要求用8进制输出表达式的结果.   输入 第一行输入一个正整 ...