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有一定认识了吧!回想一下您玩过的游戏就一定知道什 ...
随机推荐
- 锐捷Linux版的下载和使用(福大客户端)
下载锐捷程序包 点此下载 没有连接到锐捷里就进不了这个安装包的官方下载界面(好矛盾啊这个),所以我把它上传到博客园了. 解压文件 schaepher:~$ cd Downloads/ schaephe ...
- jquery datagrid添加冻结列等
frozenColumns:[[ { field: 'xh', checkbox: true }, { field: "CZ", title: "操作", wi ...
- 在""中添加"
加上\即可 "return '<span onmouseover=MouseOver(this) onmouseout=MouseOut(this) onclick=editTea(\ ...
- Python小代码_3_购物车
product_list = [ ('MacBook', 9000), ('kindle', 500), ('tesla', 900000), ('book', 100), ('bike', 2000 ...
- CentOS, Fedora, or Red Hat一行命令安装apache + mysql + php 及各种依赖库
sudo sh -c "yum install httpd httpd-devel mysql mysql-server mysql-devel php php-mysql php-comm ...
- MYSQL 索引类型、什么情况下用不上索引、什么情况下不推荐使用索引
mysql explain的使用: http://blog.csdn.net/kaka1121/article/details/53394426 索引类型 在数据库表中,对字段建立索引可以大大提高查询 ...
- 【Java集合系列】---总体框架
个的组合,这些数据项可能共享某些特征,需要以某种操作方式一起进行操作,一般来说,这些数据项的类型都是相同的,或者基类相同(若使用的语言支持继承),列表或数组通常不认为是集合,因为其大小固定,但是事实上 ...
- docker iotop :OSError: Netlink error: No such file or directory
在容器内使用iotop ,错误信息: raceback (most recent call last): File "/usr/sbin/iotop", line 16, in & ...
- 在做自动化测试之前你需要知道的,转自:http://www.cnblogs.com/fnng/p/3653793.html
什么是自动化测? 做测试好几年了,真正学习和实践自动化测试一年,自我感觉这一个年中收获许多.一直想动笔写一篇文章分享自动化测试实践中的一些经验.终于决定花点时间来做这件事儿. 首先理清自动化测试的概念 ...
- VMware Tools (ubuntu系统)安装详细过程与使用
前一段时间博主在VMware虚拟机上安装了Ubuntu系统,如果还没有安装的同学可以参考博主上一篇文章:VMware Ubuntu安装详细过程. 猿友们都知道linux不太好用,如果你想将你主机Win ...