2015年1月26日 星期一

Unity GUI 擴充


Unity  GUI 擴充在這裡適用Unity 本身的GUI來進行自己的擴充工具的類別。

這邊擴充了ImageButton及Windws與HorizontalSlider三個比較常用的工具,還可以繼續在擴充。


GUIComponents(自訂) class:

using UnityEngine;
using System.Collections;

public class GUIComponents{

//ImageButton OnClick View
bool imageButtonOnClick;

// windwosTitleName and WindwosIdNumber
public string windowsTitle;
public int windowsId;

/**
* ImageButton
* TODO ImageButton function(wordPosistionSize, image, style) 
*/
public bool getImageButton(Rect rectScreenRect, Texture image, GUIStyle style){

imageButtonOnClick = GUI.Button (rectScreenRect, image, style);

return imageButtonOnClick;
}

/**
* setAlertDialog
* TODO setAlertDialog Message
* @param {string}Title
* @param {string}Message
*/
public void setAlertDialog(string Title){
this.windowsTitle = Title;
windowsId++;
}

/**
* Windows
* TODO Windows function(wordPosistionSize, windwfunctuin, title)
*/
public void getWindwos (int id, Rect screenRect, GUI.WindowFunction windowFunctuin, string title){

title = this.windowsTitle;
id = this.windowsId;
GUI.Window (id, screenRect, windowFunctuin, title);
}

/**
* HorizontalSlider
* TODO HorizontalSlider function(wordPosistionSize, SliderValue, MaxValue, SliderStyle, ThumbStyle)
*/
public float getHorizontalSlider (Rect screenRect, float sliderValue, float sliderMaxValue, GUIStyle slider, GUIStyle thumb){

screenRect.x += screenRect.width; 
sliderValue = GUI.HorizontalSlider (screenRect, sliderValue, 0.0f, sliderMaxValue, slider, thumb);

return sliderValue;
}
}

--------------------------------------------------------------------------------

套用腳本:

using UnityEngine;
using System.Collections;

public class SystemSetting : MonoBehaviour{

//ImageButton in a Image and buttonStyle
public Texture image;
public GUIStyle buttonstyle;

/**
* WindowSetting
* OnGUI Windows Setting
* WindowsfunctionSetting can in another class use unction
*/
public void OnGUI(){

DpiResolution dpiResolution = new DpiResolution ();
GUIComponents guiCompoents = new GUIComponents ();
Rect wordPosition = new Rect (dpiResolution.getScreenWidth()/dpiResolution.getScreenWidth(),dpiResolution.getScreenHeight()/dpiResolution.getScreenWidth(), 
                             dpiResolution.getScreenWidth(), dpiResolution.getScreenHeight()/1.2f);

guiCompoents.setAlertDialog ("123");
guiCompoents.getWindwos (guiCompoents.windowsId, wordPosition, WindwosMessage, guiCompoents.windowsTitle);

}


void WindwosMessage(int id){

GUIComponents guiCompents = new GUIComponents ();
DpiResolution dpiResolution = new DpiResolution ();
Rect wordPosition = new Rect (dpiResolution.getScreenWidth()/dpiResolution.getScreenWidth(),dpiResolution.getScreenHeight()/dpiResolution.getScreenWidth(), 
                             image.width/5, image.height/2);
if(guiCompents.getImageButton (wordPosition,image, buttonstyle)){
Debug.Log("ok");
}
}
}

--------------------------------------------------------------------------------

執行結果:



2015年1月4日 星期日

FPS Gun Game (Simple / Basic)

注意: 

1. TextMesh 這個遊戲物件,這邊內用16:9 解析度製作,如果TextrueMesh 位置有誤,請自行調整。

2. 我本人版本Unity 4.6 所製作,在模型animation 如果操作有誤,請遵守4.0 版本 / 3.6版本的做法。

3. 程式部分: [Range] 以設計者設定為主,這邊已用及行拉式選擇,方便後面以客製化為主,方便以後調整即可。

