Unity_AR_Vuforia_实现模型双指放大缩小_旋转功能

大家好,我是技术小白,自己写博客既可以当作笔记,又可以帮助他人,一举两得,何不美哉?

好了不废话了,直接上代码。

提醒一下和我一样的入门小白,复制下面代码粘贴到一个新的C#脚本里时,

类名需要改成 和 外面文件名相同,否则没效果。

直接把下面脚本拖到 想要用双指放大缩小的游戏物体上即可。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//放大缩小功能 f
public class Scale : MonoBehaviour {

    Vector2 oldPos1;
    Vector2 oldPos2;

	// Use this for initialization
	void Start () {
		
	}

    // Update is called once per frame
    void Update() {
        if (Input.touchCount == 2) {
            if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved){
                Vector2 temPos1 = Input.GetTouch(0).position;
                Vector2 temPos2 = Input.GetTouch(1).position;

                if (isEnLarge(oldPos1, oldPos2, temPos1, temPos2))
                {
                    float oldScale = transform.localScale.x;
                    //放大的倍数 如果要修改 列如:oldScale * 1.025f 修改成 oldScale * 1.25f 根据自己需求修改
                    float newScale = oldScale * 1.025f;

                    transform.localScale = new Vector3(newScale, newScale, newScale);
                }else {
                    float oldScale = transform.localScale.x;
                    //缩小的倍数 如果要修改 列如:oldScale / 1.025f 修改成 oldScale / 1.25f 根据自己需求修改
                    float newScale = oldScale / 1.025f;

                    transform.localScale = new Vector3(newScale, newScale, newScale);
                }

                oldPos1 = temPos1;
                oldPos2 = temPos2;

            }
        }
	}

    bool isEnLarge(Vector2 oP1,Vector2 oP2,Vector2 nP1,Vector2 nP2) {

        float length1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        float length2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));

        if (length1 < length2)
        {
            return true;
        }
        else {
            return false;
        }
        
    }
}

2.旋转功能

提醒一下和我一样的入门小白,复制下面代码粘贴到一个新的C#脚本里时,

类名需要改成 和 外面文件名相同,否则没效果。

直接把下面脚本拖到 想要用手指旋转的游戏物体上即可。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour {

    //上一个手指的位置
    //Vector3 PrevlousPosition;
    //手指的偏移值
    //Vector3 Offset;

    //速度
    float xSpeed = 150f;
    
	// Use this for initialization
	void Start () {
	    
	}
	
	// Update is called once per frame
	void Update () {

        //如果触摸了屏幕
        if (Input.GetMouseButton(0)) {
            //判断是几个手指触摸
            if (Input.touchCount == 1) {
                //第一个触摸到手指头 phase状态 Moved滑动
                if (Input.GetTouch(0).phase == TouchPhase.Moved) {
                //根据你旋转的 模型物体 是要围绕哪一个轴旋转 Vector3.up是围绕Y旋转
                    transform.Rotate(Vector3.up*Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime);
                }
            }
        }
    }
}

补充一点(重要)

如果发现模型 点击没有效果

可能是模型 没有加碰撞体 Collider

  • 29
    点赞
  • 205
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
好的,我了解你的问题。首先,您需要在Unity中安装和设置Vuforia AR引擎。然后,您可以按照以下步骤创建一个Virtual Button,并在按下按钮时显示视频: 1. 在Unity中创建一个空白场景,并导入您要显示的视频。 2. 从Vuforia菜单中选择“AR Session”并添加到场景中。 3. 从Vuforia菜单中选择“Image Target”并添加到场景中。 4. 选择“Image Target”,在“Inspector”窗格中选择您要用于识别的图像。 5. 选择“Image Target”,在“Inspector”窗格中选择“Add Component”,然后选择“Virtual Button”。 6. 在“Virtual Button Behaviour”组件中,单击“Create”以创建虚拟按钮。 7. 将视频添加到场景中,并将其放置在场景外。 8. 在“Virtual Button Behaviour”组件中,单击“Add Virtual Button”并将其拖动到场景中的视频上。 9. 在“Virtual Button Behaviour”组件中,设置“Pressed Material”为视频材质,并设置“Enable Play Mode”为“True”。 10. 在“Virtual Button Behaviour”组件中,单击“Add Event”并添加一个新的脚本组件。 11. 在脚本组件中,编写代码以在按下按钮时播放视频。例如: ``` using UnityEngine; using System.Collections; using UnityEngine.Video; public class PlayVideo : MonoBehaviour { public VideoPlayer videoPlayer; void Start () { videoPlayer = GetComponent<VideoPlayer>(); } public void OnButtonPressed() { videoPlayer.Play(); } } ``` 12. 将脚本组件添加到“Virtual Button Behaviour”组件的事件列表中,并将“OnButtonPressed”事件拖动到新的脚本组件中。 现在,当您单击虚拟按钮时,视频应该会播放。希望这能够帮助到您!
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值