U3d的道具界面的制作

来源:互联网 发布:Java生成apk 编辑:程序博客网 时间:2024/04/28 11:29

因为本游戏涉及到道具和图鉴等系统,在进入道具和图鉴系统时,会出现类似于下图的界面(图片来源于网络,仅供示意)


这个系统用到的就是我在上一篇文章中提到的事件监听器UIEventListener。用这个方法的好处在于,可以实现程序和美工同时进行,方便又快捷。(由于在本项目中本人在UI界面方向上既是程序员又是美工,所以在整体美术风格还没有确定下来的时候,可以先着手进行程序代码方面的设计,非常好用。)


本次编写实现了一个拖拽物品的功能,用鼠标可以对物品进行拖拽,同时,将鼠标放在物品上,可以显示物品的具体文字解释说明。

本次测试涉及到了三个脚本,分别为:

1、用于写拖拽的脚本

2、用于写关于物品介绍窗口的弹出的脚本

3.、用于控制使用关闭的脚本


用于控制拖拽的脚本:

using UnityEngine;using System.Collections;public class PackageControl : MonoBehaviour {    public GameObject Used;//公开声明一个“已经使用”的物体    void Start () {        UIEventListener listener = UIEventListener.Get (gameObject);//事件监听器        listener.onHover += Hover;//鼠标移上图标的方法        listener.onDrag += Drag;//鼠标拖拽的方法        listener.onPress += Press;//鼠标按下的方法        listener.onDoubleClick += Doubleclick;//双击图标的方法    }    //鼠标移上图标,鼠标在图标上则图标大小为(1, 1, 1),移开则为(0.76f, 0.76f, 0.76f)    private void Hover(GameObject obj,bool isOver)    {        if (isOver) {            this.transform.localScale = new Vector3 (1, 1, 1);            } else {            this.transform.localScale = new Vector3 (0.76f, 0.76f, 0.76f);            }            }//鼠标拖动图标,delta为图标的位移量,拖动后的图标位置为: 图标初始位置+位移量    private void Drag(GameObject obj,Vector2 delta)    {        this.transform.localPosition =             new Vector3(transform.localPosition.x + delta.x,                         transform.localPosition.y + delta.y,                         transform.localPosition.z);    }//鼠标按下抬起,鼠标抬起时图标大小变为初始大小    private void Press(GameObject obj,bool isDown)    {        if (isDown) {        } else {            this.transform.localScale = new Vector3 (0.76f, 0.76f, 0.76f);        }    }//双击图标,表示已经使用该技能的窗口显示出来    private void Doubleclick(GameObject obj)    {        Used.SetActive (true);    }    void Update () {            }}


用于控制物品介绍窗口弹出的脚本:


using UnityEngine;using System.Collections;public class TooltipsControl : MonoBehaviour {//公开定义图标的介绍窗口    public GameObject tips1;    public GameObject tips4;    public GameObject tips7;//公开定义需要监听的对象    public GameObject Items1;    public GameObject Items4;    public GameObject Items7;    void Start () {        UIEventListener listener = UIEventListener.Get (Items1);        listener.onHover += Hover1;//鼠标移上去的方法        listener = UIEventListener.Get (Items4);        listener.onHover += Hover4;//鼠标移上去的方法        listener = UIEventListener.Get (Items7);        listener.onHover += Hover7;//鼠标移上去的方法    }        private void Hover1(GameObject obj,bool isOver)    {        if (isOver) {            StartCoroutine(Show1());//鼠标移上图标,调用协同方法        } else {        }    }    private void Hover4(GameObject obj,bool isOver)    {        if (isOver) {            StartCoroutine(Show4());        } else {                    }    }    private void Hover7(GameObject obj,bool isOver)    {        if (isOver) {            StartCoroutine(Show7());        } else {                    }    }    void Update () {    }//协程方法控制介绍的窗口显示和隐藏    IEnumerator Show1()    {        yield return new WaitForSeconds (1.0f);//鼠标移上图标等待1秒显示介绍窗口        tips1.SetActive (true);        tips4.SetActive (false);        tips7.SetActive (false);        yield return new WaitForSeconds (1.5f);//介绍窗口显示1.5秒后隐藏        tips1.SetActive (false);        tips4.SetActive (false);        tips7.SetActive (false);    }    IEnumerator Show4()    {        yield return new WaitForSeconds (1.0f);        tips4.SetActive (true);        tips1.SetActive (false);        tips7.SetActive (false);        yield return new WaitForSeconds (1.5f);        tips4.SetActive (false);        tips1.SetActive (false);        tips7.SetActive (false);    }    IEnumerator Show7()    {        yield return new WaitForSeconds (1.0f);        tips7.SetActive (true);        tips4.SetActive (false);        tips1.SetActive (false);        yield return new WaitForSeconds (1.5f);        tips7.SetActive (false);        tips4.SetActive (false);        tips1.SetActive (false);    }}

用于控制使用关闭的脚本:

using UnityEngine;using System.Collections;public class UsedcloseControl : MonoBehaviour {    public GameObject close;    void Start () {        UIEventListener listener = UIEventListener.Get (gameObject);        listener.onClick += Click;//单击按钮方法    }        private void Click(GameObject obj)    {        close.SetActive (false);//关闭提示已使用的窗口    }    void Update () {        }}


根据这三个脚本,可以快捷地实现道具和图鉴窗口。接下来要做的就是将具体的图标绘制完成,依附到相应的窗口上去。

原创粉丝点击