/// Minimap camera.
/// This script use to control minimap camera
/// </summary>
using UnityEngine;
using System.Collections;
public class MinimapCamera : MonoBehaviour {
public static int zoomLevel; //zoom level
public static MinimapCamera Instance; //declare this to global script
//Private variable
private int zoomCurrent;
[HideInInspector]
public Transform Target;
void Start()
{
//Setting Default Value
//zoomLevel与zoomCurrent 控制小地图视角的缩放
zoomLevel = 3;
zoomCurrent = zoomLevel;
//实现摄像机跟随主角
Instance = this;
GameObject hero;
hero = GameObject.FindGameObjectWithTag("Player");
//Target 为Player
Target = hero.GetComponent<Transform>();
}
// Update is called once per frame
void Update () {
//Follow player
//摄像机获取player的坐标,实现小地图的摄像机跟随效果;
transform.position = new Vector3(Target.position.x,transform.position.y,Target.position.z);
ZoomManage ();
}
//控制小地图的最大和最小缩放程度;
void ZoomManage ()
{
//Check zoom limit
// 当zoomLevel 小于0时,将其值设为0,控制在最小缩放的值为0
if(zoomLevel < 0)
{
zoomLevel = 0;
}else if(zoomLevel > 4)
{
zoomLevel = 4;
}
}
//通过zoomLevel值得大小 来控制小地图视角大小的缩放,当zoomLevel小于zoomCurrent时,说明地图要缩小,此时相机的orthographicSize值要增加;
public void ZoomUpdate()
{
if(zoomLevel < zoomCurrent)
{
this.GetComponent<Camera>().orthographicSize += 3;
zoomCurrent = zoomLevel;
}else
// 当zoomLevel大于zoomCurrent时,说明地图要放大,此时相机的orthographicSize值要缩小;
if(zoomLevel > zoomCurrent)
{
this.GetComponent<Camera>().orthographicSize -= 3;
zoomCurrent = zoomLevel;
}
}
}
/// <summary>
/// Minimap.
/// This script use to call minimap on top-right screen
/// </summary>
using UnityEngine;
using System.Collections;
public class Minimap : MonoBehaviour {
private Vector2 defaultScreenRes; //Screen Resolution
[System.Serializable]
public class GUISetting
{
public Vector2 position;
public Vector2 size;
public Texture2D[] texture;
}
[System.Serializable]
public class LabelSetting
{
public Vector2 position;
public GUIStyle labelStyle;
}
[System.Serializable]
public class MinimapSetting
{
public Vector2 position;
public Vector2 size;
public Texture texture;
public Material renderMaterial;
}
[System.Serializable]
public class ButtonSetting
{
public Vector2 position;
public Vector2 size;
public GUIStyle buttonStlye;
}
public GUISetting frameMap,mapNameBar; //GUI setting
public MinimapSetting minimap; //Minimap setting
public LabelSetting mapName; //Map name setting
//引用 ButtonSetting 类,所有的图标都在 ButtonSetting里实实例化,在这个类里加载到界面;
public ButtonSetting zoomInBt,zoomOutBt; //button setting
// Use this for initialization
void Start () {
defaultScreenRes.x = 1920; //declare max screen ratio
defaultScreenRes.y = 1080; //declare max screen ratio
}
void OnGUI ()
{
if(!GameSetting.Instance.hideMinimap)
{
// Resize GUI Matrix according to screen size
ResizeGUIMatrix();
//Draw Minimap
Graphics.DrawTexture(new Rect(minimap.position.x,minimap.position.y,minimap.size.x ,minimap.size.y),minimap.texture,minimap.renderMaterial);
//Draw MinimapFrame
GUI.DrawTexture(new Rect(frameMap.position.x,frameMap.position.y,frameMap.size.x,frameMap.size.y),frameMap.texture[0]);
//Draw Map name bar
GUI.DrawTexture(new Rect(mapNameBar.position.x,mapNameBar.position.y,mapNameBar.size.x,mapNameBar.size.y),mapNameBar.texture[0]);
TextFilter.DrawOutline(new Rect(mapName.position.x ,mapName.position.y, 1000 , 1000)
,Application.loadedLevelName,mapName.labelStyle,Color.black,Color.white,2f);
//Draw zoom in button
//GUI.Button():Make a single press button. The user clicks them and something happens immediately.
if(GUI.Button(new Rect(zoomInBt.position.x,zoomInBt.position.y,zoomInBt.size.x,zoomInBt.size.y),"",zoomInBt.buttonStlye))
{
MinimapCamera.zoomLevel++;
MinimapCamera.Instance.ZoomUpdate();
}
//Draw zoom out button
if(GUI.Button(new Rect(zoomOutBt.position.x,zoomOutBt.position.y,zoomOutBt.size.x,zoomOutBt.size.y),"",zoomOutBt.buttonStlye))
{ //zoomLevel 减小,小地图视角要缩小;
MinimapCamera.zoomLevel--;
MinimapCamera.Instance.ZoomUpdate();
}
// Reset matrix after finish
GUI.matrix = Matrix4x4.identity;
}else
{
this.enabled = false;
}
}
void ResizeGUIMatrix()
{
// Set matrix
Vector2 ratio = new Vector2(Screen.width/defaultScreenRes.x , Screen.height/defaultScreenRes.y );
Matrix4x4 guiMatrix = Matrix4x4.identity;
guiMatrix.SetTRS(new Vector3(1, 1, 1), Quaternion.identity, new Vector3(ratio.x, ratio.y, 1));
GUI.matrix = guiMatrix;
}
}