博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3D开发——LeRunning的人物角色信息的显示
阅读量:6840 次
发布时间:2019-06-26

本文共 9829 字,大约阅读时间需要 32 分钟。

///2015/08/22///

//by    xbw///

//环境 unity 4.6.1

有了人物购买系统要是没有人物信息的显示是不是感觉并没有什么卵用呢,这样呢,我决定做一个信息显示的模块。先看一下效果图,

这个图实现了什么呢。就是点击一下角色下方然后弹出信息框。而且人物模型动画移动到了右边,下边先来一段实现代码。

using UnityEngine;using System.Collections;public class renwuxixinUI : MonoBehaviour {	// Use this for initialization    public Texture2D bai;    public Texture2D hei;    public Texture2D hong;    public Texture2D ming;    public Texture2D jing;    public Texture2D lan;    public Texture2D img;    public int shu=-1;    public int shu2 = -1;	void Start () {		}		// Update is called once per frame	void Update () {        shu = quanju.flag2;        shu2 = quanju.flag;		}    void OnGUI()    {        if((shu==0||shu2==0)&&quanju.dang)        {            GUI.DrawTexture(new Rect(Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * 0.7f, Screen.height * 0.7f), bai);            if (GUI.Button(new Rect(Screen.width * 0.1f + Screen.width * 0.642f, Screen.height * 0.11f, Screen.width * 0.04f, Screen.width * 0.04f), ""))            {                quanju.dang = false; GameObject.FindGameObjectWithTag("Player").transform.Translate(Vector3.right * 5.0f);             }            GUI.DrawTexture(new Rect(Screen.width * 0.1f + Screen.width * 0.63f, Screen.height * 0.08f, Screen.width * 0.065f, Screen.width * 0.065f), img);        }        if ((shu == 5 || shu2 == 5) && quanju.dang)        {            GUI.DrawTexture(new Rect(Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * 0.7f, Screen.height * 0.7f), hei);            if (GUI.Button(new Rect(Screen.width * 0.1f + Screen.width * 0.642f, Screen.height * 0.11f, Screen.width * 0.04f, Screen.width * 0.04f), ""))            {                quanju.dang = false; GameObject.FindGameObjectWithTag("Player").transform.Translate(Vector3.right * 5.0f);            }            GUI.DrawTexture(new Rect(Screen.width * 0.1f + Screen.width * 0.63f, Screen.height * 0.08f, Screen.width * 0.065f, Screen.width * 0.065f), img);        }        if ((shu == 4 || shu2 == 4) && quanju.dang)        {            GUI.DrawTexture(new Rect(Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * 0.7f, Screen.height * 0.7f), hong);            if (GUI.Button(new Rect(Screen.width * 0.1f + Screen.width * 0.642f, Screen.height * 0.11f, Screen.width * 0.04f, Screen.width * 0.04f), ""))            {                quanju.dang = false; GameObject.FindGameObjectWithTag("Player").transform.Translate(Vector3.right * 5.0f);            }            GUI.DrawTexture(new Rect(Screen.width * 0.1f + Screen.width * 0.63f, Screen.height * 0.08f, Screen.width * 0.065f, Screen.width * 0.065f), img);        }        if ((shu == 3 || shu2 == 3) && quanju.dang)        {            GUI.DrawTexture(new Rect(Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * 0.7f, Screen.height * 0.7f), ming);            if (GUI.Button(new Rect(Screen.width * 0.1f + Screen.width * 0.642f, Screen.height * 0.11f, Screen.width * 0.04f, Screen.width * 0.04f), ""))            {                quanju.dang = false; GameObject.FindGameObjectWithTag("Player").transform.Translate(Vector3.right * 5.0f);            }            GUI.DrawTexture(new Rect(Screen.width * 0.1f + Screen.width * 0.63f, Screen.height * 0.08f, Screen.width * 0.065f, Screen.width * 0.065f), img);        }        if ((shu == 2 || shu2 ==2) && quanju.dang)        {            GUI.DrawTexture(new Rect(Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * 0.7f, Screen.height * 0.7f), jing);            if (GUI.Button(new Rect(Screen.width * 0.1f + Screen.width * 0.642f, Screen.height * 0.11f, Screen.width * 0.04f, Screen.width * 0.04f), ""))            {                quanju.dang = false; GameObject.FindGameObjectWithTag("Player").transform.Translate(Vector3.right * 5.0f);            }            GUI.DrawTexture(new Rect(Screen.width * 0.1f + Screen.width * 0.63f, Screen.height * 0.08f, Screen.width * 0.065f, Screen.width * 0.065f), img);        }        if ((shu == 1 || shu2 == 1) && quanju.dang)        {            GUI.DrawTexture(new Rect(Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * 0.7f, Screen.height * 0.7f), lan);            if (GUI.Button(new Rect(Screen.width * 0.1f + Screen.width * 0.642f, Screen.height * 0.11f, Screen.width * 0.04f, Screen.width * 0.04f), ""))            {                quanju.dang = false; GameObject.FindGameObjectWithTag("Player").transform.Translate(Vector3.right * 5.0f);            }            GUI.DrawTexture(new Rect(Screen.width * 0.1f + Screen.width * 0.63f, Screen.height * 0.08f, Screen.width * 0.065f, Screen.width * 0.065f), img);        }    }}

实现并不困难,可是要想一点bug都没有非常费劲,调试了不知道几十遍,没一点都要考虑到。由于我们的信息显示了人物已购买跟人物未购买,所以必须调用数据库。究竟有没有购买。上边一段代买仅仅是显示信息UI的。至于那些推断条件。我们用在了别处,看到都是用了全局变量了吧,在之前的文章中。characterselect.cs脚本中,我们有这样一段代码;

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);                    }                }            }

按下button的几个开关变量,还有角色的移动,非常清楚了吧,嘿嘿,还不错吧

就这样了,有不懂的地方或者实现有错误的请留言呢,我会一一回复的,希望大家互相学习,共同进步

你可能感兴趣的文章
1 张图秒懂 Nova 16 种操作 - 每天5分钟玩转 OpenStack(44)
查看>>
hostPath Volume - 每天5分钟玩转 Docker 容器技术(148)
查看>>
Liunx 终端 bash 提示符 修改 DIY
查看>>
jQuery 判断图片加载完毕例子
查看>>
Freemarker生成PDF
查看>>
IDC评述网:12月上旬全球域名解析服务商Top15
查看>>
Tomcat认证授权与简单的SSO
查看>>
Redis未授权访问及安全组漏洞招致kerberods来挖矿
查看>>
ip 归属地查询
查看>>
php xampp redis扩展
查看>>
apache 403
查看>>
scanf函数
查看>>
阿里云不做SaaS、要练好内功被集成,发布SaaS加速器
查看>>
双系统如何正确的删除Ubuntu
查看>>
大话it职场之经历风雨是否能见彩虹
查看>>
二叉树的遍历与还原
查看>>
我的友情链接
查看>>
二进制安装MariaDB 5.5.36
查看>>
我的友情链接
查看>>
DNS 安装与解析
查看>>