该视频是由广告中的广告组合赞助的，我们将做一些 this video is sponsored by ad mix in this video we're going to make some
简单的塔防AI，我们将拥有可以放置的塔 simple tower defense AI we're going to have our towers that we can place and as
在塔将瞄准的攻击范围内产生敌人 enemies get spawned an approach within the attack range the tower will aim and
射击塔也可以通过扩展范围和损坏来升级 shoot the towers are also upgradable with expandable range and damage let's
开始 begin [Music]
好吧，这就是我们要在这里做的是将我的塔放在 okay so here is what we're trying to make over here is my tower placed in the
地图，当我将鼠标悬停在上面时，可以看到该塔的攻击范围 map and when I mouse over I could see the attack range of this tower and by
按下一个按钮，我就可以发动敌人的波浪，使敌人开始产生 pressing a button I can start the enemy wave so the enemy start being spawned
他们将沿着自己的道路前进，并进入 and they're going to follow their own path and as they get within the range of
塔塔开始攻击并发射箭，箭击中 the tower the tower starts attacking and firing arrows and the arrows hit the
敌人，他们受到足够的打击而受到伤害，敌人死了，所以我有一座塔 enemies and they get damaged with enough hits the enemies die so I have one tower
但我也可以轻松地生成更多塔楼，并且生成 but I can also easily spawn a whole bunch more towers and the spawning is
正确放置在网格上，因此我们使用的是在其中创建的网格系统 correctly placed on a grid so we're using the grid system that we created in
以前的视频，我们可以通过启用Gizmos将其可视化，这里是 a previous video and we can visualize it by enabling gizmos there it is here's
我们的网格，所以这里是另一座塔，但是现在你走了，我们 our grid so it's one another tower right down here and yet there you go now we
有两个塔，现在我们可以产生更多的敌人，他们来了， have our two towers and now we can spawn some more enemies and here they come and
如您所见，这些塔也可以升级，因此在左侧 as you see the towers are also upgradable so here on the left side we
可以增加射程，在右侧我们可以增加伤害，所以它 can increase the range and on the right side we can increase the damage so it
催生了更多的敌人，让我一见就知道 spawned a bunch more enemies and let's see and yep as soon as I get within
他们开始受到打击，现在受到4548点伤害 range they start getting hit and now they're taking 4548 damage the damage
我们在上一个视频中也弹出了弹出窗口，所以请检查一下，就像这样 pop ups we're also made in a previous video so check it out and just like this
我们有一些非常简单的塔防AI工作，因此您可以右键单击以 we have some very simple tower defense AI working so you can right-click to
再放一堆塔楼，然后产生一堆敌人，他们 place a whole bunch more towers and now spawn a bunch more enemies and they
当它们进入任何一个塔的范围内时，它们就会开始出现 start coming as they get within range of any of the towers the towers start
打他们，如果我想我可以升级他们，等等等等，以便 hitting them and if I want I can upgrade them and so on and so on in order to
从长远来看，继续开发游戏，您需要考虑营利 continue building your games in the long run you need to think about monetization
AB混合是一个很好的工具，可以帮助您解决AD混合问题 AB mix is a great tool to help you solve that with AD mix you can place
游戏中的非侵入式集成广告，而不是难看的横幅 unintrusive integrated ads inside of your game so instead of an ugly banner
广告在您的游戏内部拥有一个广告牌，这对于 ad you have a billboard right inside your game this is especially useful for
通过AR和VR内容获利而又不影响玩家的沉浸感，并且 monetizing AR and VR content without breaking player immersion and since the
广告的干扰性较小，可以带来更好的播放器体验， ads are less intrusive that leads to a better player experience which leads to
较高的保留率，可让您赚更多的钱，因此您可以继续建房 higher retention which allows you to make more money so you can keep building
您的游戏真棒，该工具非常易于使用 your awesome game the tool is extremely easy to use
只需下载unity插件并导入您的场景，您将看到此 just download the unity plugin and import into your scene you'll see this
窗口，您可以在其中选择各种尺寸并拖放 window where you can select from a variety of sizes and drag and drop the
将对象混合到您的场景中，该对象具有很好的适应性，因此可以适合任何对象 ad mix object into your scene the object is very adaptable so it can fit any
像平坦的东西或将其放在周围 surface like something flat or put it around
如您所见，它非常易于使用，并且拥有游戏内广告 as you can see it's extremely simple to use and having in-game ads is a lot
设置完所有广告后，比烦人的横幅广告要好 better than annoying banner ads once you've set up all your ads click this
按钮，以在您不开始生成时验证您的应用 button to your website to validate your app once you do don't start generating
不到24小时即可获得收入，因此广告组合是一种非常易于使用的工具， revenue in under 24 hours so ad mix is an extremely easy to use tool that works
比标准广告更好，除非您立即开始赚钱 better than standard ads unless you start earning revenue immediately go to
广告混入开始并下载n mix unity插件以开始赚钱 ad mix start in and download the n mix unity plugin to start earning revenue
今天感谢您添加组合，也感谢这些出色的支持者 today thank you to add mix and also thank you to these awesome supporters
有关使此视频成为可能的信息，请访问patreon.com/scishow for making this video possible go to patreon.com/scishow
编码猴子以获得一些好处，并帮助所有人免费观看视频 code monkey to get some perks and help keep the videos free for everyone
好吧，这个圈子，让我们开始吧，好了，这就是我们的主演 alright so this circle let's get to it okay so here we are in our starring
我们所拥有的场景就是基本的地图设置，因此您可以看到它们的路径 scene all we have is the basic map setup so as you can see the path where they
按下按钮不会拿走我可以生出一些敌人，然后你就去了 won't take by pressing a button I can spawn some enemies and there you go they
在那里的角落产生，他们终于开始移动敌人 get spawned on the corner there and they finally start moving the enemies go
穿过那里的力量，穿过那里，在那里我 through the power through there there and out through there also in here I
有一个网格设置，所以如果我们启用gizmos，您可以看到所有 have a grid setup so if we enable gizmos there you go we can see all of the
在上一个视频中完全制作了网格系统的各个网格位置，因此 various grid positions the grid system was fully made in a previous video so
检查一下，在这种情况下，我们将使用网格来 check that out and in this case we're just going to use the grid in order to
将塔盖印到其位置，例如，我们将能够放置 stamp the towers into their position so for example we'll be able to place a
塔这里一个这里一个这里等等等等，所以让我们开始做 tower here one here one here and so on alright so let's begin by making the
在项目文件中可以看到塔的视觉效果，例如，我有一些纹理 tower visual here in the project files I have some textures for example this one
用塔将其拖到场景上，现在让我们制作一个空游戏 with a tower let's drag it onto the scene and now let's make a empty game
对象，这将是我们的主塔，我们将塔精灵拖入我们的内部 object this will be our main tower and we drag the tower sprite inside of our
塔游戏对象，这样我们现在可以轻松地定位精灵，以便 tower game object so this way we can now easily locate the sprite in order to
确保它与网格位置匹配现在我们还要添加另一个 make sure that it matches with the grid position now let's also add another
空通道游戏对象，所以是一个空的，我们称之为 empty channel game object so an empty one yep and let's call this the
弹药适合的位置，所以这是弹丸将要放置的位置 projectile suit from position so this is the position where the projectiles will
被解雇，所以在这种情况下，我们只需将其定位在 be fired from so in this case let's just locate it right up there at the top of
塔就好了，就像这样，我们有了一个非常基本的视觉设置，现在让我们 the tower ok so just like this we have a very basic visual setup now let's make
要处理的脚本，所以让我们创建一个新的C Sharp脚本并附带 the script to handle so let's create a new C sharp script and come with just
我们的塔让我们将其拖到游戏对象上，然后打开确定，首先让我们抓取 our tower let's drag it on to the game object and open ok now first let's grab
参考我们拍摄前的位置 reference to our shoot front position
所以这里就是我们要做的一个转换文件，我们得到了那个位置 so there it is we just do a transform file and we get the position of that
游戏对象，这样我们可以在视觉上找到它，然后获得位置 game object so this way we can visually locate it and then we get the position
通过代码，现在在这里我们还捕获鼠标单击，所以我们做一个 through code and now here let's also capture the mouse click so let's do a
完美男孩更新，在我们的更新内，您只需输入一个简单的鼠标即可 perfect boy update inside of our update you just do a simple input get mouse
按下鼠标左键，仅用于测试，当我们单击时使用 button down left mouse button and just for testing when we click let's use a
只要您可以下载实用程序中的功能，就可以弹出一个弹出窗口 function from the utilities to spawn a pop up as long as you can download the
从统一代码免费获得的实用工具猴子平静了，就这样，让我们​​来看看 utilities for free from unity code monkey calm so just like this let's see
如果可以的话，我们在这里，那里是塔楼，然后单击，然后就走了，我们 if it's working okay here we are there's the tower and click and there you go we
让我们的点击正确运行，以便正确运行塔脚本 have our clicks correctly working all right so the tower script is correctly
运行，现在我们有了塔，让我们先发射弹丸，让我们 running so now that we have our tower let's fire off a projectile first let's
创建我们的射弹，所以在这里，我有一个简单的箭头精灵， create our projectile so over here I have a simple sprite of a arrow so just
将其拖动到场景上，是的，现在有投射箭头，让我们 drag it onto the scene and yep there's the projectile arrow now let's make a
脚本来控制它，因此我们为射箭创建了新的c-sharp脚本 script to control it so we create the new c-sharp script for projectile arrow
并将脚本直接拖到它上面好吧，现在我们想在执行期间生成箭头 and drag the script straight onto it ok now we want to spawn our arrows during
运行时，为了做到这一点，让我们先将箭头打入预制 runtime so in order to do that let's make our arrow into a prefab so first
让我们创建一个文件夹来构成我们的预制件，我们只需拖动箭头 let's create a folder to form our prefabs and we simply drag the arrow
到目前为止，我们将其重命名为PF射弹箭头 onto it let's rename it PF projectile arrow alright so far so good we have a
预制射箭的预制件现在我们要在射击时生成箭头 prefab for the projectile arrow now we want to spawn arrows when we shoot so
让我们打开射箭类，在这里让我们做一个简单的静态 let's open up the projectile arrow class and in here let's make a simple static
函数以生成箭头，因此称为create and in的私有静态void function in order to spawn a arrow so private static void called create and in
在这里我们要实例化您将要实例化我们的PF弹射箭头 here we want to instantiate you're going to instantiate our PF projectile arrow
因此，在这里自然需要我们对预制件的参考，在这里 so naturally in here we need a reference to our prefab and for that over here in
现场我已经有了一个非常家庭电话游戏资产类，它是在 the scene I already have a very home phone game assets class this was made in
上一个视频，因此您可以看到该对象具有一堆字段 a previous video so this object as you can see it has a bunch of fields a bunch
参考，它有这个脚本，这个脚本的作用是我们有一个 of references and it has this script and this script what it does is we have a
静态实例，然后我们有一堆公共字段，所以在这里我可以 static instance and then we have a whole bunch of public fields so in here I can
只需为PF抛射箭头添加小马变换 simply add a pony transform for the PF projectile arrow
现在回到这里的箭头脚本中，我现在可以访问游戏资产访问权限 and now back in the arrow script in here I can now access the game assets access
这些静态实例和重力射弹箭头再次涵盖了游戏 these static instance and gravity projectile arrow again I cover the game
资产脚本在其他视频中有详细说明，因此请查看说明中的链接，以便 asset script in detail in another video so check the link in the description so
在这里我们可以生成一个箭头，我们还可以接收到一个生成的向量340 here we can spawn an arrow and let's also receive a vector 340 spawned
位置，所以我们以四元数身份在那里生成 position so we spawn in there with quaternion identity all right so this
应该产生一个弹箭，以便进行测试，让我们尝试在 should spawn a projectile arrow so for testing let's try to spawn it on the
鼠标的位置，所以首先要公开而不是私有，现在让它公开 mouse position so first instead of private let's make this public and now
进入塔楼，然后在这里按下鼠标左键，我们开始吧 go into the tower and then here when you press on your left mouse button let's go
进入项目向下箭头脚本，以创建一个新的箭头和一个免费的 into the project down arrow script in order to create a new arrow and a free
位置让我们使用鼠标位置，所以这里再次使用另一个功能 position let's use the mouse position so here again using another function from
该实用程序是为了使鼠标在此处很少位置 the utilities in order to get the mouse little position here is the function in
如果您想自己制作，可以看到非常简单，只需抓住世界 case you want to make it yourself as you can see very simple just grab the world
相机并在屏幕位置上做一个屏幕旋转点，所以让我们 camera and do a screen twirl point on the screen position all right so let's
测试一下，看看您何时按向左键，箭头是否在Senshi中 test and see if we see an arrow being in Senshi ated when are you pressing left
鼠标键我们需要做的最后一件事就是拍摄 mouse button the last thing we need is just to film
有了这个参考，所以我们只是将预制件拖到那里好吧，让我们测试一下 with this reference so we just drag the prefab right in there okay let's test
好的，我们在这里，然后单击，然后您就可以找到正确产生的箭头 okay here we are and click and there you go there's the arrow correctly spawned
太棒了，所以我们正确地产生了箭头预制件 right awesome so we have the arrow prefab being correctly spawned in the
正确的鼠标位置，现在我们不想在鼠标位置上生成它，但是 correct Mouse position now we don't want to spawn it on the mouse position but
而是我们想在塔上生成它并朝某个位置射击，所以 rather we want to spawn it on the tower and shoot towards a certain position so
在这种情况下，朝着鼠标的位置射击，因此返回箭头脚本 in this case shoot towards the mouse position so back in the arrow script
让我们接收一个生成位置，然后是目标位置的vector3 let's receive a spawn position and then also a vector3 for the target position
现在在我们的对象中，让我们创建一个设置函数，以便我们可以 and now here in our object let's make a setup function just so that we can
接收目标位置的向量三，当我们创建 receive our vector three for the target position and when we create to
实例化并获取箭头变换，然后获取该类型的组件 instantiate and grab the arrow transform then we do get component of this type
投射箭头，然后我们可以调用安装程序并传递到目标位置 projectile arrow and then we can call setup and pass in our target position
好吧，现在当我们调用此函数时，我们将传递 all right so now when we call this function we're going to pass in the
生成和Atari位置，现在我们有了这个位置，我们需要做的就是 spawn and Atari position and now that we have this position all we need to do is
确保我们的项目我们的箭头朝着它前进，所以现在在这里 make sure that our project our arrow moves towards it so now here
我们需要问一个问题，即我们应如何实质上移动投影机 we need to ask a question which is how should we move the projector essentially
我们有两个选择，我们可以手动移动它，也可以使用物理 we have two options we can move it manually or we can use the physics
系统，这将取决于您的设计目标和您要尝试的目标 system so that's going to depend on your design goals and what you're trying to
在这里处理您的游戏，我们将以最简单的方式进行 do with your game here we're going to do the simplest way possible so just
无需物理操作即可手动移动变换，因此仅在我们的私人男孩上 manually moving the transform with no physics so just on our private boy
更新我们计算朝向目标位置的方向，这样我们就可以 update we calculate the direction towards the target position so we have
移动方向我们以一定的移动速度向移动方向移动 the move direction we move towards the move direction by a certain move speed
然后我们检查一下距离，以便我们是否足够接近 and after we do let's just check the distance so if we are close enough to
目标位置，然后我们就可以摧毁这个游戏对象 the target position then we simply destroy this game object all right so
那里有一些非常简单，非常基本的运动代码，现在我们可以回到 there it is some very simple very basic movement code now we can go back into
我们的塔脚本，在这里，我们在函数中有两个参数，第一个 our tower script and in here now we have two parameters in our function the first
一个是生成位置，所以我们将使用从 one is a spawn position so we're going to use our projectile shoot from
位置并作为目标，让我们向鼠标射击吧，让我们 position and as the target let's shoot towards the mouse all right so let's
测试好吧，所以我们在这里，然后单击其中的任何一个，其中有箭头， test okay so here we are and click any of there you go there's the arrow and
正确地进行了操作，因此，例如，单击此处并说达到了 correctly goes so let's say click in here and it says it reaches yep it gets
罢工棒极了，现在我们需要固定速度和旋转 the strike all right awesome now we need to fix the speed as well as the rotation
因此，为了提高速度，我们只需增加速度和旋转速度 so let's do that for the speed let's just increase it and for the rotation
让我们正确地注油，所以我们使用的是 let's get the oiler angles all right so there it is for the angles we're using a
函数将向量三转换为欧拉角，这是 function to transform a vector three into a Euler angle and here is the
功能，以防您想自己动手，我们只需设置欧拉 function in case you want to make yourself and we just set the Euler
角度，因为我们在2D模式下工作，所以我们只在DZ上设置好，让我们看看好 angles and since we were working in 2d we only set on D Z okay let's see okay
并点击他们的任何一个哟，现在我们已经从 and click any of their yo now we have errors correctly being fired from the
朝目标鼠标位置倾斜 tower and towards the target mouse position all right awesome
好了，现在我们有了基本的塔式功能，让它正常工作 okay so we have our basic tower functionality now let's make it work
如果我已经设置了一个简单的脚本以生成一些脚本，现在就与敌人 with enemies now if I'm already set up a simple script in order to spawn some
敌人，因此只需按一个简单的键，敌人就会开始 enemies so by pressing a simple key there you go the enemies start being
生成并开始显示，因此脚本在此游戏对象上 spawn and they start showing up so the script is on this game object here it is
这是脚本，非常简单，我们要做的就是首先制作 here is the script it's very simple all we're doing is first of all making
网格，所以我们有验证它的功能 the grid so we have the function to validate it
这只是为了以后我们可以将塔架卡入位置，然后 this is just so we can later snap our towers into position and then we just
在不同的难度下有三种不同类型的波浪 have three different types of waves in different difficulties and on each wave
我们只是发现了一大堆敌人，就逻辑而言，他们所做的一切 we just find a whole bunch of enemies and in terms of logic all they're doing
是盲目地跟随一个简单的Waypoint，所以现在非常简单 is blindly following a simple Waypoint so there it is very simple now the
通过此功能可以创建敌人，这是我们正在做的漂亮 enemies are created through this function here it is we're doing pretty
我们在弹箭上所做的几乎相同的事情 much the same thing that we're doing on the projectile arrow
因此，我们对敌人的预制件进行了感应，然后我们进行了基本设置，现在 so we insensate the enemy prefab and then we do a basic set up and now as the
醒来时会在此处创建敌人，并将它们添加到某个敌人列表中，因此 enemies are created here on awake they are added onto a certain enemy list so
这是一个静态列表，其中包含所有在世敌人以及 this is a static list which contains all of the alive enemies and with that over
在这里，我有一个简单的功能来取得最接近的敌人，它占据一个位置并 here I have a simple function to get the closest enemy it takes a position and a
最大范围，并简单地循环遍历所有敌人，而忽略其中的任何一个 max range and simply cycles through all the enemies ignores if any of them are
仍然死了并寻找范围内最近的敌人，所以此功能是 still dead and looks for the closest enemy within range so this function is
为了寻找最接近我们塔的敌人，我们将使用什么 what we're going to use in order to look for the closest enemy to our tower so
让我们进入我们的塔脚本，在这里让我们停止手动触发我们的 let's go into our tower script and here let's stop manually firing our
弹丸，在这里，让我们做一个函数来获取最近的敌人 projectiles and in here and let's make a function to get the closest enemy now
我们将在敌人类别上使用该函数，以便为 we're going to use the function on the enemy class so get closest enemy for the
通过此变换飞镖位置的位置，现在对于最大范围，让我们 position passing this transform dart position and now for the max range let's
将其存储在字段中 store it in a field
所以就这样，所以现在在我们的更新中，我们可以尝试获取最接近的 so just like this all right so now in our update we can try to get the closest
敌人，如果不知道，那么我们有一个有效的敌人，所以在这里，我们 enemy and if it is not know then we have a valid enemy so it's in here that we
想要生成我们的射箭，它将从我们的射击位置朝着 want to spawn our projectile arrow going to spawn from our shoot position towards
敌人的位置没问题，所以我们要像这样 the enemy position all right so there it is however like this we're going to have
一个问题，但让我们尝试一下，看看现在还可以，到目前为止，看来 an issue but let's try it out just to see okay here we are and so far it seems
好，但让我们产生一些敌人，他们来了，现在让我们看看 good but let's spawn some enemies and here they come and now let's see what
当它们进入范围并发生时，它们就会发生，这是疯狂的 happens as they get within range and yep they're going with that that is insane
好吧，所以在这里我们基本上看到一个箭头被发射 all right so here we are seeing essentially one arrow being fired by
每个朋友，所以很明显我们不想射那么多箭 every single friend so obviously we don't want to shoot that many arrows so
让我们添加一个简单的拍摄计时器，让我们 let's add a simple shoot timer so let's make a
相位射击计时器可以，所以我们有一个非常基本的射击计时器 phasic shoot timer all right so we have a very basic shoot timer coming down
用我们的增量时间，如果我们低于零，那么我们测试是否有敌人 with our delta time and if we go under zero then we test if we have an enemy
我们重置了拍摄定时器，所以这很简单，让我们看看可以，所以在这里 and we reset our shoot timer so there it is very simple let's see okay so here we
现在让我们产生一些敌人，他们来了，因为它们在里面 are now let's spawn some enemies and here they come and as they are within
射程，然后塔就开始射击更容易控制的 range and there you go the tower start shooting a more manageable amount of
到目前为止，所有箭头都很好，所以现在没有损坏，但逻辑是 arrows all right so far so good so right now there's no damage but the logic is
实际起作用，所以让我们现在伤害我们的敌人 actually working so let's damage our enemies now the
敌人已经具有此伤害功能，它将造成伤害 enemies already have this damage function it takes in an int for a damage
并产生一些粒子和一堆东西，并减少 amount and simply spawn some particles and a bunch of things and reduces on the
健康系统，让我们使用此功能让我们回到弹丸中 health system so let's use this function let's go back in our projectile and in
当弹丸到达目标位置时我们就在这里，所以它在这里 here we have when the projectile reaches the target position so it's in here that
我们实际上需要称呼敌人伤害，这意味着我们需要获得一个 we need to essentially call enemy damage so that means we need to receive a
参考我们的目标敌人，所以让我们在这里，而不是我们的创造 reference to our target enemy so let's go up here and our create instead of
接收目标位置让我们接收敌人，我们绕过每个目标 receiving a target position let's receive a enemy and we pass around each
设置，这里我们将目标位置定义为敌人位置，现在 setup and here we define the target position as the enemy position and now
在这里我们实际上可以调用我们的损伤函数，所以在这里我们需要损伤 here we can actually call our damage function so here we need the damage
因此，我们也可以收到赔偿，因此我们也可以得到赔偿金额 amount so let's also receive that all right so we also get the damage amount
在这里，我们可以使用它，现在应该回到塔中了 and in here we use it all right that should do it now back in the tower all
我们需要的是当我们产生怪胎时敌人参考的人 we need is the person the enemy reference when spawning our freak out
现在我们还需要损坏量，所以让我们像使用 and now we also need the damage amount so let's the final like we did with the
范围好吧，我们有一个伤害值，这里只是为了好玩，让我们将其随机分配 range okay we have a damage amount and here just for fun let's randomize it a
有点应该做的让我们在这里测试好吧，让我们 bit all right that should do it let's test okay here we are and let's
产生一些敌人，然后它们开始出现，随着进近，他们 spawn some enemies and here they start to come and as the approach yep they
开始被枪杀，然后就像他们都被枪杀一样 start getting shot and there you go just like that they're all being shot and
其中一些实际上还活着，但是是的，我们去了 some of them are actually living but yep there we go
我们的箭头很棒，所以在这里，我们有塔， we have the arrows working awesome okay so here we have our Tower and it's
正确射击附近的敌人，现在让我们做一个简单的迷你 correctly shooting at nearby enemies now let's make a simple mini
很好的系统，所以在塔中，我们已经为 great system alright so here in the tower we already defined a field for the
范围和伤害的数量，所以它很容易操作 range and on for the damage amount so it's going to be very easy to manipulate
这些让我们做一些功能来做到这一点，所以在这里让我们做 these let's make some functions to do that all right so here let's do very
简单函数只是修改我们的简单字段，然后再次使用 simple functions just modifying our simple fields which again are then using
此功能，然后在此功能中，现在进入编辑器 this function and up here in this function now let's go into the editor to
让我们的升级看起来不错，所以在这里我们创建一个空的游戏对象， make our upgrade visual okay so in here let's create an empty game object let's
公开称为升级，所以这是我们要放置的对象 call this the upgrade openly so this is the object that we're going to place on
为了升级塔的顶部，在里面添加一个精灵 top of the tower in order to upgrade and inside let's add a sprite with this
现在在此部分中，它做了两个按钮的升级和一个按钮的关闭 section here now it's made two buttons to upgrade and one button to close all
正确，所以现在这里是按钮我正在使用按钮废料的地方 right so there it is now here for the buttons I'm using the button scrap
脚本也是代码模拟实用程序的一部分，它只是一个简单的精灵 script which is also part of the code mock utilities it's just a simple sprite
基本按钮以便捕获鼠标交互，因为这是一个精灵 base button in order to capture Mouse interactions since this is a sprite it's
基于闯入，所以我们需要一个简单的对撞机，所以在这里 based on doing break-ins so we require a simple Collider alright so here we have
我们的升级叠加层游戏对象实际上，我们将其放置在 our upgrade overlay game object we're essentially going to place this right on
为了升级塔的顶部，现在让我们为此编写一个脚本 top of a tower in order to upgrade that tower now let's make a script for this
所以一个新的脚本，只要附加好就可以了，现在先打开它，让我们做一些非常 so a new script and just attach it okay now open it first let's make some very
基本的显示和隐藏功能，因此只需将游戏对象设置为true或false basic show and hide functions so just set the game object to true or false
好吧，现在在节目中，我们也收到一个 okay and now on the show let's also receive a
我们将要显示的塔，将其设置为true时，我们还将 tower that we're going to show on and when we set it to true we also set the
将怀疑位置转换为塔式转换位置，使其位于顶部 transform doubt position to the tower transform position so it's right on top
现在，为了使此脚本易于使用，让我们 of the tower and now in order to make this script easy to use and let's make a
静态显示功能 static show function
好的，所以这里是一个静态实例，将其设置为清醒状态， all right so here it is we just have a static instance we set it on awake and
我们有我们的show static函数，可以在调用实例时使用-哦，这只是 we have our show static function which in calls the instance - oh this is just
所以我们有一个非常容易使用的函数，现在我们需要做的是调用 so we have this function that is very easy to use now we need to do is call
当我们将鼠标移到塔上时，返回编辑器 this when we pass the mouse over the tower so back in the editor
让我们与塔楼不同，然后在上面套上一个对撞机 let's unlike the tower and apply a box Collider onto it all right there's the
对撞机，现在我们可以进入塔脚本 collider and now we can go into the tower script
在这里我们可以实现我们自己的鼠标回车功能 and in here we can implement our own mouse enter function this gets triggered
当鼠标经过与该对象相连的对撞机时，在这里让我们 when the mouse passes over the collider attached to this object so in here let's
只需进入升级覆盖图即可调用我们的show函数并通过 just go into the upgrade overlay in order to call our show function and pass
在我们的塔中好吧，就像这样，它应该可以正常工作 in our tower alright so just like this it should be working
让我们测试一下，在这里，因为我将鼠标移到了其中的任何一个上， let's test okay here we are as I passed the mouse over any of there you go the
叠加层显示的很好，现在让我们实现 overlay shows up alright good now let's just implement the functions for the
纽扣 buttons
好的，所以就在这里，但是醒着之后，我们只需获取按钮精灵组件 all right so here it is but awake we simply get the button sprite component
并且我们将绞痛功能设置为这些连接功能，因此隐藏原因 and we set the colic function to these connect functions so in the hide reason
在升级范围的后面，您只需调用函数即可升级我们的塔 behind on the upgrade range you simply call the functions to upgrade our tower
然后我们还有另一个功能或可以更新视觉效果，所以 and then we just have another function or to update the visuals all right so
让我们测试一下，所以如果您在并且有塔，现在将鼠标放在 let's test okay so if you're and there's the tower and now place the mouse over
是的，你现在去吧，我们可以看到很好的房屋范围，我们可以 yep there you go now we can see the home range all right very nice and we can
也可以增加范围，因此通过单击按钮，您可以找到范围 also increase the range so by clicking the button and there you go the range
确实会增加，这也会增加伤害，所以如果我产生一些 actually does increase and this one also increases damage so if I spawned some
敌人现在，我们看着他们，在这里，他们来了 enemies right now and we look at them and here they come and as soon as they
进入范围内，然后你就开始射击，他们需要大量的 get within range yep there you go they start getting shot and they take tons of
伤害让我们继续增加，然后你会遭受更多的伤害 damage let's keep increasing and there you go a lot more damage and a much
范围更大，好吧，就像这样，我们有了 bigger range all right so just like this we have our
现在，铁塔完全可以正常运行，并且拥有自己的升级覆盖层 tower fully functioning with its own upgrade overlay awesome now finally
现在让我们制作基本的塔式生成器，以便生成我们的塔 let's just make our basic tower spawner now in order to spawn our tower it's
将会非常简单，我们已经有了塔，我们需要新的是转换 going to be very simple we already have our tower all we need new is convert
将其放入预制件中，然后生成该预制件，以便使其成为预制件 this into a prefab and then spawn that prefab so to make it a prefab just drag
将其放到prefabs文件夹中，然后转到脚本 it on to the prefabs folder yep there you go and now it's going to the script
我们用来产生敌人的武器，所以在这里我们可以轻松地添加一个 that we're using to spawn our enemy so this one and in here we can easily add a
生成Tower功能，所以首先让我们测试一下输入，将鼠标按钮按下 spawn Tower function so first let's test for the input get mouse button down
鼠标右键是什么意思 what's this for the right mouse button
我们产生了一个塔 and we spawn a tower
好吧，就是这样，所以我们做的差不多 all right so there it is just like that so we're doing pretty much the same
我们一直在做的事情，所以我们在San Shi a ting获取参考 thing that we're always doing so we're in San Shi a ting grabbing the reference
通过游戏资产并在鼠标位置上做出响应 by the game assets and responding it on the mouse position which is being
使用我们的握把验证过，这样非常简单，应该可以正常工作 validated using our grip okay so that's it very simple and it should be working
让我们测试一下，好了，现在就到了，我们没有塔了，现在放鼠标 let's test okay so here we are and we have no towers now let's place the mouse
在这里，右键单击其中的任何一个，我们就可以正确生成塔了 in here and right click any of there we go we have our tower correctly spawned
如果我们产生一些敌人，您就不会看到该塔正在完全运作，所以这里 and if we spawn some enemies you don't see that tower is fully working so here
他们来了，如果他们进入了范围之内，那么塔就开始射击 they come and as they get within range if there you go the tower starts firing
然后你就可以打倒敌人了 and there you go taking down enemies all right great
再一次，这是与我们漂亮的网格一起工作的，所以您去的很完美 and again this is working with our nice grid so there you go it's perfectly
现在，我们在这里放置另一个塔，然后右键单击 located in there now let's place another tower right here so right-click and if
现在你去那里，我们有两个塔，它们分别是 there you go now we have these two towers and they are individually
可升级，所以让我们找到更多的敌人，我们隐藏了小发明 upgradeable so let's find some more enemies and we hide our gizmos and there
你现在就去拿走那和我在我里面的东西，等等 you go now taking out that and me that in me and so on any of their going with
很好，所以现在只有两座塔足以应付这些敌人 that very nice so now just two towers are enough to handle these enemies we
可以产生更多敌人，然后我们可以尝试升级塔 can spawn a whole bunch more enemies and then we can try upgrading the tower so
让我们增加这一范围，而这也增加伤害 let's increase the range on this one and this one as well increase damage by
相当多，这些太多了，但是是的，你去了这里 quite a bit and these are way too many but yep there you go over here we have
我们的系统正常工作，因此在这里我们有一个非常不错的简单塔防AI our system working so here we have a very nice simple tower defense AI
工作，因此我们可以通过右键单击来放置塔，然后您就可以 working so we can place towers just by right-clicking and there you go we can
放置一堆塔楼放置塔楼时不要在里面寻找敌人 place a whole bunch of towers the towers get placed don't look for enemies within
范围，如果有的话，我们只是攻击他们，塔楼也有 range and if there are some then we simply attack them the towers also have
一个非常好的迷你升级系统，以增加范围以及 a very nice mini upgrade system in order to increase the range as well as the
再次造成伤害，这是一个非常简单的塔防AI迷你游戏，谢谢 damage so again here it is a very simple tower defense ai mini game thank you to
添加混音，也感谢您为这些出色的支持者制作的视频 add mix and also thank you to these awesome supporters for making this video
可能去patreon.com/scishow t猴子代码以获得一些津贴和帮助 possible go to patreon.com/scishow t code monkey to get some perks and help
只要您可以下载项目，就可以让所有人免费观看视频 keep the videos free for everyone as long as you can download the project
公用代码中的工具文件猴子comm，因此开始进入 files in utilities from unity code monkey comm so start to the channel for
更多的统一教程发布任何人都有它的帐户，下一个见 more unity tutorials post any person have it accounts and I'll see you next
时间 time [Music]