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

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

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

/////////////////环境 unity4.6.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实现了对角色是否拥有,只用拥有的角色才可以选择它上场,下节再细说这个购买的系统实现

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-29 07:50:33

Unity3D开发——LeRunning角色选择界面制作的相关文章

cocos2dx实现环形角色选择界面

数据结构采用双向循环链表.由候选角色组成链表的节点,将链表中的节点在屏幕上按照椭圆分布. .h: 1 //链表节点——候选角色 2 class CandidateRole :public CCNode{ 3 public: 4 CandidateRole(); 5 ~CandidateRole(); 6 virtual bool init(const char* name); 7 static CandidateRole* create(const char* name); 8 Candidat

DoTween应用在炉石传说,从开始界面到角色选择界面动画切换

动画播放步骤前面有... 然后在moveStart脚本里面添加 public DOTweenAnimation Hero_SelectPage;    public bool isCanShowSelectRole = false;//是否进入角色选择界面 public DOTweenAnimation WelcomPage;    public bool isWelcomFinished = false; 在inspector面板上进行拖拽赋值 “Welcom Page”是开始界面的logo

cocos2dx3.2格斗类游戏人物选择界面

先看最终实现的效果图: 创建以人物选择场景 ChooseHero.h #ifndef __CHOOSE_HERO_H__ #define __CHOOSE_HERO_H__ #include "cocos2d.h" #include "ui/CocosGUI.h" #include "cocos-ext.h" #include "cocostudio/CocoStudio.h" using namespace cocos2d:

Unity3D游戏开发从零单排(四) - 制作一个iOS游戏

提要 此篇是一个国外教程的翻译,虽然有点老,但是适合新手入门.自己去写代码,debug,布置场景,可以收获到很多.游戏邦上已经有前面两部分的译文,这里翻译的是游戏的最后一个部分. 欢迎回来 在第一篇中,我们学会了怎么在Unity中搭建游戏的场景,并且设置模型的物理属性. 在第二篇中,我们学会了怎么在unity中使用脚本,并且创建了大部分的游戏逻辑,包括投球和得分! 在这最后一节中,我们将会为用户创建一个菜单系统,并且和GameController进行交互,我们开始吧. 在设备上测试 到目前为止,

CR开发笔记-3GUI界面制作

今天是第三篇 今天主要是做UI界面,非常容易,我也不用NGUI直接上好了! 直接上代码! using UnityEngine; using System.Collections; public class Startmenu : MonoBehaviour { float fullWidth; float fullHeight; Rect rtFullscreen; Rect rtBeginBtn; //窗口定义 //这个玩意的效果就是只让一个窗口存在 bool WindowsExist; //

炉石传说之角色选择和对战界面

在建立NGUI的基础之上: 1.在UIRoot上建立一个空物体,并取名为welcome,将UIRoot下的几个子物体移动到welcome下,进行统一管理. 2.选择bg_selectrole图片作为背景选择界面,将此图片加入到Altas. 3.在UIRoot下创建一个精灵,并且将bg_selectrol选择. 4.在背景图片下选择相应的9个英雄.将9个英雄的图标加入到Altas中. 5.在背景下建立一个sprite-children.将hero1做成一个预制.将图片拖到prefab的文件夹中就可

Unity3D开发基础组件提取总结

在游戏开发过程中,除了逻辑功能的开发之外,还有非常多基础的模块.这些模块,对大部分手机网络游戏来说都是一样的.所以,在上个游戏已经上线运营大半年之际,我认为有必要将这些模块整理出来.让后面其它游戏的开发,能够不用从0開始.该游戏client採用Unity3D开发,那么我们这里抽取的基础组件也就是针对Unity3D引擎来的. 简单的梳理之后,我发现主要有下面一些模块,能够独立抽取出来,当然各个模块之间也有一个依赖关系.比方日志模块,差点儿是全部模块都须要依赖的. 1.日志模块 游戏开发中,我们避免

unity3d开发实战《啪啪三国》技术详解!

去年11月,上海火溶网络CEO王伟峰以其第一款3d手游产品<啪啪三国>为例,着重讲解了unity3D手机网游开发的经验,其中涉及了团队组成.人员要求.常见的unity3d开发遇到的坑及解决办法.在演讲中,王伟峰也贡献了<啪啪三国>开发过程中总结的各种经验,从优化.插件库.服务器架构.SDK等很多细节进行了讲解.值得一说的是,王伟峰现场演讲十分幽默,冷笑话段子不断爆出,让在场观众在连续的笑声中听完这个特别的技术演讲. <ignore_js_op> 以下是王伟峰现场演讲实录

Unity3D 研究院之IOS高级界面发送消息与Unity3D消息的接收

今天和盆友们讨论IOS的高级界面与unity3d游戏引擎的交互,这个在开发中是非常重要的,unity3d 毕竟是一个面向多平台的一个游戏引擎,它不可能全部为IOS 考虑的面面俱到,引擎中也不存在针对IOS的高级界面的控件的使用. 本例实现游戏背景是Unity3D 的游戏世界,前面添加4个IOS的高级界面的按钮,并且点击这些按钮可以将消息传递给背景的Unity3D ,让它做一些事情. 上一章介绍了触摸IOS屏幕 移动摄像机的位置,下面有盆友问我说他不想移动摄像机的位置,就想移动物体的位置,我在这里