1、GUI自适应分辨率
public const float kDesignWidth = 960f; //游戏测试时宽度
public const float kDesignHeight = 640f; //游戏测试时高度
private Vector2 _scaleOffset = Vector2.one;
private float _scale = 1.0f;
void Start()
{
_scaleOffset.x = Screen.width / kDesignWidth;
_scaleOffset.y = Screen.height / kDesignHeight;
_scale = Mathf.Max(_scaleOffset.x, _scaleOffset.y);
}
void OnGUI()
{
if (_scale < 1)
{
GUI.skin = gameMenuGUISkinForSmall; //小字体
}
else
{
GUI.skin = gameMenuGUISkinForNormal;//正常字体
}
//绘制背景图
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), backgroundTex);
//绘制按钮
if (GUI.Button(new Rect(77 * _scaleOffset.x, 345 * _scaleOffset.y, 130 * _scaleOffset.x, 130 * _scaleOffset.y), resumeButtonTex, GUIStyle.none))
}
2、获取动画总的播放时间以及当前动画播放到哪一时间(老版动画系统)
当前动画总的时间:_animation[_currentAnimation.name].length / _animation[_currentAnimation.name].speed
动画播放完,执行OnAnimationFinished方法:
Invoke("OnAnimationFinished", _animation[_currentAnimation.name].length / _animation[_currentAnimation.name].speed);
private void OnAnimationFinished()
{
//some codes
}
当前动画播放到哪一时间:
//normalizedTime(float):动画当前规范化时间,1是动画结尾. 0.5是动画中间
_animation[_currentAnimation.name].normalizedTime
3、判断运行平台
//判断设备平台
public bool IsMobile
{
get
{
return (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android);
}
}
4、判断网络的可达性
//判断网络是否可用
public bool NetworkAvailable
{
get
{
return Application.internetReachability != NetworkReachability.NotReachable;
}
}
NotReachable:表示设备没有连接网络
ReachableViaCarrierDataNetwork:表示设备是通过运营商数据连接的
ReachableViaLocalAreaNetwork:表示设备是通过WiFi或有线网络连接的
5、判断设备方向
Input.deviceOrientation == DeviceOrientation.FaceDown 表示屏幕朝下
Unknown:设备的方向不能被确定。
Portrait:设备在纵向模式,设备直立并home按钮在底部。
PortraitUpsideDown:设备在纵向模式,但颠倒一下,设备直立并home按钮在顶部。
LandscapeLeft:设备在横向模式,设备直立并home按钮在右边。
LandscapeRight:设备在横向模式,设备直立并home按钮在左边。
FaceUp:设备保持与地面平行,屏幕的面向上。
FaceDown:设备保持与地面平行,屏幕的面向下。
6、获取触摸状态
Input.touchCount 获取手指触摸屏幕的数量
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
//some codes
}
}
Begin:手指已触摸屏幕。
Moved:手指在屏幕上移动。
Stationary:手指触摸屏幕,但并没有移动。
Ended:手指从屏幕上移开。这是一个触摸的最后状态。
Canceled:系统取消跟踪触摸,如用户把屏幕放到他脸上或超过五个接触同时发生。这是一个触摸的最后状态。
Input类的其他详细使用如下:
水平(Horizontal )和垂直(Vertical )被映射到w, a, s, d键和方向键
Fire1, Fire2, Fire3被分别映射到Ctrl,Option(Alt)和Command键
Mouse X 和 Mouse Y被映射到鼠标移动增量
Window Shake X 和 Window Shake Y 被映射到窗口的移动
7、可视化改变碰撞器的大小
选择碰撞器的对象,然后按下Shift,会出现碰撞器的控制柄,这样就可以用鼠标可视化的改变碰撞器大小。
在场景面板中展示的碰撞器都是绿色的,Mesh Collider除外,它的网格显示着碰撞的边界。
8、单例模式
public class GameController : MonoBehaviour
{
private static GameController _instance = null;
public static GameController SharedInstance
{
get
{
if (_instance == null)
{
_instance = GameObject.FindObjectOfType(typeof(GameController)) as GameController;
}
return _instance;
}
}
void Awake()
{
_instance = this;
}
private GameController()
{
}
}