首页 > 其他分享 >Unity Android端操作-显示/影藏+旋转+放大缩小+截图的脚步代码

Unity Android端操作-显示/影藏+旋转+放大缩小+截图的脚步代码

时间:2023-02-18 15:11:23浏览次数:40  
标签:GetTouch void System Unity using Input Android Vector2 影藏

显示/影藏

 //获取操作对象
    public GameObject text;   
    //初始旋转角度
    public float xspeed = 120;
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
        MySetActive();
    }
    //手指点击关闭显示
    void MySetActive() 
    {
        //点击
        if (Input.GetMouseButton(0))
        {
            //一个手指点击
            if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                text.SetActive(false);
                //2个点击
                if (Input.GetTouch(0).tapCount == 2)
                {
                    text.SetActive(true);//显示
                }
            }
        }
    }

旋转+放大缩小

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

public class EnLargeLogic : MonoBehaviour
{
    //初始操作手指位置
    Vector2 oldPos1;
    Vector2 oldPos2;

    //文字对象
    private TextMesh textMesh;
    //初始旋转角度
    public float xspeed = 120;
    void Start()
    {
        this.textMesh = this.GetComponentInParent<TextMesh>();
    }

    // Update is called once per frame
    void Update()
    {
        //2根手指触摸
        if (Input.touchCount == 2)
        {
            //判断手指移动  Moved==移动
            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;
                //获取xyz坐标值
                float this_x = this.transform.localScale.x;
                float this_y = this.transform.localScale.y;
                float this_z = this.transform.localScale.z;
                if (isEnLarge(oldPos1,oldPos2,temPos1,temPos2))
                {
                    this.transform.localScale = new Vector3(this_x * 1.025f, this_y * 1.025f, this_z * 1.025f);
                }
                else
                {
                    this.transform.localScale = new Vector3(this_x / 1.025f, this_y / 1.025f, this_z / 1.025f);
                }
                //更新操作后的值
                oldPos1 = temPos1;
                oldPos2 = temPos2;
            }
        }
        //单击旋转执行
        MyRotate();
    }
    //判断手势是否是方大
    bool isEnLarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2) 
    {
        //x差值平方+y差值平方
        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));
        //判断oP1平方根是否大于nP1
        if (length1 < length2)
        {
            return true;
        }
        else
        { 
            return false;
        }
    }
    //旋转
    void MyRotate()
    {
        //按下
        if (Input.GetMouseButton(0))
        {
            //单击
            if (Input.touchCount == 1)
            {
                //滑动
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    //旋转(以Y轴)
                    this.textMesh.transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * xspeed * Time.deltaTime, Space.Self);
                }
            }
        }
    }
}

截图

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;

public class ScreenshotLogic : MonoBehaviour
{
    //截屏
    //主相机对象
    private Camera arCamera;
    void Start()
    {
        //获取主相机对象
        arCamera = GameObject.Find("ARCamera").GetComponent<Camera>();
    }
    void Update()
    {
        
    }
    //截屏保存 --不包含UI的
    public void OnScreenshotClick()
    {
        //获取系统当前时间  编辑成字符串形式
        System.DateTime now = System.DateTime.Now;
        //转字符串类型
        string time = now.ToString();
        //去掉空格
        time = time.Trim();
        //替换符号
        time = time.Replace("/", "-");
        //拼接成完整图片名称
        string fileName = "ARScreenShot" + time + ".png";

        //判断是否是Android手机操作
        if (Application.platform == RuntimePlatform.Android)
        {
            //只获取主相机的画面
            //新建截屏对象赋值给主相机操作
            RenderTexture rt = new RenderTexture(Screen.width,Screen.height,1);
            arCamera.targetTexture = rt;
            //渲染
            arCamera.Render();
            //渲染纹理
            RenderTexture.active = rt;

            //保存截图到填图上  材质:TextureFormat.RGB24,是否映射:false
            Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
            //读取填图   Rect:坐标
            texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
            //应用保存
            texture.Apply();

            //关闭截屏
            arCamera.targetTexture = null;
            RenderTexture.active = null;
            Destroy(rt);


            //将填图转换为数组
            byte[] bytes = texture.EncodeToPNG();
            //保存路径   手机相册截图地址
            string destination = "/sdcard/DCIM/Camera";
            //判断地址是否存在
            if (!Directory.Exists(destination))
            {
                //创建地址
                Directory.CreateDirectory(destination);
            }
            //拼接保存地址
            string pathSave = destination + "/" + fileName;

            Debug.Log(pathSave);

            //文件写入==保存
            File.WriteAllBytes(pathSave, bytes);
        }
    }
}

  

标签:GetTouch,void,System,Unity,using,Input,Android,Vector2,影藏
From: https://www.cnblogs.com/clf125800/p/17132676.html

相关文章

  • Android 高德地图自定义Marker覆盖物
    直接贴代码:privatevoidquernResCircle(LatLnglatLng,Stringtitle,Stringaddress){ViewmarkerView=ViewGroup.inflate(LocationActivity.this,R.layout.......
  • Android Studio相关配置说明
    介绍:AndroidSdutio是谷歌推出的一个Android集成开发工具。现已将SDK(softwaredevelopmentkit)集成到 AndroidStudio中。可以直接集成安装。 PS:如果部分用户先......
  • Unity 胶囊碰撞体(CapsuleCollider)
    胶囊碰撞体(CapsuleCollider) 由两个半球与一个圆柱体连接在一起组成。胶囊碰撞体与胶囊原始碰撞体的形状相同。         属性属性:        ......
  • win系统下eclipse开发android环境配置
    一.安装java环境1.下载JDK并安装在java官方网站下载jdk安装软件,下载网址:https://www.oracle.com/java/technologies/downloads/此时oracle发布的java已......
  • Android笔记--动态申请权限
    动态申请权限在动态申请权限这里,一共分为两种不同的模式,分别是Lazy模式(懒汉式)和Hungry模式(饿汉式),这两种模式区分的话,可以通俗地解释一下就是,对于懒汉来说,只有在我们点击某......
  • 【Android逆向】滚动的天空中插入smali日志
    1.编写一个MyLog.java放到一个android工程下,编译打包,然后反编译拿到MyLog的smali代码packagecom.example.logapplication;importandroid.util.Log;publicclassM......
  • 【Unity 3D游戏开发】在Unity使用NoSQL数据库方法介绍
    随着游戏体积和功能的不断叠加,游戏中的数据也变得越来越庞杂,这其中既包括玩家产生的游戏存档等数据,例如关卡数、金币等,也包括游戏配置数据,例如每一关的配置情况。尽管Unity......
  • Unity TextMesh 操作-----点击显示/影藏+长按方大+旋转
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassSetActiveLogic:MonoBehaviour{//获取操作对象publicGa......
  • unity创建物体的编辑器的回调
    注意:需要Unity2021以上版本 参考https://forum.unity.com/threads/editor-callbacks-for-gameobject-creation-deletion-duplication-by-user-or-user-script.788792/......
  • Unity在使用UI接口时,遇到拖拽位置错误的问题
     IDragHandler,IEndDragHandler,IBeginDragHandler在使用UI拖拽接口的时候,发现拖拽时会异常的偏移最后发现是因为是直接使用了transform.position导致的后来改成Re......