Unity3D开发——LeRunning角色选择界面制作

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

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

/////////////////环境 unity4.6.1/////////////

今天的玲珑杯数字媒体创意大赛算是差不多结束了,做了一个多月的也积累了不少经验,写一下教程吧,关于这个人物角色选择的界面

先看一下效果图

Unity3D开发——LeRunning角色选择界面制作_第1张图片

UI内容不少吧,现在先说一下这个人物切换,

这个呢就用预制体了,把需要的角色添加到prefabs文件夹,没有的话就在Assets中新建一个,然后把角色拖进去,

这样预制体初步好了,接着就是代码了,先贴一下代码,然后载详细解释

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), "购买"))
            {
                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 >= 5000)
                {
                    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), "太空小白" + "\n" + "5000$" + "\n" + "已内购"))
                    { 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 (GUI.Button(new Rect(Screen.width * 0.42f, Screen.height * 0.6f, Screen.width * 0.15f, Screen.height * 0.3f), "忍者小黑" + "\n" + "50000$" + "\n" + "已购买"))
                    {
                        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), "忍者小黑" + "\n" + "50000$" + "\n" + "未购买"))
                    {
                        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), "功夫小红" + "\n" + "40000$" + "\n" + "已购买"))
                    {
                        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), "功夫小红" + "\n" + "40000$" + "\n" + "未购买"))
                    {
                        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), "明明" + "\n" + "30000$" + "\n" + "已购买"))
                    {
                        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), "明明" + "\n" + "30000$" + "\n" + "未购买"))
                    {
                        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), "静静" + "\n" + "20000$" + "\n" + "已购买"))
                    {
                        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), "静静" + "\n" + "20000$" + "\n" + "未购买"))
                    {
                        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), "刑警小蓝" + "\n" + "10000$" + "\n" + "已购买"))
                    {
                        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), "刑警小蓝" + "\n" + "10000$" + "\n" + "未购买"))
                    {
                        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), "返回"))
            { Application.LoadLevel("first"); }
            if (GUI.Button(new Rect(Screen.width * 0.64f, Screen.height * 0.8f, Screen.width * 0.15f, Screen.height * 0.1f), "进入游戏"))
            {
                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"); }
    }
}

其实角色选择只是前边的一小部分,后边的OnGUI就不是了,当然GUI中包含了角色选择的所有精华,这个角色的选择,就是开一个GameObject的数组,在Start中获取数组的大小,就是拖进这个数组中的prefabs的个数,再用

Instantiate(character[i], transform.position, transform.rotation);生成这个数组中的任意一个,他的左右按钮切换的GUI其实就是改变的i的值,然后循环显示,很容易实现,下边的ONGUI实现了对角色是否拥有,只用拥有的角色才可以选择它上场,下节再细说这个购买的系统实现

你可能感兴趣的:(Unity3D开发——LeRunning角色选择界面制作)