Unity3d Development--lerunning Role Selection Interface production

Source: Internet
Author: User

2015/08/20///////////////////

by xbw/////////////////////////

Environmental unity4.6.1/////////////

Today's exquisite Cup digital media creative contest is almost over, did one months also accumulated a lot of experience, write a tutorial bar, about the character selection of the interface

Check it out first.

UI content a lot of it, now let's talk about this character switch,

This is the use of precast, the need to add the role to the Prefabs folder, no words in the assets to create a new, and then drag the characters into the

So the preform initially good, then the code, first put the code, and then download the detailed explanation

Using unityengine;using system.collections;public class characterselect:monobehaviour{//public int flag=1;    Public gameobject[] character;    private int index = 0;    Public texture2d selent1;    Public texture2d Selent2;    Public Guiskin GUIskin2;    Private gameobject[] charactershow;    private int choose1 = 0;    private int choose2 = 0;    Public Guiskin Guiskin;    Public AudioClip Crashsound;    Public AudioClip CrashSound2;    Public AudioClip CrashSound3;    Public AudioClip CrashSound4;    Public AudioClip CrashSound5;    Public AudioClip CrashSound6;    Public AudioClip CrashSound7;    Public texture2d Jinbi;    Public Texture2d Luntai;    Private Audiosource _audiosource; void Start () {charactershow = new gameobject[character.        Length];    Instantiationcharacter ();        } void Awake () {_audiosource = this.gameobject.addcomponent<audiosource> ();        _audiosource.loop = false;        _audiosource.volume = 1.0f; _audiosourCe.pitch = 1.0f;            } void Update () {if (choose1==1) {index + = 1; if (index >= character.            Length) {index = 0;            } characterchange (index);        choose1 = 0;            } if (choose2==1) {index-= 1; if (Index < 0) {index = character.            Length-1;            } characterchange (index);        Choose2 = 0; }} void Characterchange (int indexxxx) {for (int i = 0; i < character. Length; i++) {if (i = = indexxxx) {//show charactershow[i].            SetActive (TRUE); } else//unvisible//charactershow[i].                SetActive (FALSE);        Charactershow[i].active = false; }} void Instantiationcharacter () {for (int i = 0; i < character. Length; i++) {CharActershow[i] = (gameobject) (Instantiate (Character[i], transform.position, transform.rotation));    } characterchange (index);    }//ienumerator Example ()//{//print (time.time);    Yield return new Waitforseconds (10);    Print (time.time); } void Ongui () {if (GUI.        button (new Rect (Screen.width * 0.389f, Screen.height * 0.056f, Screen.width * 0.06f, Screen.height * 0.085f), ""))            {_audiosource.playoneshot (crashsound);            Application.loadlevel ("Shangdian");        Quanju.shangdian = 1; } GUI.        Drawtexture (New Rect (Screen.width * 0.0f, Screen.height * 0.0f-50, Screen.width * 0.5f, Screen.height * 0.6f), JINBI); if (GUI.        button (new Rect (Screen.width * 0.889f, Screen.height * 0.056f, Screen.width * 0.06f, Screen.height * 0.085f), ""))            {_audiosource.playoneshot (crashsound);            Application.loadlevel ("Shangdian");        Quanju.shangdian = 2; } GUI. DrAwtexture (New Rect (Screen.width * 0.5f, Screen.height * 0.0f-50, Screen.width * 0.5f, Screen.height * 0.6f), Luntai);        Gui.skin = Guiskin;        Guistyle BB1 = new Guistyle ();        Bb1.fontsize = 40;        Bb1.normal.textColor = new Color (255, 255, 255); Quanju.        Luntotal = Playerprefs.getint ("Luntai"); Quanju.        Jintotal = Playerprefs.getint ("Jinbi"); Gui. Label (New Rect (Screen.width * 0.65f, Screen.height * 0.07f, 0, 0), Quanju.        Luntotal.tostring (), BB1); Gui. Label (New Rect (Screen.width * 0.15f, Screen.height * 0.07f, 0, 0), Quanju.            Jintotal.tostring (), BB1);            Gui.skin = GUIskin2; if (GUI.                 button (new Rect (Screen.width * 0.37f, Screen.height * 0.5f, Screen.width * 0.05f, Screen.width * 0.05f), "")) {                _audiosource.playoneshot (Crashsound);                Startcoroutine (Example ());                if (index==0) _audiosource.playoneshot (CRASHSOUND2);                    if (index = = 5)_audiosource.playoneshot (CRASHSOUND3);                if (index = = 4) _audiosource.playoneshot (CRASHSOUND4);                if (index = = 3) _audiosource.playoneshot (CRASHSOUND5);                if (index = = 2) _audiosource.playoneshot (CRASHSOUND6);                if (index = = 1) _audiosource.playoneshot (CRASHSOUND7);            CHOOSE1 = 1; } GUI.            Drawtexture (New Rect (Screen.width * 0.37f, Screen.height * 0.5f, Screen.width * 0.05f, Screen.width * 0.05f), selent1);            Gui.skin = GUIskin2; if (GUI.                 button (new Rect (Screen.width * 0.57f, Screen.height * 0.5f, Screen.width * 0.05f, Screen.width * 0.05f), "")) {                _audiosource.playoneshot (Crashsound);                Startcoroutine (Example ());                if (index = = 0) _audiosource.playoneshot (CrashSound2); if (index = = 5) _audiosource.playonEshot (CRASHSOUND3);                if (index = = 4) _audiosource.playoneshot (CRASHSOUND4);                if (index = = 3) _audiosource.playoneshot (CRASHSOUND5);                if (index = = 2) _audiosource.playoneshot (CRASHSOUND6);                if (index = = 1) _audiosource.playoneshot (CRASHSOUND7);            Choose2 = 1; } GUI.            Drawtexture (New Rect (Screen.width * 0.57f, Screen.height * 0.5f, Screen.width * 0.05f, Screen.width * 0.05f), selent2);            Playerprefs.setint ("Renwu1", QUANJU.RENWU1);            QUANJU.RENWU1 = Playerprefs.getint ("renwu1");            Playerprefs.setint ("Renwu2", QUANJU.RENWU2);            QUANJU.RENWU2 = Playerprefs.getint ("Renwu2");            Playerprefs.setint ("Renwu3", QUANJU.RENWU3);            QUANJU.RENWU3 = Playerprefs.getint ("Renwu3");            Playerprefs.setint ("Renwu4", quanju.renwu4); Quanju.renwu4 = Playerprefs.getint("Renwu4");            Playerprefs.setint ("Renwu5", Quanju.renwu5);            Quanju.renwu5 = Playerprefs.getint ("Renwu5");            Playerprefs.setint ("Renwu6", Quanju.renwu6);            Quanju.renwu6 = Playerprefs.getint ("Renwu6"); if (GUI.            button (new Rect (Screen.width * 0.8f, Screen.height * 0.8f, Screen.width * 0.15f, Screen.height * 0.1f), "buy") {if (index = = 1 && quanju.renwu1 = = 0 && quanju. Jintotal >= 10000) {Quanju. Jintotal = Quanju.                    JinTotal-10000; Playerprefs.setint ("Jinbi", Quanju.                    Jintotal);                    QUANJU.RENWU1 = 1;                Playerprefs.setint ("Renwu1", QUANJU.RENWU1); if (index = = 2 && quanju.renwu2 = = 0 && quanju. Jintotal >= 20000) {Quanju. Jintotal = Quanju.                    JinTotal-20000; Playerprefs.setint ("Jinbi", Quanju.         Jintotal);           QUANJU.RENWU2 = 1;                Playerprefs.setint ("Renwu2", QUANJU.RENWU2); if (index = = 3 && Quanju.renwu3 = = 0 && quanju. Jintotal >= 30000) {Quanju. Jintotal = Quanju.                    JinTotal-30000; Playerprefs.setint ("Jinbi", Quanju.                    Jintotal);                    QUANJU.RENWU3 = 1;                Playerprefs.setint ("Renwu3", QUANJU.RENWU3); if (index = = 4 && Quanju.renwu4 = = 0 && quanju. Jintotal >= 40000) {Quanju. Jintotal = Quanju.                    JinTotal-40000; Playerprefs.setint ("Jinbi", Quanju.                    Jintotal);                    Quanju.renwu4 = 1;                Playerprefs.setint ("Renwu4", quanju.renwu4); if (index = = 5 && Quanju.renwu5 = = 0 && quanju. Jintotal >= 50000) {Quanju. Jintotal = Quanju. JinTotal-50000; Playerprefs.setint ("Jinbi", Quanju.                    Jintotal);                    Quanju.renwu5 = 1;                Playerprefs.setint ("Renwu5", Quanju.renwu5); if (index = = 0 && Quanju.renwu6 = = 0 && quanju. Jintotal >=) {Quanju. Jintotal = Quanju.                    JinTotal-5000; Playerprefs.setint ("Jinbi", Quanju.                    Jintotal);                    Quanju.renwu6 = 1;                Playerprefs.setint ("Renwu6", Quanju.renwu6); }} if (index = = 0) {if (GUI. button (new Rect (Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "Space small white" + "\ n" + " 5000$ "+" \ n "+" in-purchased ") {Quanju.dang = true; quanju.flag = 0; quanju.flag2 = 0; Gameobject.findgameobjectwithtag ("Player"). Transform. Translate (Vector3.left * 5.0f); }                           }            if (index = = 5) {if (Quanju.renwu5 = = 1) {if (GU  I.button (New Rect (Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "Ninja Little Black" + "\ n" +  "50000$" + "\ n" + "purchased") {Quanju.dang = true; quanju.flag = 5; quanju.flag2 = 5; Gameobject.findgameobjectwithtag ("Player"). Transform.                    Translate (Vector3.left * 5.0f); }} else {if (GUI. button (new Rect (Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "Ninja Little Black" + "\ n" + " 50000$ "+" \ n "+" not purchased ") {Quanju.dang = true; quanju.flag2 = 5; Gameobject.findgameobjectwithtag ("Player"). Transform.                    Translate (Vector3.left * 5.0f); }}} if (index = = 4) {if (Quanju.renwu4 = = 1)                {if (GUI. button (new Rect (Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "Kung Fu Little Red" + "\ n" + " 40000$ "+" \ n "+" purchased ") {Quanju.dang = true; quanju.flag = 4; quanju.flag2 = 4 ; Gameobject.findgameobjectwithtag ("Player"). Transform.                    Translate (Vector3.left * 5.0f); }} else {if (GUI. button (new Rect (Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "Kung Fu Little Red" + "\ n" + " 40000$ "+" \ n "+" not purchased ") {Quanju.dang = true; quanju.flag2 = 4; Gameobject.findgameobjectwithtag ("Player"). Transform.                    Translate (Vector3.left * 5.0f);                }}} if (index = = 3) {if (quanju.renwu3 = = 1) {if (GUI. Button (New Rect (Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "Ming" + "\ n" + "30000$" + "\ n" + "purchased") {Quanju.dang = true; quanju.flag = 3; quanju.flag2 = 3; Gameobject.findgameobjectwithtag ("Player"). Transform.                    Translate (Vector3.left * 5.0f); }} else {if (GUI. button (new Rect (Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "clearly" + "\ n" + "30 000$ "+" \ n "+" not purchased ") {Quanju.dang = true; quanju.flag2 = 3; Gameobject.findgameobjectwithtag ("Player"). Transform.                    Translate (Vector3.left * 5.0f);                }}} if (index = = 2) {if (quanju.renwu2 = = 1) {if (GUI. button (new Rect (Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "quiet" + "\ n"+ "20000$" + "\ n" + "purchased") {Quanju.dang = true; Quanju.flag = 2; quanju.flag2 = 2; Gameobject.findgameobjectwithtag ("Player"). Transform.                    Translate (Vector3.left * 5.0f); }} else {if (GUI. button (new Rect (Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "quiet" + "\ n" + "20 000$ "+" \ n "+" not purchased ") {Quanju.dang = true; quanju.flag2 = 2; Gameobject.findgameobjectwithtag ("Player"). Transform.                    Translate (Vector3.left * 5.0f);                }}} if (index = = 1) {if (quanju.renwu1 = = 1) {if (GUI. button (new Rect (Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "Interpol small Blue" + "\ n" + " 10000$ "+" \ n "+" purchased ")) {Quanju.daNg = true; Quanju.flag = 1; Quanju.flag2 = 1; Gameobject.findgameobjectwithtag ("Player"). Transform.                    Translate (Vector3.left * 5.0f); }} else {if (GUI. button (new Rect (Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "Interpol small Blue" + "\ n" + " 10000$ "+" \ n "+" not purchased ") {Quanju.dang = true; quanju.flag2 = 1; Gameobject.findgameobjectwithtag ("Player"). Transform.                    Translate (Vector3.left * 5.0f); }}} if (GUI.            button (new Rect (Screen.width * 0.2f, Screen.height * 0.8f, Screen.width * 0.15f, Screen.height * 0.1f), "back"))            {Application.loadlevel ("First");} if (GUI.            button (new Rect (Screen.width * 0.64f, Screen.height * 0.8f, Screen.width * 0.15f, Screen.height * 0.1f), "Enter the game") {if (index = = 1 && quanju.renwu1==1) {Quanju.flag = 1;            } if (index = = 2&&quanju.renwu2==1) {Quanju.flag = 2;            } if (index = = 3 && Quanju.renwu3 = = 1) {Quanju.flag = 3;            } if (index = = 4 && Quanju.renwu4 = = 1) {Quanju.flag = 4;            } if (index = = 5 && Quanju.renwu5 = = 1) {Quanju.flag = 5;            } if (index = = 0 && Quanju.renwu6 = = 1) {quanju.flag = 0; } application.loadlevel ("Loading4"); }    }}

In fact, the role of choice is just a small part of the front, the back of the Ongui is not, of course, the GUI contains all the essence of the role selection, the role of choice, is to open a Gameobject array, in start to get the size of the array, is dragged into the array of prefabs number , and then use

Instantiate (Character[i], transform.position, transform.rotation); Generate this array of any one, his left and right buttons to switch the GUI is actually changed the value of I, and then loop display, Is easy to achieve, the bottom of the Ongui to achieve the role of the possession, only with the role to choose it to play, the next section to elaborate on the purchase of the system implementation

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Unity3d Development--lerunning Role Selection Interface production

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.