首先,是要新建一个UIRoot(使用的NGUI开发),因为一个项目中有时候往往一个UIRoot无法搞定,需要分割出一部分进行单独的处理,所以大家要视情况而定。
复制代码
接下来是要对图片的动态载入
复制代码
最后是实现对图片的拖拽浏览,就是给你被拖拽的对象添加一个UI Drag Object脚本,注意不要忘记给被拖拽对象加Box Collider.
复制代码
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.
本文介绍如何在Unity中利用NGUI实现小地图缩放功能,并控制Camera的视野大小。同时,介绍了如何动态加载本地图片到游戏中的方法。
8858

被折叠的 条评论
为什么被折叠?



