NGUI是用C#編寫的Unity(有專業版和免費版)外掛程式,提供強大的UI系統和事件通知架構。
用NGUI外掛程式在unity3D裡面開發,需要如下步驟:
1.首先下載免費的NGUI外掛程式,有錢當然可以下載專業版!
:http://www.tasharen.com/get.php?file=NGUI
2.在Unity3d裡面匯入下載的NGUI外掛程式,匯入完記得refresh重新整理下,才會在unity3D功能表列出現NGUI。
3.匯入完畢後,在菜單NGUI裡面找到open the UI wizard,建立
4.添加完,繼續在菜單NGUI裡面找到open the widget wizard,添加計算機要用到的按鈕button、標籤label
5.添加完畢後,調整介面得到自己想要的效果介面
6.添加C#指令碼calc.cs,編寫代碼如下:
using UnityEngine;using System.Collections;public class calc : MonoBehaviour {public string strPutkeyCode;public UILabel strResult;//結果顯示public static string str1;//第一個運算元public static string str2;//第二個運算元public static string strOpt;//標記加減乘除符號int sum = 0;//計算結果void OnClick(){if(strPutkeyCode == "=")//按下=時開始計算{if(strOpt == "/"){sum =int.Parse(str2)/int.Parse(str1);}else if(strOpt == "*"){sum =int.Parse(str1)*int.Parse(str2);}else if(strOpt == "+"){sum =int.Parse(str1)+int.Parse(str2);}else if(strOpt == "-"){sum =int.Parse(str2)-int.Parse(str1);}str1 = "";str2 = sum.ToString();Debug.Log(sum);strResult.text = sum.ToString();}if(strPutkeyCode == "CE") //清零{Debug.Log("CE");strResult.text = "0";sum = 0;str1 = "";str2 = "";}if(strPutkeyCode == "B") //刪除一個數字{Debug.Log("B");str1=str1.Substring(0,str1.Length-1);if(str1 == ""){strResult.text = "0";return;}strResult.text = str1;}if(strPutkeyCode == "/" || strPutkeyCode == "*" ||strPutkeyCode == "-"||strPutkeyCode == "+"){strOpt=strPutkeyCode;//記錄按下的操作符,並把原先得到的運算元一str1賦給運算元二str2if(str1 != ""){str2=str1;}strResult.text = str2;Debug.Log("code---");str1="";}if(strPutkeyCode =="1" ||strPutkeyCode =="2" ||strPutkeyCode =="3" ||strPutkeyCode =="4" ||strPutkeyCode =="5" ||strPutkeyCode =="6" ||strPutkeyCode =="7" ||strPutkeyCode =="8" ||strPutkeyCode =="9" ||strPutkeyCode =="0"){str1 += strPutkeyCode;//拼接按下的數字strResult.text = str1;Debug.Log(str1);}}// Use this for initializationvoid Start () {}}
7.最後剩下的就是對象綁定了,把指令碼calc.cs和label綁定(即拖)到各個按鈕,並為按鈕鍵賦索引值,其中一個
結果如下:
下載代碼地址:http://download.csdn.net/detail/wyz365889/5753403