Unity iOS 3DTouch 接入

Unity发展速度真的很快,想起去年策划让我接3DTouch我还需要自己写OC的代码,Unity-iOS要互相调来调去,今天发现Unity已经将3DTouch集成进来了,
1、废话不说上Git链接https://github.com/Prince-Ling/Unity3DTouch
2、废话不说上代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;  
using UnityEngine.EventSystems;  

// (1)Input.touchPressureSupported:是否支持3Dtouch,bool类型 
// (2)Touch.pressure:获取当前按压值,float类型 
// (3)Touch.maximumPossiblePressure:获取最大按压值,float类型

public class ThreeDTouch : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerClickHandler{

    public Button threeDTouchBtn;
    public Image scaleImage; 
    public Text showText;
    public Text pressText;
    // Use this for initialization
    void Start () {
        if(Input.touchPressureSupported)
        {
            showText.text = "支持3DTouch";

        }
        else
        {
            showText.text = "!!不支持3DTouch!!";

        }
    }

    [SerializeField]  
    UnityEvent m_OnLongpress = new UnityEvent();  
    private bool isHad3DTouch = false;  
    private float lastInvokeTime;  


    // Update is called once per frame  
    void Update()  
    {  
        if(Input.touchPressureSupported == false)
        {
            return;
        }

        scaleImage.transform.localScale = new Vector3(Input.GetTouch (0).pressure,Input.GetTouch (0).pressure,Input.GetTouch (0).pressure);
        pressText.text = "力度:"+Input.GetTouch (0).pressure.ToString();
        if (isHad3DTouch == false)  
        { 

            if(Input.GetTouch (0).pressure > Input.GetTouch(0).maximumPossiblePressure*0.7f)
            {
                isHad3DTouch = true;
                Handheld.Vibrate();
            }
        }  
    }  


    public void OnPointerDown(PointerEventData eventData)  
    {  
        m_OnLongpress.Invoke();  


        isHad3DTouch = false;  


        lastInvokeTime = Time.time;  
        Debug.Log("鼠标按下");  
    }  


    public void OnPointerUp(PointerEventData eventData)  
    {  
        isHad3DTouch = true;  
        Debug.Log("鼠标抬起");  
    }  


    public void OnPointerExit(PointerEventData eventData)  
    {  
        Debug.Log("鼠标退出");  
    }  
    public void OnPointerClick(PointerEventData eventData)  
    {  
        Debug.Log("鼠标点击");  
    }  

}

 

posted on   &大飞  阅读(304)  评论(0编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示