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

显示/影藏

 //获取操作对象
    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);
        }
    }
}

  

posted @ 2023-02-18 15:03  打工仔-也想飞  阅读(262)  评论(0)    收藏  举报