using UnityEngine;
using System.Collections;
public class Loop : MonoBehaviour {
public Texture2D[] btnTex = new Texture2D[9];
Rect[] btnRect = new Rect[9];
Rect[] rectInit = new Rect[9];
float minSize = 64.0f;
float maxSize = 128.0f;
float maxDis = 256;
float minY;
float btnSpace = 16.0f;//图标之间的空隙
float leftSpace;//图标左边的空隙
void Start () {
minY = Screen.height - minSize;
float tatalWidth = minSize * btnTex.Length + btnSpace * (btnTex.Length-1);//图标两边的总长度,btnTex.Length指几张图
leftSpace = (Screen.width - tatalWidth) * 0.5f;//使图片到中间位置时,左边需要多少空白位置
for(int i = 0; i< btnTex.Length; i++){
rectInit[i] = new Rect((minSize+btnSpace)*i + leftSpace,minY,minSize,minSize);//通过设置初始直接赋给btnRect
}//每个图形的位置
}//图形都依次排在中间且有空隙
void Update () {
if (Input.mousePosition.y <= minSize) { //当鼠标低于图标时
for (int i = 0; i < btnTex.Length; i++) {//对于每一张图片
float centerX = btnRect [i].x + btnRect [i].width * 0.5f;//每个图形的中心点
float disMouseToCenterX = Mathf.Abs (Input.mousePosition.x - centerX);//鼠标距离中心点的值
if (disMouseToCenterX <= maxDis) {//如果鼠标距离中心点的位置小于最大距离
btnRect [i].width = crsp (0, maxDis, disMouseToCenterX, maxSize, minSize);//放大图形宽度
} else {
btnRect [i].width = minSize;//否则没变化
}
btnRect [i].height = btnRect [i].width;//根据上方情况放大高度或不变
btnRect [i].y = Screen.height - btnRect [i].height;//确定图形应处于y轴哪个位置
}
float totalBtnWidth = btnRect [0].width;//初始所有图标总长度为第一個图标长度
for (int i = 1; i < btnTex.Length; i++) {
totalBtnWidth += btnRect [i].width + btnSpace;
}//通过相加计算出所有图标的总长度,有空隙
float leftBtnWidth = (Screen.width - totalBtnWidth) * 0.5f;//计算出第一個图标距左边的距离
btnRect [0].x = leftBtnWidth;//确定距离为第一個图标的x轴位置
for (int i = 1; i < btnTex.Length; i++) {
btnRect [i].x = btnRect [i - 1].x + btnRect [i - 1].width + btnSpace;
}//确定每个图标应处于x轴哪个位置
} else {//当鼠标不在范围内,图标仍旧是原来的样子
for(int i = 0; i< btnTex.Length; i++){
btnRect[i] = rectInit[i];
}
}
}
void OnGUI (){//画出图形
for(int i = 0; i < btnTex.Length; i++){
GUI.DrawTexture (btnRect[i],btnTex[i]);
}
}
float crsp (float a, float b, float x, float m, float n){ //求扩展后对应的点
float y = m - (a - x) * (m - n) / (a - b);
return y;
}
}
【Unity3d学习笔记】Mac图标
最新推荐文章于 2020-09-24 11:12:16 发布