美文网首页
跨平台的移动和触控

跨平台的移动和触控

作者: IcodeIlife | 来源:发表于2016-07-13 10:37 被阅读0次

Unity3D 支持多平台的发布,但是平时在测试的过程中往往需要在各个平台之间进行切换,在平时开发中,对象的移动、舞台缩放使用鼠标控制比较方便,但是在安卓平台,鼠标就无法发挥作用了,只能使用触控操作,最近整理了两种控制器,适用于桌面以及安卓平台。

最终使用代码如下:

using UnityEngine;  using System.Text;  using System.Collections.Generic;    public class ControllerObject : MonoBehaviour  {      public UILabel uiLabel;      private PlatformController controller;        private IListtextList;        void Awake()      {          this.textList = new List();          #if UNITY_STANDALONE_WIN              controller = this.gameObject.AddComponent();          #elif UNITY_ANDROID              controller = this.gameObject.AddComponent();          #endif            controller.Init (OnBeginEndCallback, OnMoveCallback, OnScaleCallback, OnUpdateCallback, OnEndCallback);      }        private void OnBeginEndCallback()      {          this.uiLabel.text = "开始";      }        private void OnMoveCallback(Vector2 direction)      {          if(this.textList.Count > 10)          {              this.textList.RemoveAt(0);          }          this.textList.Add("移动:" + direction.x + ":" + direction.y);          this.uiLabel.text = this.GetText ();      }        private void OnScaleCallback(float distance)      {          if(this.textList.Count > 10)          {              this.textList.RemoveAt(0);          }          this.textList.Add("缩放:" + distance);          this.uiLabel.text = this.GetText ();      }        private void OnUpdateCallback()      {        }        private void OnEndCallback()      {          this.uiLabel.text = "结束";      }        private string GetText()      {          StringBuilder stringBuilder = new StringBuilder ();            foreach(string text in this.textList)          {              stringBuilder.Append(text);              stringBuilder.Append("\n");          }            return stringBuilder.ToString ();      }  }  下面是各个类的代码:TouchCallback.csusing UnityEngine;using System.Collections;////// 触碰回调函数

///public class TouchCallback{// 开始回调函数,(按钮按下、触碰)触发一次public delegate void Begin();// 移动回调函数,移动时触发public delegate void Move(Vector2 direction);// 缩放回调函数,缩放时触发public delegate void Scale(float distance);// 结束回调函数,(按钮松开,触离)触发一次public delegate void End();// 更新回调函数,每侦触发public delegate void Update();}[csharp] view plain copy 在CODE上查看代码片派生到我的代码片using UnityEngine;  using System.Collections;    ////// 平台控制器

///public class PlatformController : MonoBehaviour  {      protected TouchCallback.Begin beginCallback;      protected TouchCallback.Move moveCallback;      protected TouchCallback.Scale scaleCallback;      protected TouchCallback.Update updateCallback;      protected TouchCallback.End endCallback;        ////// 初始化回调函数

//////Begin callback.      ///Move callback.      ///Scale callback.      ///Update callback.      ///End callback.      public virtual void Init(TouchCallback.Begin beginCallback, TouchCallback.Move moveCallback, TouchCallback.Scale scaleCallback, TouchCallback.Update updateCallback, TouchCallback.End endCallback)      {          this.beginCallback = beginCallback;          this.moveCallback = moveCallback;          this.scaleCallback = scaleCallback;          this.updateCallback = updateCallback;          this.endCallback = endCallback;      }  }  [csharp] view plain copy 在CODE上查看代码片派生到我的代码片using UnityEngine;  using System.Collections;    ////// 鼠标控制器

///public class MouseController : PlatformController  {        /// 鼠标枚举      enum MouseTypeEnum      {          LEFT = 0      }        ////// 缩放距离

///private float scrollDistance;        ////// 鼠标按住状态

///private bool mousePressStatus;        void Update()      {          // 按下鼠标、轴          if (Input.GetMouseButtonDown ((int)MouseTypeEnum.LEFT))          {              this.mousePressStatus = true;              // 触发开始回调函数              if(this.beginCallback != null) this.beginCallback();          }          // 松开鼠标、轴          if (Input.GetMouseButtonUp ((int)MouseTypeEnum.LEFT))          {              this.mousePressStatus = false;              // 触发结束回调函数              if(this.endCallback != null) this.endCallback();          }          // 如果鼠标在按住状态          if (this.mousePressStatus)          {              // 触发移动回调函数              if(this.moveCallback != null) this.moveCallback(new Vector2(Input.GetAxis ("Mouse X"), Input.GetAxis ("Mouse Y")));          }          // 鼠标滚轮拉近拉远          this.scrollDistance = Input.GetAxis ("Mouse ScrollWheel");          // 触发缩放回调函数          if (this.scrollDistance != 0f && this.scaleCallback != null) this.scaleCallback(this.scrollDistance);            // 触发每帧执行更新          if (this.updateCallback != null) this.updateCallback ();      }  }  [csharp] view plain copy 在CODE上查看代码片派生到我的代码片using UnityEngine;  using System.Collections;    ////// 触碰控制器

///public class TouchController : PlatformController  {      ////// 修正比例

///private float rate = 50f;        ////// 触点一

///private Touch oneTouch;            ////// 触点二

///private Touch twoTouch;            ////// 最后一次缩放距离

///private float lastScaleDistance;            ////// 当前缩放距离

///private float scaleDistance;        void Update()      {          // 如果只有一个触点          if (Input.touchCount == 1)          {              this.oneTouch = Input.touches[0];              // 触点开始              if(this.oneTouch.phase == TouchPhase.Began)              {                  // 触发开始回调函数                  if(this.beginCallback != null) this.beginCallback();              }              // 触点移动              else if(oneTouch.phase == TouchPhase.Moved)              {                  // 触发移动回调函数                  if(this.moveCallback != null) this.moveCallback(new Vector2(this.oneTouch.deltaPosition.x, this.oneTouch.deltaPosition.y) / this.rate);              }              // 触点结束              else if(oneTouch.phase == TouchPhase.Ended)              {                  // 触发结束回调函数                  if(this.endCallback != null) this.endCallback();              }          }          // 如果有多个触点          if(Input.touchCount > 1)          {              this.oneTouch = Input.touches[0];              this.twoTouch = Input.touches[1];              // 如果是缩放              if(oneTouch.phase == TouchPhase.Moved && twoTouch.phase == TouchPhase.Moved)              {                  this.scaleDistance = Vector2.Distance(this.oneTouch.position, this.twoTouch.position);                  // 触发缩放回调函数                  this.scaleCallback((this.scaleDistance - this.lastScaleDistance) / this.rate);                  this.lastScaleDistance = this.scaleDistance;              }          }          // 触发每帧执行更新          if (this.updateCallback != null) this.updateCallback ();      }  }

相关文章

  • 跨平台的移动和触控

    Unity3D 支持多平台的发布,但是平时在测试的过程中往往需要在各个平台之间进行切换,在平时开发中,对象的移动、...

  • Kivy跨平台技术开发iOS

    #kivy Kivy是一套专门用于跨平台快速应用开发的开源框架,使用Python和Cython编写,对于多点触控有...

  • kivy 学习之HelloWord

    Kivy是一套专门用于跨平台快速应用开发的开源框架,使用Python和Cython编写,对于多点触控有着非常良好的...

  • [Unity插件] FingerGestures

    FingerGestures是一款强大的手势识别插件,支持鼠标和触控。跨平台,最新的3.0版本支持自定义手势识别。...

  • 触控事件的学习笔记(待修稿)

    iOS的触控事件是基于多点触控模型。 一、触控可以概述为: 1.触控是用户手指触碰屏幕,或触碰屏幕并在屏幕上移动时...

  • Flutter RN 原生对比

    移动端跨平台开发技术演进 现在主流的移动开发平台是Android和iOS,之前还有过windows phone,每...

  • 跨平台移动应用开发框架 2019-03-18

    标签(空格分隔): 移动应用 跨平台 混和开发 Flutter 移动应用跨平台开发框架,根据其原理,主要分为三类:...

  • 手指触控点

    触控点id和索引区别 getActionIndex方法,获取触控点索引,索引值和触控手指个数相关,从0开始。触控点...

  • POSIX文件操作(一)

    前言 在移动开发中,难免要做一些跨平台的事情。iOS和Android各执一套,平台很难跨。但其底层Linux和Un...

  • 美团-外卖客户端容器化架构的演进

    1.背景 1.1 移动端跨平台技术的介绍 移动端的跨平台技术不是一个新话题,早在几年前,WebView容器、Rea...

网友评论

      本文标题:跨平台的移动和触控

      本文链接:https://www.haomeiwen.com/subject/eqqsjttx.html