///////////////////////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的几个开关变量,还有角色的移动,很清楚了吧,嘿嘿,还不错吧
就这样了,有不懂的地方或者实现有错误的请留言呢,我会一一回复的,希望大家互相学习,共同进步
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。