Unity NGUI 网络斗地主 -界面制作
源文件在群(63438968群共享!)
@灰太龙
这一节说一下NGUI的界面摆放,并且教会大家使用NGUI的自适应功能!
在这里感谢@Gamer,是他给我的一些指教和资料!
1.首先在菜单栏中选择NGUI->Open->UI Wizard,这个时候会弹出一个窗体
其中,默认的层是Default,如果是这个层的话,就会有问题!
自己试一试就知道了,在这里不阐述了!
(注解:新建Layer,在Inspector中,最后一个命令Add Layer...添加一个层,即可,名字可以任意取!)
那么会在Hierarchy视图中自动生成几个物体,截图:
UI Root(2D)为根物体,Camer为UI摄像机,
在这儿删除两个物体,
1.UI Root(2D)身上的脚本,并且将这个物体的缩放值(x,y,z)都改成1
2.删除Anchor物体!
在Camera上添加一个脚本,截图:
这个脚本随后上传,其中Screen Width和Screen Height为在Game视图中的窗口大小,(点击Game视图中的Stats按钮,可以看到当前游戏窗口的大小的),经过以上步骤的操作,控件都会是自适应的!
贴上MyCamera.cs脚本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// <summary>
/// This is used to decide how the camera rendering result to draw to screen.
/// </summary>
public enum Camera2DStretchMode
{
None,
StretchFit,
AspectStretchFit,
} /// <summary>
/// A Camera2D is a camera through which the player views the world.
/// </summary>
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class MyCamera : MonoBehaviour
{
public static Rect cameraRect;
/// <summary>
/// The width of the target screen window in pixels.
/// </summary>
public float screenWidth = ; /// <summary>
/// The height of the target screen window in pixels.
/// </summary>
public float screenHeight = ; /// <summary>
/// Camera's half-size in orthographic mode
/// </summary>
public float orthographicSize
{
get
{
return _orthographicSize;
} set
{
_orthographicSize = value; float aspect = (float)screenWidth / (float)screenHeight; screenHeight = 2f * _orthographicSize;
screenWidth = screenHeight * aspect;
}
} [SerializeField]
private float _orthographicSize = ; /// <summary>
/// This is used to decide how the camera rendering result to draw to screen.
/// </summary>
public Camera2DStretchMode stretchMode = Camera2DStretchMode.StretchFit; void Reset()
{ } void OnDestroy()
{ } private bool isOpenGL = false;
void Awake()
{
} void resetCamera()
{
orthographicSize = screenHeight * 0.5f; camera.orthographic = true;
camera.orthographicSize = screenHeight * 0.5f;
camera.aspect = (float)screenWidth / (float)screenHeight; int mask = ;
int i = ; float hw = screenWidth * 0.5f;
float hh = screenHeight * 0.5f; if (isOpenGL)
camera.projectionMatrix = Matrix4x4.Ortho(-hw, hw, -hh, hh, 0.0f, 1024f);
else
{
camera.projectionMatrix = Matrix4x4.Ortho(-hw + 0.5f, hw + 0.5f, -hh - 0.5f, hh - 0.5f, -0.01f, 1024f);
} if (Screen.width <= 0f || Screen.height <= 0f)
return; if (stretchMode == Camera2DStretchMode.None)
{
camera.pixelRect = new Rect((Screen.width - screenWidth) * 0.5f, (Screen.height - screenHeight) * 0.5f, screenWidth, screenHeight);
} if (stretchMode == Camera2DStretchMode.StretchFit)
{
camera.pixelRect = new Rect(0f, 0f, Screen.width, Screen.height);
} if (stretchMode == Camera2DStretchMode.AspectStretchFit)
{
float cameraAspect = (float)screenWidth / (float)screenHeight;
float screenAspect = (float)Screen.width / (float)Screen.height; if (screenAspect >= cameraAspect)
{
float h = Screen.height;
float w = Screen.height * cameraAspect;
camera.pixelRect = new Rect((Screen.width - w) * 0.5f, 0f, w, h);
}
else
{
float w = Screen.width;
float h = w * ((float)screenHeight / (float)screenWidth);
camera.pixelRect = new Rect(, (Screen.height - h) * 0.5f, w, h);
} } cameraRect = camera.pixelRect;
} void OnPreRender()
{
resetCamera(); } void OnEnable()
{
isOpenGL = SystemInfo.graphicsDeviceName.ToUpper().IndexOf("OPENGL") >= ;
resetCamera();
}
}
现在可以添加控件了,点击NGUI->Open->Widget Tool来添加控件了,添加控件比较简单!
下一篇 NGUI的图集 Altas