SendMessageUpwards定义简单按钮(Unity3D开发之十)
猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢!
SendMessageUpwards是朝物体和上级父物体发送信息。也可以用来制作按钮。
下面就是一个简单的GUI交互模式。
一、GUI的接口事件处理类
GUIInterface.cs
using UnityEngine;
using System.Collections;
public class GUIInterface : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void onTouchStartGame()
{
Debug.Log("onTouchStartGame->");
}
}
按钮被点击之后调用的handler方法。
MenuItem.cs
using UnityEngine;
using System.Collections;
// A simple menu item. This simply specifies the handler function to call when the item is clicked/tapped
[RequireComponent(typeof(Collider2D))]
public class MenuItem : MonoBehaviour
{
public string handler; // the name of the handler function to be called when this item is tapped
public string sfxName; // the name of a sound clip to play when this item is tapped
}
1、在你的新场景中,创建一个Object对象命名为GUI;
2、创建一个3D Text对象,命名为Start Game;
3、将2步中的Start Game对象拖入1步中的GUI,作为其子对象;
4、将2步中的Start Game添加脚本MenuItem.cs, 添加Box Collider 2D碰撞盒,并勾中Is Trigger。
5、将1步中的GUI对象的Layer修改为GUI
二、输入管理器
InputManager.cs
using UnityEngine;
using System.Collections;
public class InputManager : MonoBehaviour {
public LayerMask GUILayerMask;
public static InputManager Instance;
void Awake ()
{
// check there isn't more than one instance of the InputManager in the scene
if(Instance != null)
{
Debug.LogError("More than one InputManager found in the scene");
return;
}
// set the global instance
Instance = this;
}
// Use this for initialization
void Start () {
}
private void UpdateInput()
{
bool tapped = false;
Vector3 tapPos = Vector3.zero;
// check for mouse input
if(Input.GetMouseButtonDown(0))
{
tapped = true;
tapPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
// check for touch input
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
tapped = true;
tapPos = Camera.main.ScreenToWorldPoint(touch.position);
}
}
// respond to any mouse-down or touch events
if(tapped)
{
// check if there's any gui elements at the tap position
RaycastHit2D hit = Physics2D.Raycast(tapPos, Vector2.zero, 1.0f, GUILayerMask);
if (hit.collider != null)
{
MenuItem menuItem = hit.collider.gameObject.GetComponent<MenuItem>();
if(menuItem != null)
{
if(menuItem.sfxName != "")
{
// PlaySfx(menuItem.sfxName)
}
menuItem.SendMessageUpwards(menuItem.handler);
}
}
}
}
void Update ()
{
UpdateInput();
}
}
1、创建Object,命名InputManager,添加脚本InputManager.cs
2、修改属性InputManager GUILayer Mask属性为GUI。
ok。现在完成了。
思路:
1、InputManager负责获取手指点击和GUI层对象的碰撞,并检测碰撞体是否含有MenuItem脚本,如果有,则发送MenuItem的handler函数消息。
2、GUIInterface则注册并接受所有GUI层的Item的消息。统一处理交互事件。
SendMessageUpwards定义简单按钮(Unity3D开发之十)的更多相关文章
- 如何修改新建脚本模板-ScriptTemplates(Unity3D开发之十五)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/44957631 ...
- uGUI使用代码动态添加Button.OnClick()事件(Unity3D开发之十二)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/42705885 ...
- Orientation Auto Rotation旋转屏幕crash问题(Unity3D开发之十四)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/44133127 ...
- 自动生成材质Material(Unity3D开发之十九)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46854411 ...
- 自己主动生成材质Material(Unity3D开发之十九)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46854411 ...
- 2DSprite添加Light照射(Unity3D开发之十六)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/45534245 ...
- 分别修改Cube每个面的贴图UV(Unity3D开发之十八)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46611169 ...
- 分别改动Cube每一个面的贴图UV(Unity3D开发之十八)
猴子原创.欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46611169 ...
- 跟我从零基础学习Unity3D开发--NGUI入门基础
英雄联盟(撸啊撸) QQ飞车 魔兽世界等等相信大家都玩过游戏吧,玩过那UI知道是什么吧?UI可能说得有点专业的话那么游戏中那些属性面板例如: 现在对UI有一定认识了吧!回想一下您玩过的游戏就一定知道什 ...
随机推荐
- 解决ansible首次连接host服务器需验证问题
问题描述: [root@iZm5e79rtwsq2hm57teyk5Z ansible]# ansible aofeng -f 5 -m ping 47.93.18.191 | FAILED! =&g ...
- javascrpt_数组学习
1.构造函数 var arr = new Array(); Array 构造函数有一个很大的缺陷,就是不同的参数,会导致行为不一致. 因此,不建议使用它生成新数组,直接使用字面量是最好的做法. 2.静 ...
- 手写JAVA虚拟机(三)——搜索class文件并读出内容
查看手写JAVA虚拟机系列可以进我的博客园主页查看. 前面我们介绍了准备工作以及命令行的编写.既然我们的任务实现命令行中的java命令,同时我们知道java命令是将class文件(字节码)转换成机器码 ...
- 罗列Linux发行版的基础目录名称,命令法则和功能
罗列Linux发行版的基础目录名称命名法则及功用规定 目录描述 /主层次 的根,也是整个文件系统层次结构的根目录 /bin存放在单用户模式可用的必要命令二进制文件,所有用户都可用,如 cat.ls.c ...
- Java基础学习(1)——反射
反射就是把Java类中的各种成分映射成相应的Java类(主要用于框架开发) 反射的基石–>Class类 Java程序中的各个类属于同一事物,描述这类事务的Java类名就是Class. Class ...
- HTML标签部分(块级/行级)
一.基本块级标签 1.HTML标签的分类: a.块级标签:显示为块状,独占一行,自动换行. b.行级标签:在一行中,从左往右依次排列,不会自动换行. 2.h标签(标题标签) h标签 ...
- linux系统性能监控--网络利用率
Linux中提供了许多有助于评估各种 Linux网络性能的监视工具,其中一些监视工具也可用于解决网络问题以及监视性能. Linux内核为用户提供了大量的网络系统信息,这有助于监视网络的健康状态并检测在 ...
- Matlab—regexp正则表达式
原文转自:http://blog.csdn.net/yf210yf/article/details/42421523 关于正则表达式的基本知识 正则表达式就是一个表达式(也是一串字符),它定义了某种字 ...
- tomcat内存溢出解决,java.lang.OutOfMemoryError: PermGen space
今天遇到了一个java.lang.OutOfMemoryError: PermGen space异常问题,一直解决不了,根据网上修改了tomcat的配置文件,但是还是解决不了,最后是通过如下方式解决的 ...
- Linux-2.6.25 TCPIP函数调用大致流程
插口层系统调用send sys_send sys_sendtosendto sys_sendto sock_sendmsgsendmsg sys_send ...