【论】Unity的Input应用(一)

使用Unity做下列的功能实现:鼠标点击下去,出现抹布,抹布随着鼠标的移动而移动。当抹布擦拭神灯后,会出现灯神。



        //鼠标版
        if (Input.GetMouseButtonDown(0))
        {
            dusterCloth = Instantiate(dusterClothPre) as GameObject;
            dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.mousePosition);
            dusterCloth.transform.parent = clothParent;
            dusterCloth.transform.localScale = Vector3.one;
            dusterCloth.transform.localEulerAngles = Vector3.zero;
        }
        if (Input.GetMouseButton(0))
        {
            dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit hit;
            Ray ray = ca.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.name == "1" || hit.collider.name == "2")
                {
                    timeMax += Time.deltaTime;                    
                    if (timeMax > 1f)
                    {
                        timeMax = 0;
                        //触发按钮 
                        Debug.Log("Send Message Trigger");
                        transform.parent.GetComponent<Sg_Aladdin>().SelectBtnMessage(hit.collider.gameObject);
                    }
                }
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            //删掉抹布
            Destroy(dusterCloth);
            timeMax = 0;
        }


        //触屏版
        if (Input.touchCount > 0)
        {
            var touch = Input.GetTouch(0);
            switch (touch.phase)
            {
                case TouchPhase.Began:
                    dusterCloth = Instantiate(dusterClothPre) as GameObject;
                    dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.GetTouch(0).position);
                    dusterCloth.transform.parent = clothParent;
                    dusterCloth.transform.localScale = Vector3.one;
                    dusterCloth.transform.localEulerAngles = Vector3.zero;
                    break;
                case TouchPhase.Moved:
                    dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.GetTouch(0).position);
                    RaycastHit hit;
                    Ray ray = ca.ScreenPointToRay(Input.GetTouch(0).position);
                    if (Physics.Raycast(ray, out hit))
                    {
                        uilabel.text = hit.collider.name;
                        if (hit.collider.name == "1" || hit.collider.name == "2")
                        {
                            timeMax += Time.deltaTime;                    
                            if (timeMax > 1f)
                            {
                                timeMax = 0;
                                //触发按钮 
                            }
                        }
                    }
                    break;
                case TouchPhase.Ended:
                    //删掉抹布
                    Destroy(dusterCloth);
                    timeMax = 0;
                    break;
            }
        }


Notice : 界面摆放时,需要触发事件的UI置于最顶层。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中的输入系统是用于处理用户输入的组件。它允许您捕获和响应用户键盘、鼠标和触摸屏输入。 在Unity中,您可以通过以下几种方式来处理输入: 1. 使用`Input`类:Unity提供了一个名为`Input`的静态类,您可以使用它来检测用户按下的键、鼠标按钮以及触摸屏上的触摸手势。您可以在脚本中使用`Input.GetKey`、`Input.GetMouseButton`和`Input.GetTouch`等函数来检测输入事件。 2. 使用事件系统:Unity提供了一个事件系统,通过将输入事件绑定到特定的函数上,您可以使用事件触发器来处理输入。例如,您可以使用`EventTrigger`组件来在用户点击按钮时触发相关功能。 3. 使用自定义输入管理器:如果您需要更高级的输入控制,您可以编写自己的输入管理器。您可以通过继承`MonoBehaviour`类并重写`Update`函数来实现自定义逻辑,然后在脚本中访问输入管理器的实例来处理输入。 无论您选择哪种方法,处理输入的一般流程如下: 1. 捕获用户输入:使用合适的方法检测用户的键盘、鼠标或触摸屏输入。 2. 响应输入:根据用户的输入来执行相应的操作。这可以是移动游戏对象、触发特定的事件或执行其他逻辑。 3. 更新游戏状态:根据输入的结果来更新游戏状态,例如更新玩家位置、改变游戏进程或进行其他操作。 Unity的输入系统非常灵活,可以轻松处理各种用户输入。根据您的需求和项目类型,选择最合适的方法来处理输入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值