How to make tower defense game Code, operation, description--unity 5.3.5F1

Source: Internet
Author: User
Tags unity 5

I've used unity before but for the first time I've written such a comprehensive tower defense game. I will be a succession of some of the projects I have experienced and experience published hope that you can give the evaluation, I am grateful to you for your criticism and praise. In addition I am just a student not fine, shoddy also please see not the past of the great God let go ... 0.0 ..... ...................

First, the management settings for the path

usingUnityengine;usingSystem.Collections;namespaceraygame{ Public classPathmanager:monobehaviour {//Road Points Group         Publicgameobject[] Path1;  Publicgameobject[] Path2; /// <summary>        ///return to the road point/// </summary>        /// <returns>The node.</returns>        /// <param name= "Pathid" >The path index.</param>        /// <param name= "Nodeindex" >Road Point Index</param>         PublicGameobject GetNode (intPathid,intNodeindex) {            if(Pathid = =1){                //out of Path array range, return Null                if(Nodeindex >=path1.length) {return NULL; } Else {                    //return to the corresponding point game object                    returnPath1 [Nodeindex]; }            }Else{                if(Nodeindex >=path2.length) {return NULL; } Else {                    returnPath2 [Nodeindex]; }            }        }    }}

Then the gamer manages and sets

Authors:liu yong//Email: <[email protected]>//qq:1931195500using unityengine;using System.Collections ; using Unityengine.scenemanagement;namespace Raygame{public class Gamemanager:monobehaviour {Animator canvasAnim;// Maximum health value public int totallife = 20;//maximum monetary value public int totalgold = 200;//current health value int curlife = 0;//current Gold int curgold = 0; Uimanager uimgr;public int[] towerprices;void awake () {uimgr = Gameobject.find ("Uimanager"). Getcomponent<uimanager> (); Canvasanim = Uimgr.getcomponent<animator> ();} void Start () {datainit (); Uiinit ();} Data initialization void Datainit () {curlife = Totallife;curgold = Totalgold;} The UI interface Initializes void Uiinit () {uimgr.setheartvalue (curlife); Uimgr.setgoldvalue (curgold);} Add Money public void addgold (int num) {curgold + = Num;uimgr.setgoldvalue (curgold);} public void usegold (int num) {curgold-= Num;uimgr.setgoldvalue (Curgold);} Increase health value 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 () {//Touch switch Canvasanim.settrigger ("Showstartbutton");} public void Entergame () {scenemanager.loadscene ("Game2");}}}

Monsters Management and Settings

//Authors:liu Yong//Email: <[email protected]>//qq:1931195500usingUnityengine;usingSystem.Collections;usingSystem.Threading;namespaceraygame{ Public classMonstermanager:monobehaviour {//Monster Resource Path        stringMonpath ="Prefabs/monsters/mon1"; //Spawn Point         PublicTransform Spawnpoint; //Refresh Time//Public float Spawninterval =3f;        voidStart () { for(intI=0; i<=5; i++) {Spawn (); } invokerepeating ("Start2", *, -); }        voidStart2 () { for(intI=0; i<=5; i++) {Spawn (); }        }        voidSpawn () {//random number generation, semi-closed half-open interval//load RA material resources and instantiate game objectsGameobject Mon =(Gameobject) Instantiate (Resources.load (Monpath), Spawnpoint.position, quaternion                . Identity); //Associating Monster Mobile ComponentsMon. Addcomponent<monstermove> (); Mon. AddComponent<MonsterHealth> (); Mon. AddComponent<Rigidbody2D> (); Boxcollider2d Collider= Mon. Addcomponent<boxcollider2d> (); Collider.istrigger=true; }    }}

Attack tower management and settings

Authors:liu yong//Email: <[email protected]>//qq:1931195500using unityengine;using System.Collections Namespace Raygame{public class Towermanager:monobehaviour {//Tower Point public gameobject[] towerpoints;//construction process picture public Sprite[] towerbuilding;//Blood Bar resource path string barpath = "Prefabs/ui/progressbar";//Tower time public float buildingtime = 1f;// Current Tower point Gameobject curtowerpoint = null;//current tower type int curtowerid;void awake () {//Traverse all tower points for (int i = 0; i < Towerpoints.len Gth i++) {gameobject tp = towerpoints [i];//Associated collision box TP. Addcomponent<circlecollider2d> ();//correlation script TP. Addcomponent<towerpoint> ();}} public void showbuilding (Gameobject _towerpoint,int towerid) {//remember tower point curtowerpoint = _towerpoint;//Remember tower type Curtowerid = towerid;//get render component Spriterenderer Sprender = _towerpoint.getcomponent<spriterenderer> ();// Change Sprite Picture sprender.sprite = towerbuilding [towerid-1];//Show Build Process showprogress (_towerpoint);} void ShowProgress (Gameobject towerpoint) {gameobject barobj = (gameobject) instantiate (Resources.Load (Barpath), vector3.zero,quaternion.identity);//progressbar component ProgressBar bar = barobj.getcomponent< Progressbar> ();//Mount Blood bar bar. Setbarparent (towerpoint.transform);//Activate progress bar. SetActive (true,buildingtime,gameobject);} public void Progressend () {Debug.Log ("End of Progress bar"); Buildtower (curtowerpoint,curtowerid,1);} <summary>///Tower///</summary>///<param name= "TowerPoint" > Tower point </param>///<param name= " Type "> Tower types </param>///<param name=" level "> Tower </param>public void Buildtower (gameobject Towerpoint,int Type,int level) {//Follow the rules stitching path string towerpath = "Prefabs/towers/tower" +type+ "/tower" +type+ "_" +level;d Ebug. Log ("Show path" +towerpath);//Instantiate Tower object Gameobject Tower = (gameobject) Instantiate (Resources.load (Towerpath), TowerPoint.transform.position, quaternion.identity); Tower TW = Tower. Addcomponent<tower> (); tw. Settowertype (type); tw. Towerinit ();//delete Tower point destroy (TowerPoint);}}

Uimanger Management and Settings

Authors:liu yong//Email: <[email protected]>//qq:1931195500using unityengine;using System.Collections ; using Unityengine.ui;namespace Raygame{public class Uimanager:monobehaviour {string Panelpath = "prefabs/ui/ Towerpanel ";//point to the currently open panel gameobject Curpanel = null;//point to the current tower point of the click Public gameobject curtowerpoint = Null;public Text Heartvalue;public text goldvalue;public text wavevalue;///<summary>///display panel at specified location//</summary>///< param name= "pos" > specified position </param>public void Showtowerpanel (Vector3 pos,gameobject towerpoint) {if (Curpanel! = NULL) {Destroy (curpanel);} Curpanel = (gameobject) Instantiate (Resources.load (Panelpath), pos,quaternion.identity);//record the currently clicked Tower point 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 ();}}}

In addition, it also needs a lot of dynamic pictures, path point settings, path selection. There is no way to demonstrate these kinds of binding work. Want complete file privately qq email me [email protected]

The components of the above map are already complete. Next comes the code for tower defense, buying towers, Monster moves, monsters being attacked and killing monsters to earn money, etc.

How to make tower defense game Code, operation, description--unity 5.3.5F1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.