【Unity3D自学记录】Unity中的物体缩放-Android和PC平台

来源:互联网 发布:车削中心编程 编辑:程序博客网 时间:2024/06/05 08:54

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

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

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脚本设置为可用            }        }    }}


0 0