unity 实现图片的放大与缩小,并实现拖拽浏览

首先,是要新建一个UIRoot(使用的NGUI开发),因为一个项目中有时候往往一个UIRoot无法搞定,需要分割出一部分进行单独的处理,所以大家要视情况而定。 

复制代码
using UnityEngine;
using System.Collections;
       
public class Minimap : MonoBehaviour {
       
    public Camera minimapCamera;
    public float minfiledView = 44f;
    public float maxfiledView = 95f;
    private Vector3 offsetPosition;
       
    void Start () {
       
    }
           
    // Update is called once per frame
    void Update () {
        
    }
       
    public void ZoomInButtonClick()
    {
        if (minimapCamera.fieldOfView > minfiledView)
        {
            minimapCamera.fieldOfView -= 1;
        }
        else
            minimapCamera.fieldOfView = minfiledView; 
    }
       
    public void ZoomOutButtonClick()
    {
        if (minimapCamera.fieldOfView < maxfiledView)
        {
            minimapCamera.fieldOfView += 1;
        }
        else
            minimapCamera.fieldOfView = maxfiledView;
    }
}
此处的代码构思来源于小地图的制作,通过控制fieldOfView来确定Camera的视野大小,大家也可以设定视野的范围,让他在指定区间内变化。 

接下来是要对图片的动态载入 


复制代码
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Drawing;
       
public class OpenPicture : MonoBehaviour
{ 
    public Texture2D _texture;
    public GameObject DaYangTu;
       
    IEnumerator Open()
    {
        string path = Application.persistentDataPath + "/Image" + "/placeholder.jpg";
        WWW www = new WWW("file://" + path);
        yield return www;
        Debug.Log(":" + www.error);
        _texture = new Texture2D(300, 300);
        _texture = www.texture;
        DaYangTu.GetComponent<UITexture>().mainTexture = _texture;
    }
       
    public void OnClick()
    {
        DaYangTu.SetActive(true);
        StartCoroutine(Open());
    }
    // Update is called once per frame
    void Update () {
           
    }
}
此处我使用的是使用WWW加载本地指定路径的文件,发布后可以更换图片,但名字要保持不变。 
最后是实现对图片的拖拽浏览,就是给你被拖拽的对象添加一个UI Drag Object脚本,注意不要忘记给被拖拽对象加Box Collider.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值