首页 > Unity3D专栏 > 【Unity3D】Unity中的物体缩放Android和PC平台
2017
07-25

【Unity3D】Unity中的物体缩放Android和PC平台

在本文中缩放的是NGUI的Texture对象:

在一个有Collider的可以控制Texture的对象上添加如下脚本即可:

【Unity3D】Unity中的物体缩放Android和PC平台 - 第1张  | Unity3D虚拟世界

Texture是此对象的子物体,即ScaleDragTexture组件中的Image对象。

using UnityEngine;   
public class ScaleDragTexture : MonoBehaviour  
{  
    public Transform uitexture;  
    //当触屏点超过1个时,对图片进行放大和缩小,最小缩放比例为图片加载时大小      
    private float newdis;   //两个手指之间的距离  
    private float olddis;  
    private float minScale = 1.0f;     //放大比例  
    private float maxScale = 2.0f;  
    private float curScale = 1.0f;  
    private Vector3 uitextureDefaultScale;  
    private Vector3 uitextureCurScale;  
    void Update()  
    {  
        if (Application.platform == RuntimePlatform.Android)  
        {  
            //物体的缩放   
            if (Input.touchCount > 1)  
            {  
                this.GetComponent<UIDragObject>().enabled = false;  //将DragObject脚本设置为不可用,来控制缩放的时候不进行拖拽  
                if (uitextureDefaultScale != this.GetComponent<UpdateImage>().uitextureDefaultScale)  
                {//获取图片未被拖拽时(默认状态)的缩放值  
                    uitextureDefaultScale = uitextureCurScale = this.GetComponent<UpdateImage>().uitextureDefaultScale;  
                }  
                if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)  
                {  
                    var pos1 = Input.GetTouch(0).position;  
                    var pos2 = Input.GetTouch(1).position;  
                    newdis = Vector2.Distance(pos1, pos2);//获取两个触击点的距离  
                    curScale = uitextureCurScale.x / uitextureDefaultScale.x;   //获取当前的缩放比例值  
                    if (newdis < olddis)  
                    {//比例缩小  
                        if (curScale > maxScale || curScale > minScale)  
                            uitexture.localScale -= uitextureDefaultScale * Time.deltaTime;  
                    }  
                    if (newdis > olddis)  
                    {//比例放大  
                        if (curScale <= maxScale)  
                            uitexture.localScale += uitextureDefaultScale * Time.deltaTime;  
                    }  
                    if (curScale < minScale)  
                    {//当前比例小于最小值时恢复到默认缩放值  
                        uitexture.localScale = uitextureDefaultScale;  
                    }  
                    uitextureCurScale = uitexture.localScale;  
                    olddis = newdis;  
                }  
                if (curScale == minScale)  
                {//图片未进行缩放时,只有Y轴上可以被拖动  
                    this.GetComponent<UIDragObject>().scale = new Vector3(0, 1, 0);  
                }  
                else  
                {//当图片缩放后,在X、Y轴上可以被拖动  
                    this.GetComponent<UIDragObject>().scale = new Vector3(1, 1, 0);  
                }  
            }  
            else if (Input.touchCount == 1)  
            {  
                this.GetComponent<UIDragObject>().enabled = true;  //将DragObject脚本设置为可用  
            }  
        }  
  
        else  
        {  
            if (Input.GetMouseButton(2))    //鼠标中键  
            {  
                this.GetComponent<UIDragObject>().enabled = false;  //将DragObject脚本设置为不可用,来控制缩放的时候不进行拖拽  
  
                if (uitextureDefaultScale != this.GetComponent<UpdateImage>().uitextureDefaultScale)  
                {//获取图片未进行缩放时的Scale值  
                    uitextureDefaultScale = uitextureCurScale = this.GetComponent<UpdateImage>().uitextureDefaultScale;  
                }  
                curScale = uitextureCurScale.x / uitextureDefaultScale.x;   //获取当前的缩放比例值  
                if (Input.GetAxis("Mouse ScrollWheel") < 0)  
                {//比例放大  
                    if (curScale <= maxScale)  
                        uitexture.localScale += uitextureDefaultScale * Time.deltaTime;  
                }  
                else if (Input.GetAxis("Mouse ScrollWheel") > 0)  
                {//比例缩小  
                    if (curScale > minScale || curScale > maxScale)  
                        uitexture.localScale -= uitextureDefaultScale * Time.deltaTime;  
                }  
                if (uitexture.localScale.x < uitextureDefaultScale.x)  
                {//当缩放的值小于默认值时,恢复到默认值  
                    uitexture.localScale = uitextureDefaultScale;  
                }  
                uitextureCurScale = uitexture.localScale;  
                if (curScale == minScale)  
                {//默认未进行缩放时只有Y轴上可以被拖动  
                    this.GetComponent<UIDragObject>().scale = new Vector3(0, 1, 0);  
                }  
                else  
                {//缩放之后X、Y轴上可以被拖动  
                    this.GetComponent<UIDragObject>().scale = new Vector3(1, 1, 0);  
                }  
            }  
            else if (Input.GetMouseButtonDown(0))  
            {  
                this.GetComponent<UIDragObject>().enabled = true;  //将DragObject脚本设置为可用  
            }  
        }  
    }  
}  

 

 



虚拟世界提醒您:如果您觉得本文不错,快快将这篇文章分享出去吧 ,感谢您的支持和关注,谢谢!
最后编辑:
作者:虚拟世界
专注互联网技术及资讯 技术研究中心|有相关Unity3D问题或者作事宜请联系我哦!
捐 赠如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝或者微信直接向我捐款,在此非常感谢您对虚拟世界的捐赠。

留下一个回复

你的email不会被公开。