4. 這邊子彈用拖曳效果所製作(Trail Renderder)。

5. 這邊使用Box Collider 的方式做成彈道及彈道射程。

---------------------------------------------------------------------
Clip(彈夾腳本) :

using UnityEngine;
using System.Collections;

public class Clip : MonoBehaviour {

[Range(0, 30)]
public int bullet = 0;

[Range(0, 2f)]
public float offsetZ;

public TextMesh bulletDateTxtMag;

public ReservcClip clipBulletNumner;


void BulletDateTxtMag(int bulletNumber){
bulletDateTxtMag.text = "Bullet: " + bullet + "  Clip: " + clipBulletNumner.clipInBullet;
}

// Update is called once per frame
void Update () {

if(Input.GetMouseButton(0)){
bullet -= 1;
}
if(bullet <= 0){
bullet = 0;
}
BulletDateTxtMag(bullet);
}
}

---------------------------------------------------------------------
MainGun(主槍腳本) :

using UnityEngine;
using System.Collections;

public class MainGun : MonoBehaviour
{
[Range(1, 500)]
public int sizeY;
[Range(1, 30)]
public int centery;
public Animator gunReloadingAnimation;
public Clip clipDate;

public GameObject bullets;
// Update is called once per frame
void Update (){

gunReloadingAnimation.GetComponent<Animator> ();

if(clipDate.bullet > 0){
if(Input.GetMouseButtonDown(0)){
Frie();
}else{
NoFire();
}
}else if(clipDate.bullet == 0){
NoFire();
}
}

void Frie(){

gunReloadingAnimation.SetInteger ("FrieSw", 1);

Instantiate (bullets, transform.position, transform.rotation);

gameObject.GetComponent<BoxCollider> ().size = new Vector3 (0, 0, sizeY);
gameObject.GetComponent<BoxCollider> ().center = new Vector3 (0, 0, centery);

}

void NoFire(){
gunReloadingAnimation.SetInteger ("FrieSw",2);
gameObject.GetComponent<BoxCollider> ().size = new Vector3 (0, 0, 0);
gameObject.GetComponent<BoxCollider> ().center = new Vector3 (0, 0, 0);
}
}

---------------------------------------------------------------------

ReservcClip(備用彈夾):

using UnityEngine;
using System.Collections;

public class ReservcClip : MonoBehaviour {

[Range(0, 90)]
public int clipInBullet;
[Range(1, 30)]
public int totalnumberofbullets;

public Clip bulletNumber;
public Animator gunReloadingAnimation;

// Update is called once per frame
void Update () {

gunReloadingAnimation.GetComponent<Animator> ();

if(clipInBullet >= totalnumberofbullets & bulletNumber.bullet < totalnumberofbullets){
if (Input.GetKeyDown (KeyCode.R)){
Switchingclip ();
}
}else if(clipInBullet < totalnumberofbullets){

if(Input.GetKeyDown(KeyCode.R)){
UpdateClip();
}
}
}

void UpdateClip(){

gunReloadingAnimation.SetInteger ("FrieSw",3);
bulletNumber.bullet = clipInBullet;

if(bulletNumber.bullet == clipInBullet){
clipInBullet = 0;
gunReloadingAnimation.SetInteger ("FrieSw",4);
}

}

void Switchingclip(){

int complement;
complement = totalnumberofbullets - bulletNumber.bullet;
if(clipInBullet > 0){
gunReloadingAnimation.SetInteger ("FrieSw",3);
clipInBullet -= complement;
if(bulletNumber.bullet < totalnumberofbullets){
bulletNumber.bullet += complement;

}
}
}
}

---------------------------------------------------------------------

BulletPosition (子彈腳本):

using UnityEngine;
using System.Collections;

public class BulletPosition : MonoBehaviour {

[Range(50, 500)]
public int speed;
// Update is called once per frame
void Update () {

transform.Translate (Vector3.forward * Time.deltaTime * speed);

Destroy(this.gameObject, 1);
}

----------------------------------------------------------------

結果圖: