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有一定认识了吧!回想一下您玩过的游戏就一定知道什 ...
随机推荐
- 怎样使用Secure CRT查看vcenter和esxi主机的日志文件(转)
对ESXI主机的日志管理对于故障诊断和合规性至关重要.Esxi主机的日志通过syslog工具进行管理的,在默认的情况下,日志文件存储在主机的scratch分区中(/scratch/log/).scra ...
- Mysql锁机制--写锁
Mysql 系列文章主页 =============== 1 准备数据 1.1 建表 1.1.1 建立 Employee表 DROP TABLE IF EXISTS employee; CREATE ...
- Postgresql合并年月日、月份和日期左侧补零
在写一个统计查询的 SQL 语句时,需要根据年.月.日分组,但要求返回的字段是日期格式:yyyy年MM月dd日.刚开始我的做法是返回年.月.日,然后再手动拼接年月日,而且还要判断月份和日期是否为个位数 ...
- Just for mysql
mysql的下载与安装 由于学校开设了数据库专业,并且最近准备在做一个web端的设计,虽然本人是负责前端(当然,前端技术也很LOW),但因种种原因,准备开始学习数据库相关的知识,以mysql为例. 昨 ...
- jquery easyui combox不能编辑只能选择
$('#tts').combobox({ editable:false });
- 取list的值
list.get(0):之类的我就不写了 我就写一个我老忘记的 Iterator it = list.iterator(); while(it.hasNext()){ Student stu = it ...
- python读取文本文件数据
本文要点刚要: (一)读文本文件格式的数据函数:read_csv,read_table 1.读不同分隔符的文本文件,用参数sep 2.读无字段名(表头)的文本文件 ,用参数names 3.为文本文件制 ...
- ZOJ-2965
Accurately Say "CocaCola"! Time Limit: 2 Seconds Memory Limit: 65536 KB In a party he ...
- ACM Bone Collector
Many years ago , in Teddy's hometown there was a man who was called "Bone Collector". Th ...
- 存出和载入Docker镜像
存出镜像 如果要导出镜像到本地文件,可以使用 docker save 命令. $ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL ...