1.点击拖拽3D物体,适用于PC、安卓

1.需要UGUI

2.需要Collider、场景中要存在EventSystem

3.将脚本挂载物体上即可

 

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

//安卓 PC都好用 点击物体移动
/// <summary>
/// 物体上要有collider
/// 场景中要存在EventSystem
/// </summary>
[RequireComponent(typeof(RectTransform))]
public class EventClick : MonoBehaviour, IPointerClickHandler
{ 
    public Text text;

    private bool isDrag = false;
    //偏移量
    private Vector3 offset = Vector3.zero;


    public void OnPointerClick(PointerEventData eventData)
    {
        text.text="点到我啦!!!";
    }



    IEnumerator OnMouseDown()
    {
        //将物体由世界坐标系转化为屏幕坐标系,由vector3 结构体变量ScreenSpace存储,以用来明确屏幕坐标系Z轴的位置
        Vector3 ScreenSpace = Camera.main.WorldToScreenPoint(transform.position);
        //完成了两个步骤,1由于鼠标的坐标系是2维的,需要转化成3维的世界坐标系,2只有三维的情况下才能来计算鼠标位置与物体的距离,offset即是距离
        Vector3 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, ScreenSpace.z));
        Debug.Log("down");
        //当鼠标左键按下时
        while (Input.GetMouseButton(0))
        {
            //得到现在鼠标的2维坐标系位置
            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, ScreenSpace.z);
            //将当前鼠标的2维位置转化成三维的位置,再加上鼠标的移动量
            Vector3 CurPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
            //CurPosition就是物体应该的移动向量赋给transform的position属性
            transform.position = CurPosition;
            yield return new WaitForFixedUpdate();
        }

    }
}

2.UGUI双指放大缩放旋转物体

1.安卓端好用,PC端不行

2.需要Collider、场景中要存在EventSystem

3.将脚本挂载物体上即可

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum FingerIE
{
    zero,
    OneFinger,
    TwoFinger,
}
public class Demo : MonoBehaviour
{

    public Vector3 initialRot;
    public Vector3 initialSca;
    public static Demo instance;
    IEnumerator ie;
    FingerIE finger_num = FingerIE.zero;
    void Awake()
    {
        instance = this;
    }
    // Update is called once per frame
    void Update()
    {

        if (Input.touchCount == 0)
        {
            if (finger_num != FingerIE.zero)
            {
                StopCoroutine(ie);
                ie = null;
                finger_num = FingerIE.zero;
            }
        }
        else if (Input.touchCount == 1)
        {
            if (finger_num != FingerIE.OneFinger)
            {
                if (ie != null)
                {
                    StopCoroutine(ie);
                }
                ie = IMonitorMouseOneFinger();
                StartCoroutine(ie);
                finger_num = FingerIE.OneFinger;
            }
        }
        else if (Input.touchCount == 2)
        {
            if (finger_num != FingerIE.TwoFinger)
            {
                if (ie != null)
                {
                    StopCoroutine(ie);
                }
                ie = IIMonitorMouseTwoFinger();
                StartCoroutine(ie);
                finger_num = FingerIE.TwoFinger;
            }
        }

    }
    /// <summary>
    /// 一根手指控制转动
    /// </summary>
    /// <returns></returns>
    IEnumerator IMonitorMouseOneFinger()
    {
        //单手操作
        Touch oneFingerTouch;
        while (true)
        {
            oneFingerTouch = Input.GetTouch(0);
            if (oneFingerTouch.phase == TouchPhase.Moved)
            {
                Vector2 deltaPos = oneFingerTouch.deltaPosition;
                transform.Rotate(-Vector3.up * deltaPos.x * 0.2f, Space.World);
                transform.Rotate(-Vector3.left * deltaPos.y * 0.2f, Space.World);
            }
            yield return 0;
        }
    }
    /// <summary>
    /// 两个手指控制缩放
    /// </summary>
    /// <returns></returns>
    IEnumerator IIMonitorMouseTwoFinger()
    {
        Touch firstOldTouch;
        Touch secondOldTouch;
        Touch firstNewTouch;
        Touch secondNewTouch;
        float oldDistance;
        float newDistance;
        while (true)
        {
            firstOldTouch = Input.GetTouch(0);
            secondOldTouch = Input.GetTouch(1);
            oldDistance = Vector2.Distance(firstOldTouch.position, secondOldTouch.position);
            yield return 0;
            firstNewTouch = Input.GetTouch(0);
            secondNewTouch = Input.GetTouch(1);
            newDistance = Vector2.Distance(firstNewTouch.position, secondNewTouch.position);
            if (oldDistance > newDistance && this.transform.localScale.x > 0.25f)
            {
                this.transform.localScale -= Vector3.one * 0.1f;
            }
            else if (oldDistance < newDistance && this.transform.localScale.x < 2f)
            {
                this.transform.localScale += Vector3.one * 0.1f;
            }
        }
    }
    /// <summary>
    /// 复位
    /// </summary>
    public void ResetRot()
    {
        this.transform.localEulerAngles = initialRot;
        this.transform.localScale = initialSca;
    }

}

@Liam:有用→收藏→关注   听说长得好看的人都这么做!