cocos2d-x 3.0 触摸注册函数下面先来详细的介绍一下。
cocos2d-x 3.0版本的事件分发的机制较之之前的版本进行了修改,把事件处理的逻辑分离出来,并通过不同的事件监听器来监听不同的事件。当一个节点收到了事件,就会指派一个事件分发器_eventDispatcher专门来分发这些事件。对于触摸来说,大概的过程就是我们先创建一个对应触摸事件的监听器,然后覆盖触摸事件的函数,并把它们绑定到监听器,然后可以设置一下这个监听器的属性,最后把监听器添加到分发器之中,系统就会自动进行触摸事件的处理。
我们先看看单点触摸的使用,下面是源代码中关于单点触摸监听器的类,可以看到
这个Unity3D批量修改贴图 导入设置工具脚本十分小巧,但是威力大。特别针对大批量贴图要调整尺寸等等的时候作用尤为明显。
在菜单中添加“Custom→Texture”的方式来批 量改变所选的贴图导入设置。Unity本身只能一次打开一张图片进行导入设置,目前这个脚本可以批量更改贴图格式,是否开启MipMap,调整纹理最大尺 寸,是否可读等等。
用法是把脚本放在你项目的资源目录的Editor文件夹下。然后选择你要批处理的纹理。到菜单中选择要处理的类型就可以了。
ChangeTextureImportSettings。csforUnity2.x
程序代码csharp代码:
- using UnityEngine; using UnityEditor;
- // ///////////////////////////////////////////////////////////////////////////////////////////////////////// //
- // Batch Texture import settings modifier. //
- // Modifies all selected textures in the project window and applies the requested modification on the
- // textures. Idea was to have the same choices for multiple files as you would have if you open the // import settings of a single texture. Put this into Assets/Editor and once compiled by Unity you find
- // the new functionality in Custom -> Texture. Enjoy! :-) //
- // Based on the great work of benblo in this thread: //
- http://forum.unity3d.com/viewtopic.php?t=16079&start=0&postdays=0&postorder=asc&highlight=textureimporter //
- // Developed by Martin Schultz, Decane in August 2009 // e-mail: ms@decane.net //
- // ///////////////////////////////////////////////////////////////////////////////////////////////////////// public class ChangeTextureImportSettings : ScriptableObject {
- [MenuItem ("Custom/Texture/Change Texture Format/Auto")] static void ChangeTextureFormat_Auto() {
- SelectedChangeTextureFormatSettings(TextureImporterFormat.Automatic); }
- [MenuItem ("Custom/Texture/Change Texture Format/RGB Compressed DXT1")] static void ChangeTextureFormat_RGB_DXT1() {
- SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT1); }
- [MenuItem ("Custom/Texture/Change Texture Format/RGB Compressed DXT5")] static void ChangeTextureFormat_RGB_DXT5() {
- SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT5); }
- [MenuItem ("Custom/Texture/Change Texture Format/RGB 16 bit")] static void ChangeTextureFormat_RGB_16bit() {
- SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB16); }
- [MenuItem ("Custom/Texture/Change Texture Format/RGB 24 bit")] static void ChangeTextureFormat_RGB_24bit() {
- SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB24); }
- [MenuItem ("Custom/Texture/Change Texture Format/Alpha 8 bit")] static void ChangeTextureFormat_Alpha_8bit() {
- SelectedChangeTextureFormatSettings(TextureImporterFormat.Alpha8); }
- [MenuItem ("Custom/Texture/Change Texture Format/RGBA 16 bit")] static void ChangeTextureFormat_RGBA_16bit() {
- SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB16); }
- [MenuItem ("Custom/Texture/Change Texture Format/RGBA 32 bit")] static void ChangeTextureFormat_RGBA_32bit() {
- SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB32); }
- // ----------------------------------------------------------------------------
- [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/32")] static void ChangeTextureSize_32() { SelectedChangeMaxTextureSize(32); }
- [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/64")] static void ChangeTextureSize_64() { SelectedChangeMaxTextureSize(64); }
- [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/128")]
- static void ChangeTextureSize_128() { SelectedChangeMaxTextureSize(128); }
- [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/256")] static void ChangeTextureSize_256() { SelectedChangeMaxTextureSize(256); }
- [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/512")] static void ChangeTextureSize_512() { SelectedChangeMaxTextureSize(512); }
- //Unity3D教程:www.unitymanual.com
- [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/1024")] static void ChangeTextureSize_1024() { SelectedChangeMaxTextureSize(1024); }
- [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/2048")] static void ChangeTextureSize_2048() { SelectedChangeMaxTextureSize(2048); }
- // ----------------------------------------------------------------------------
- [MenuItem ("Custom/Texture/Change MipMap/Enable MipMap")] static void ChangeMipMap_On() { SelectedChangeMimMap(true); }
- [MenuItem ("Custom/Texture/Change MipMap/Disable MipMap")] static void ChangeMipMap_Off() { SelectedChangeMimMap(false); }
- // ----------------------------------------------------------------------------
- static void SelectedChangeMimMap(bool enabled) {
- Object[] textures = GetSelectedTextures(); Selection.objects = new Object[0];
- foreach (Texture2D texture in textures) {
- string path = AssetDatabase.GetAssetPath(texture);
-
-
-
-
-
- TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
- textureImporter.mipmapEnabled = enabled; AssetDatabase.ImportAsset(path); } }
- //Unity3D教程手册:www.unitymanual.com
- static void SelectedChangeMaxTextureSize(int size) {
- Object[] textures = GetSelectedTextures(); Selection.objects = new Object[0];
- foreach (Texture2D texture in textures) {
- string path = AssetDatabase.GetAssetPath(texture);
- TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
- textureImporter.maxTextureSize = size; AssetDatabase.ImportAsset(path); } }
- static void SelectedChangeTextureFormatSettings(TextureImporterFormat newFormat) {
- Object[] textures = GetSelectedTextures(); Selection.objects = new Object[0];
- foreach (Texture2D texture in textures) {
- string path = AssetDatabase.GetAssetPath(texture); //Debug.Log("path: " + path);
- TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
- textureImporter.textureFormat = newFormat; AssetDatabase.ImportAsset(path); } }
- static Object[] GetSelectedTextures() {
- return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets); } class EventListenerTouchOneByOne: public EventListener
- {
- public:
- static const std::string LISTENER_ID;
- static EventListenerTouchOneByOne* create();
- virtual~EventListenerTouchOneByOne();
- void setSwallowTouches(bool needSwallow);
- bool isSwallowTouches();
- ///Overrides
- virtual EventListenerTouchOneByOne* clone() override;
- virtual bool checkAvailable() override;
- //
- public:
- std::function<bool(Touch*, Event*)>onTouchBegan;
- std::function<void(Touch*, Event*)>onTouchMoved;
- std::function<void(Touch*, Event*)>onTouchEnded;
- std::function<void(Touch*, Event*)> onTouchCancelled;
- private:
- EventListener TouchOneByOne();
- bool init();
- std::vector<Touch*>_claimedTouches;
- bool _needSwallow;
- friend class EventDispatcher;
- };</span>
//添加一个测试的精灵
- autoonion = Sprite::create("onion.png");
- onion->setPosition(Point(visibleSize.width/2, visibleSize.height/2));
- onion->setScale(0.2);
- this->addChild(onion);
- //创建一个触摸监听器,这里使用单点触摸事件
- autoTouchListenr = EventListenerTouchOneByOne::create();
- //设置吞噬为true,不让触摸往下传递
- TouchListenr->setSwallowTouches(true);
- //和回调函数绑定
- TouchListenr->onTouchBegan= CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
- TouchListenr->onTouchMoved= CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
- TouchListenr->onTouchEnded= CC_CALLBACK_2(HelloWorld::onTouchEnded,this);
- //添加监听器到事件分发器中
- _eventDispatcher->addEventListenerWithSceneGraphPriority(TouchListenr,onion);
- 下面我们覆盖单点触摸中提供的触摸函数:
-
- [cpp] view plaincopy
- bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
- {
- //获取精灵对象并取得精灵的矩阵
- autosprite = static_cast<Sprite*>(event->getCurrentTarget());
- Rect rect = sprite->getBoundingBox();
- //获取触摸点的坐标
- Point point = touch->getLocation();
- //判断触摸点是否在精灵的矩阵范围内
- if(rect.containsPoint(point))
- {
- return true;
- }
- return false;
- }
- void HelloWorld::onTouchMoved(Touch* touch, Event* event)
- {
- //获取精灵对象
- autosprite = static_cast<Sprite*>(event->getCurrentTarget());
- //改变精灵的位置
- sprite->setPosition(sprite->getPosition()+ touch->getDelta());
- }
- void HelloWorld::onTouchEnded(Touch* touch, Event* event)
- {
- CCLog("touch end!");
- }
在上面的例子中我们看到了,传递过来的参数主要有Touch* touch和Event* event,我们可以进入源代码中查看他们的作用。Touch类是继承了REF,查看源代码中的主要成员如下:
也就是Touch传入的是触摸点的坐标位置,并且Touch类为我们提供一些坐标的写法,那么我们就方便很多了。譬如上面例子中的触摸移动时,Touch就为我提供一个getDelta()来计算点的位移。
而对于Event类,主要是传入处理的对象,看源代码有以下主要成员:
那么我们在处理触摸对象的时候和例子那样通过getCurrentTarget()来获取对象并处理即可。
另一方面,我们在把监听器添加到事件分发器的时候,会看到代码提示两种方法,一个为:addEventListenerWithSceneGraphPriority,另一个为addEventListenerWithFixedPriority。同样可以查看源代码如下:
根据注释可以知道,前者的触发优先级是按照第二个参数中的node的显示顺序来确定的,而且默认的fixedPriority为0,也就是如果精灵位置靠前,则会优先响应触摸。而后者按照第二个参数的整形变量值的大小来确定的,而且不能为0,值越小那么优先响应触摸。两者除了优先级不一样,移除的过程也有差异。前者会在触摸对象对应的node析构之后系统会帮我们调用移除触摸,但是后者就需要我们手动移除,这一点要注意。
下面我们可以修改上述例子的代码,添加多两个精灵,然后在分发事件代码中添加:
点击运行可以测试一下,没有问题。要是想使用addEventListenerWithFixedPriority的方式的话,虽然可以自定义优先级,但是由于没有绑定node对象,所以还需要在触摸函数中引入需要控制的对象才行。但是最后别忘了使用_eventDispatcher->removeEventListener(listerName);来实现监听器的移除。
多点触摸和单点触摸类似,看下面多点触摸监听器的源码。
可以发现多点触摸就是传入多个Touch对象而已,然后处理的时候可以遍历vector<Touch*>来逐个处理每一个点,例子可以参考源码自带的例子MutiTouchTest。