猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢!

原文地址: http://www.cocos2dev.com/?p=582

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开发之十)的更多相关文章

  1. 如何修改新建脚本模板-ScriptTemplates(Unity3D开发之十五)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/44957631 ...

  2. uGUI使用代码动态添加Button.OnClick()事件(Unity3D开发之十二)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/42705885 ...

  3. Orientation Auto Rotation旋转屏幕crash问题(Unity3D开发之十四)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/44133127 ...

  4. 自动生成材质Material(Unity3D开发之十九)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46854411 ...

  5. 自己主动生成材质Material(Unity3D开发之十九)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46854411 ...

  6. 2DSprite添加Light照射(Unity3D开发之十六)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/45534245 ...

  7. 分别修改Cube每个面的贴图UV(Unity3D开发之十八)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46611169 ...

  8. 分别改动Cube每一个面的贴图UV(Unity3D开发之十八)

    猴子原创.欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46611169 ...

  9. 跟我从零基础学习Unity3D开发--NGUI入门基础

    英雄联盟(撸啊撸) QQ飞车 魔兽世界等等相信大家都玩过游戏吧,玩过那UI知道是什么吧?UI可能说得有点专业的话那么游戏中那些属性面板例如: 现在对UI有一定认识了吧!回想一下您玩过的游戏就一定知道什 ...

随机推荐

  1. 我在 B 站学习深度学习(生动形象,跃然纸上)

    我在 B 站学习深度学习(生动形象,跃然纸上) 视频地址:https://www.bilibili.com/video/av16577449/ tensorflow123 http://tensorf ...

  2. Oracle 导入、导出DMP(备份)文件

    首先说明dmp文件: Oracle备份文件是以dmp结尾,这种文件是oracle的逻辑备份文件,常用于数据库逻辑备份,数据库迁移等操作. 一.Oracle导入备份文件步骤:我用的是Oracle 11g ...

  3. Python学习--课本程序练习(周更)

    1.绘制正方形螺旋线 import turtle turtle.setup(600,300,200,200) turtle.pensize(1) turtle.color('green') i=0 w ...

  4. vue中的eventBus

    在vue2中,父子组件传递数据,父组件可以直接传递数据进子组件,而子组件通过调用父组件传递进来的方法,将自己的数据传递回去. 那兄弟组件之间,或者是兄弟组件的子组件之间如何传递呢? 当然vuex是一种 ...

  5. Java 的异常处理机制

    异常是日常开发中大家都「敬而远之」的一个东西,但实际上几乎每种高级程序设计语言都有自己的异常处理机制,因为无论你是多么厉害的程序员,都不可避免的出错,换句话说:你再牛逼,你也有写出 Bug 的时候. ...

  6. Tarjan笔记1

    Tarjan 2822 爱在心中 ** 时间限制: 1 s ** 空间限制: 128000 KB ** 题目等级 : 钻石 Diamond 题解 题目描述 Description"每个人都拥 ...

  7. C stat函数的用法举例(转载)

    stat函数讲解表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat( ...

  8. Java程序员的Golang入门指南(上)

    Java程序员的Golang入门指南 1.序言 Golang作为一门出身名门望族的编程语言新星,像豆瓣的Redis平台Codis.类Evernote的云笔记leanote等. 1.1 为什么要学习 如 ...

  9. Dynamics CRM2016 Web Api之更新时间字段值

    前篇我们论述了时间字段的查询,本篇来论述下时间字段的更新. 还是以之前建的当地时间(时间行为为用户当地时间)字段来测试 可以看到web api更新的是数据库的时间,而在前台的反映就是做了加8处理,所以 ...

  10. 关于Windows下程序执行的说明

    估计有很多人首次都是通过Windows(微软的操作系统)来使用计算机的,Windows的设计导致很多人认为所有程序只要双击一下就可以被正确执行了,所以一大堆初学程序设计的童鞋就会遇到些疑问: 为什么双 ...