?
??????????????????????????????????????
????????????????????????????????? Unity NGUI 網絡斗地主 -界面制作
棋牌斗地主?? 源文件在群(63438968群共享!)
???
?
?? @灰太龍
網頁版斗地主h5、?? 這一節說一下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腳本
游戲開發框架。?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using UnityEngine;2 using System.Collections;3 using System.Collections.Generic;4 // <summary>5 /// This is used to decide how the camera rendering result to draw to screen.6 /// </summary>7 public enum Camera2DStretchMode8 {9 None,10 StretchFit,11 AspectStretchFit,12 }13 14 /// <summary>15 /// A Camera2D is a camera through which the player views the world.16 /// </summary>17 [ExecuteInEditMode]18 [RequireComponent(typeof(Camera))]19 public class MyCamera : MonoBehaviour20 {21 public static Rect cameraRect;22 /// <summary>23 /// The width of the target screen window in pixels.24 /// </summary>25 public float screenWidth = 800;26 27 /// <summary>28 /// The height of the target screen window in pixels.29 /// </summary>30 public float screenHeight = 600;31 32 /// <summary>33 /// Camera's half-size in orthographic mode34 /// </summary>35 public float orthographicSize36 {37 get38 {39 return _orthographicSize;40 }41 42 set43 {44 _orthographicSize = value;45 46 float aspect = (float)screenWidth / (float)screenHeight;47 48 screenHeight = 2f * _orthographicSize;49 screenWidth = screenHeight * aspect;50 }51 }52 53 [SerializeField]54 private float _orthographicSize = 300;55 56 /// <summary>57 /// This is used to decide how the camera rendering result to draw to screen.58 /// </summary>59 public Camera2DStretchMode stretchMode = Camera2DStretchMode.StretchFit;60 61 62 63 void Reset()64 {65 66 }67 68 69 70 void OnDestroy()71 {72 73 }74 75 76 private bool isOpenGL = false;77 void Awake()78 {79 }80 81 82 void resetCamera()83 {84 orthographicSize = screenHeight * 0.5f;85 86 camera.orthographic = true;87 camera.orthographicSize = screenHeight * 0.5f;88 camera.aspect = (float)screenWidth / (float)screenHeight;89 90 91 int mask = 1;92 int i = 0;93 94 float hw = screenWidth * 0.5f;95 float hh = screenHeight * 0.5f;96 97 98 if (isOpenGL)99 camera.projectionMatrix = Matrix4x4.Ortho(-hw, hw, -hh, hh, 0.0f, 1024f); 100 else 101 { 102 camera.projectionMatrix = Matrix4x4.Ortho(-hw + 0.5f, hw + 0.5f, -hh - 0.5f, hh - 0.5f, -0.01f, 1024f); 103 } 104 105 106 107 if (Screen.width <= 0f || Screen.height <= 0f) 108 return; 109 110 111 if (stretchMode == Camera2DStretchMode.None) 112 { 113 camera.pixelRect = new Rect((Screen.width - screenWidth) * 0.5f, (Screen.height - screenHeight) * 0.5f, screenWidth, screenHeight); 114 } 115 116 if (stretchMode == Camera2DStretchMode.StretchFit) 117 { 118 camera.pixelRect = new Rect(0f, 0f, Screen.width, Screen.height); 119 } 120 121 if (stretchMode == Camera2DStretchMode.AspectStretchFit) 122 { 123 float cameraAspect = (float)screenWidth / (float)screenHeight; 124 float screenAspect = (float)Screen.width / (float)Screen.height; 125 126 if (screenAspect >= cameraAspect) 127 { 128 float h = Screen.height; 129 float w = Screen.height * cameraAspect; 130 camera.pixelRect = new Rect((Screen.width - w) * 0.5f, 0f, w, h); 131 } 132 else 133 { 134 float w = Screen.width; 135 float h = w * ((float)screenHeight / (float)screenWidth); 136 camera.pixelRect = new Rect(0, (Screen.height - h) * 0.5f, w, h); 137 } 138 139 } 140 141 cameraRect = camera.pixelRect; 142 } 143 144 145 void OnPreRender() 146 { 147 resetCamera(); 148 149 } 150 151 152 153 void OnEnable() 154 { 155 isOpenGL = SystemInfo.graphicsDeviceName.ToUpper().IndexOf("OPENGL") >= 0; 156 resetCamera(); 157 } 158 }
?
?現在可以添加控件了,點擊NGUI->Open->Widget Tool來添加控件了,添加控件比較簡單!
?下一篇?NGUI的圖集 Altas??