我做了一個實用的 選擇 程式碼,且提供 主畫面 與 選擇頁面 的切換
選擇頁面的 ok 與 Cancel 皆有作用,如果按下 ok 將套用選擇頁面的數字
如果按下取消,則還原先前的數字,在選擇頁面上的數字並不會被套用
且該程式碼可用於Android 與 iOS 等手機上
選擇頁面還可以用觸控滑動,我想該程式碼可以幫助手機開發者很大的忙
運行畫面(主畫面):
運行畫面(選擇畫面):
程式碼:
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
int Main_Menu = 0;
int SelectButton_Menu = 1;
//
int Now_Menu = 0;
void OnGUI(){
if(Now_Menu == Main_Menu){
MainMenu ();
}else if(Now_Menu == SelectButton_Menu){
SelectButton_OnGUI ();
}
}
void Update(){
SelectButton_Update ();
}
string mySelect = "0";
string mySelect_New = "";
Vector2 scrollPosition;
void MainMenu(){
GUI.Label (new Rect(Screen.width/2-75,5, 200,20), "Your selected button is : " + mySelect);
//-------------------------------------------------------------
if(GUI.Button(new Rect(Screen.width/2-50,Screen.height/2-25, 100, 50),"Select Button")){
Now_Menu = SelectButton_Menu;
scrollPosition.y = 0;
mySelect_New = "0";
}
}
Touch touch;
int buttonHight = 50;
int buttonSpacing = 10;
int buttonCount = 100;
void SelectButton_OnGUI(){
GUI.Label (new Rect(Screen.width/2-75,5, 200,20), "Your selected button is : " + mySelect_New);
//-------------------------------------------------------------
scrollPosition = GUI.BeginScrollView(
new Rect(5,50,Screen.width-10,Screen.height-110),
scrollPosition,
new Rect(0,0,Screen.width-40,(buttonHight+buttonSpacing)*buttonCount));
for(int i = 0;i < buttonCount; i++){
if(GUI.Button(new Rect(0,i*(buttonHight+buttonSpacing),Screen.width-30,buttonHight),"" + i)){
mySelect_New = "" + i;
}
}
GUI.EndScrollView ();
//-------------------------------------------------------------
if(GUI.Button(new Rect(5,Screen.height-55, Screen.width/2-5,50), "Cancel")){
Now_Menu = Main_Menu;
}
//-------------------------------------------------------------
if(GUI.Button(new Rect(Screen.width/2+5,Screen.height-55, Screen.width/2-10,50), "OK")){
mySelect = mySelect_New;
Now_Menu = Main_Menu;
}
}
void SelectButton_Update(){
if(Input.touchCount > 0){
touch = Input.touches[0];
if (touch.phase == TouchPhase.Moved){
scrollPosition.y += touch.deltaPosition.y;
}
}
}
}
留言列表