2014年5月21日 星期三

Unity GUI Window 及 Toggle(可製作修改按鍵)

Unity Web Player | Test
大概需要這些的遊戲物件(由圖表示)


在這裡就不在多做,如何建立物件的介紹



程式碼部 分為兩個:

LayoutKeybroad 類別 請放在Camera

using UnityEngine;
using System.Collections;

public class LayoutKeybroad : MonoBehaviour {
//GUIwindowSw
private int swNumber = 1;

// GUI windown default width/ height and GUI width/hight
public float windowWidth = 1024f, windowHeight = 768f,guiWidth = 120f, guiHeight = 30f; 

//GUI neme
public string guiLabel_1, guiLabel_2, guiLabel_3;
//button SW
public int buttonSw1 = 1, buttonSw2 = 1, buttonSw3 = 1;

// Greate new GameObjcet
public GameObject keybroadSystemSelect1, keybroadSelectImage;

void GameGuide ()
{
GUI.Window (0, new Rect(Screen.width/windowWidth + 120f, Screen.height/windowHeight, 400, 300), DoMyWindow, "Player Keybroad Setting");
}


/**
* gameObjcet add Script GetComponent<ScriptName>()is Sw
* button sw is number two multiple is true else false
*/
public void DoMyWindow (int windowID)
{
if (GUI.Button (new Rect (Screen.width / windowWidth + 20, Screen.height / windowHeight + 30f, guiWidth, guiHeight), guiLabel_1)) {
buttonSw1 += 1;
if(buttonSw1 %2 == 0){
keybroadSystemSelect1.GetComponent<KeybroadSystemSelect1>().enabled = true;
}else if(buttonSw1 %2 != 0){
keybroadSystemSelect1.GetComponent<KeybroadSystemSelect1>().enabled = false;
keybroadSelectImage.GetComponent<MeshRenderer> ().enabled = false;
}
}

/**
* guiwindwnSw is number two multiple is true else false
* true Run GameGuide fashion
*/
void OnGUI ()
{
if (GUI.Button (new Rect (20, 80, 200, 50), "Player Keybroad Setting"))
swNumber += 1;
if (swNumber %2 == 0){
keybroadSelectImage.GetComponent<MeshRenderer> ().enabled = true;
GameGuide ();
}else if(swNumber %2 != 0){
keybroadSelectImage.GetComponent<MeshRenderer> ().enabled = false;
keybroadSystemSelect1.GetComponent<KeybroadSystemSelect1>().enabled = false;
keybroadSelectImage.renderer.material.mainTexture = null;
}
}
}

KeybroadSystemSelect1 類別,請放在空物件

using UnityEngine;
using System.Collections;

public class KeybroadSystemSelect1 : MonoBehaviour {

// gui windown default width/ height and GUI width/hight
public float windowWidth = 256f, windowHeight = 310f, guiWidth, guiHeight; 
//buttonSelectSw1 bool 
private bool buttonSelectSw1 = false;

// Layout Keybroad Image
public GameObject layoutKeybroadSelect;
public Texture[] attack, jump, imageNull;

//gui name
private string upArrowDownArrowLeftArrwRigntArrow = "Sorry default keybroad up and down/left and right can't be modified",
  attackName = "Attack", jumpName = "Jump";
//keybroad secelct tooleActtackSw and button sw
public static bool toggleAttackSw1 = false, toggleAttackSw2= false, toogleAttackLayoutSw = false,  
toogleJumpSw1 = false, toogleJumpSw2 = false, keybraodselect1 = false, keybroadselect2 = false;

void OnGUI(){

KeybroadSelect ();
}

/**
* gui color is green contain exterior literal
*/
void KeybroadSelect(){
GUI.color = Color.green;
GUI.Window (1, new Rect(Screen.width/windowWidth + 130f, Screen.height/windowHeight + 20, 410, 300), KeybroadSelect1, "Keybroad Selcet 1");
}

/**
* gui color is yellow contain exterior literal
* gui Attackbutton is ture ToogleAttackSelect fashion
* gui Jumpbutton is true ToogleJumpSelect fashion
*/
void KeybroadSelect1(int windowID){

GUI.color = Color.yellow;
GUI.Label(new Rect(Screen.width/windowWidth + 15f, Screen.height/windowHeight + 20f, guiWidth + 450f, guiHeight),upArrowDownArrowLeftArrwRigntArrow);

if (GUI.Button (new Rect (Screen.width / windowWidth + 15f, Screen.height / windowHeight + 60f, guiWidth, guiHeight),attackName)) {
keybraodselect1 = !keybraodselect1;
keybroadselect2 = false;
}

if (GUI.Button (new Rect (Screen.width / windowWidth + 15f, Screen.height / windowHeight + 120f, guiWidth, guiHeight), jumpName)){
keybroadselect2 = !keybroadselect2;
keybraodselect1 = false;
}

if(GUI.Button(new Rect(Screen.width/windowWidth/windowWidth + 150f, Screen.height/windowHeight + 220f, guiWidth, guiHeight),"Enter")){
toggleAttackSw2 = false;
toggleAttackSw1 = false;
toogleJumpSw1 = false;
toogleJumpSw2 = false;
layoutKeybroadSelect.renderer.material.mainTexture = imageNull[0];
this.enabled = false;
}

if(keybraodselect1){
ToogleAttackSelect();
}
if (keybroadselect2) {
ToogleJumpSelect();
}
}

/**
* gui toggle = true/false waint player select
* sw control toggle true and false
*/
void ToogleAttackSelect(){

bool sw = false;

toggleAttackSw1 = GUI.Toggle(new Rect(Screen.width/windowWidth + 150f, Screen.height/windowHeight + 60f, guiWidth, guiHeight), toggleAttackSw1, "Mouse left Key");
toggleAttackSw2 = GUI.Toggle(new Rect(Screen.width/windowWidth + 150f, Screen.height/windowHeight + 100f, guiWidth, guiHeight), toggleAttackSw2, "Keybroad is J");

if(toggleAttackSw1){
GUI.Label(new Rect(Screen.width/windowWidth + 158f, Screen.height/windowHeight + 150f, guiWidth+60f, guiHeight),"AnyWhere input mouse left key");
layoutKeybroadSelect.renderer.material.mainTexture = attack[2];
if(Input.GetMouseButton(0)){
layoutKeybroadSelect.renderer.material.mainTexture = attack[3];
sw = !sw;
if(sw && toggleAttackSw2){ 
sw = !sw; 
toggleAttackSw1 = !toggleAttackSw1; 
toggleAttackSw2 = !toggleAttackSw2;
}
}
if (toggleAttackSw2) {
layoutKeybroadSelect.renderer.material.mainTexture = attack[0];
if(Input.GetKeyDown(KeyCode.J)){
layoutKeybroadSelect.renderer.material.mainTexture = attack[1];
}
sw = !sw;
if(sw && toggleAttackSw1){
toggleAttackSw2 = !toggleAttackSw2;
toggleAttackSw1 = !toggleAttackSw1;
}
}
}

void ToogleJumpSelect(){
bool sw = false;

toogleJumpSw1 = GUI.Toggle (new Rect (Screen.width / windowWidth + 150f, Screen.height / windowHeight + 60f, guiWidth, guiHeight), toogleJumpSw1, "Keybroad is Space");
toogleJumpSw2 = GUI.Toggle (new Rect (Screen.width / windowWidth + 150f, Screen.height / windowHeight + 100f, guiWidth, guiHeight), toogleJumpSw2, "Keybroad is K");

if(toogleJumpSw1){
layoutKeybroadSelect.renderer.material.mainTexture = jump[0];
if(Input.GetKeyDown(KeyCode.Space)){
layoutKeybroadSelect.renderer.material.mainTexture = jump[1];
}
sw = !sw; 
if(sw && toogleJumpSw2){ 
sw = !sw; 
toogleJumpSw1 = !toogleJumpSw1; 
toogleJumpSw2 = !toogleJumpSw2;
}
}
if (toogleJumpSw2) {
layoutKeybroadSelect.renderer.material.mainTexture = jump[0];
if(Input.GetKeyDown(KeyCode.K)){
layoutKeybroadSelect.renderer.material.mainTexture = jump[2];
}
sw = !sw;
if(sw && toggleAttackSw1){
toogleJumpSw2 = !toogleJumpSw2;
toogleJumpSw1 = !toogleJumpSw1;
}
}
}
}




2014年5月20日 星期二

Unity 血條製作(動態血條)

Unity Web Player | Test
動態血條製作方式:

1. 先建立兩個 GUITextrue。




2. 再建立空物件把GUITextrue弄成子物件後,再建立程式去撰寫程式去控制,
GUITextrue Width。

4. 再把血量圖片拉進去GUITextrue裡面的Textrue屬性。

5. 再把程式套用到空物件上面。


6. 程式部分: 

using UnityEngine;
using System.Collections;

public class Hp_Data : MonoBehaviour
{
public GameObject hp1, hp2; // GUITextrue 物件
public int hp1Number, hp2Number;  // 血量輸入
private bool sw = false;   // 控制扣血加血開關

void OnGUI(){

if (hp1Number >= 0){
if (hp2Number > hp1Number){
hp2Number -= 1;
hp2.guiTexture.pixelInset = new Rect (-150f, 120f, hp2Number, 16f);
}
}


if ((GUI.Button(new Rect(100, 80, 100, 50),"- HP"))) {
if (hp1Number > 0){
hp1Number -= 10;
hp1.guiTexture.pixelInset = new Rect (-150f, 120f, hp1Number, 16f);
}
}

if(GUI.Button(new Rect(280f, 80, 100, 50),"+ HP") && hp2Number == hp1Number){
sw = true;
if(sw){
if (hp2Number < 190){
hp2Number += 10;
hp2.guiTexture.pixelInset = new Rect (-150f, 120f, hp2Number, 16f);
}
}
if (hp2Number  >= hp1Number){
Debug.Log(hp2Number);
if(hp1Number < 190){
hp1Number += 10;
Debug.Log("open");
hp1.guiTexture.pixelInset = new Rect (-150f, 120f, hp1Number, 16f);
}
}
}
}
}




2014年5月15日 星期四

Unity 遊戲引擎 基本介紹

Unity3D是一個用於創建諸如三維視訊遊戲、建築可視化、實時三維動畫等類型互動內容的綜合型創作工具。Unity類似於DirectorBlenderVirtoolsTorque Game Builder等利用交互的圖型化開發環境為首要方式的軟體其編輯器執行在Windows 和Mac OS X下,可發布遊戲至WindowsWiiOSXiOS平台。也可以利用Unity web player外掛程式發布網頁遊戲,支援Mac 和Windows的網頁瀏覽。它的網頁播放器也被Mac widgets所支援。

所支持的語法: C#、javaScript、BooScript
可匯入模型檔: obj、fbx,blend。