善用繼承可以讓遊戲有更多的變化且減少無意義的COPY&PASTE(?)
U3D的JS繼承用法如下
class Parent {
function Start () {
print("parent's Start");
A();
}
function A () {
print("parent's A");
B();
}
function B () {
print("parent's B");
C();
}
function C () {
print("parent's C");
}
}
class Children extends Parent {
function A () {
print("Children's A");
B();
}
function C () {
super.C();
print("Children's C");
}
}
如果在GameObject上附加Children時會發現此GameObject的執行結果為
parent's Start
Children's A
parent's B
parent's C
Children's C
在寫怪物AI時用繼承的方式會輕鬆很多,底層先寫好每隻怪都會做的動作(移動、攻擊等等),繼承後就可以按不同需求附加個別處理如(特殊攻擊)。
2011年9月22日 星期四
2011年9月15日 星期四
UNITY3D - 圖層觀念
U3D裡面有2D物件也有3D物件,因此要注意物件之間是否會造成顯示異常。
2D世界其實跟3D世界不屬於同一個世界...
可以把2D世界當成是貼在鏡頭前,所以2D物件一定會在介面上擋到3D物件。
而2D世界中的圖片會依據transform.position.z來當作圖層設定,z軸越大表示圖越上面。
例:
A圖片z軸為0
B圖片z軸為1
C圖片z軸為2
呈現出來就會變成
┌───────┐
│ ┌───────┐
│ A │ ┌───────┐
│ │ B │ │
└───│ │ C │
└───│ │
└───────┘
要注意的是在OnGUI底下畫出來的東西圖層是最高的,所以2D物件也會被OnGUI的東西蓋到喔。
2D世界其實跟3D世界不屬於同一個世界...
可以把2D世界當成是貼在鏡頭前,所以2D物件一定會在介面上擋到3D物件。
而2D世界中的圖片會依據transform.position.z來當作圖層設定,z軸越大表示圖越上面。
例:
A圖片z軸為0
B圖片z軸為1
C圖片z軸為2
呈現出來就會變成
┌───────┐
│ ┌───────┐
│ A │ ┌───────┐
│ │ B │ │
└───│ │ C │
└───│ │
└───────┘
要注意的是在OnGUI底下畫出來的東西圖層是最高的,所以2D物件也會被OnGUI的東西蓋到喔。
標籤:
UNITY 3D
2011年8月31日 星期三
UNITY3D - 2D圖動畫工具
遊戲中常常會用到一些2D連續圖來做UI特效,但是UNITY 3D本身不支援GIF格式的檔案,基於這些需求而寫了以下這隻程式。
目前的功能有:
1.播放一次
2.循環播放
3.淡入
4.淡出
5.放大
6.縮小
7.上升
8.下降
變數功能:
aniplayers :動畫詳細設定
size:有幾段動畫要播放
aniTexture:放連續圖的位置,需依序放入。
playTime:這段動畫的播放時間長度
frequency:時間內播放次數(淡入、淡出、放大、縮小、上升、下降無作用)
playMode:播放模式(播放一次、循環播放、淡入、淡出、放大、縮小、上升、下降)
waitTime:播放後等待時間(循環播放的作用為多久循環一次)
offset:偏移量,放大、縮小、上升、下降的依據
sync:同步,勾選後會跟下一段動畫同步播放
destroy:動畫播放完是否銷毀此GameObject
注意事項:
若勾選destroy時,至少要有一段動畫不能勾選sync
=======================================================================
enum AniPlayMode {PlayOnce,PlayCycle,FadeIn,FadeOut,Magnify,Minify,Up,Down};
class ANIPLAYER {
var aniTexture : Texture[];
@HideInInspector var startTime : float;
var playTime : float;
var frequency : int;
var playMode : AniPlayMode;
var waitTime : float;
var offset : float;
var sync : boolean;
}
var aniplayers : ANIPLAYER[];
var destroy : boolean;
@script RequireComponent (GUITexture)
function OnEnable () {
Play();
}
function Play () {
for(var aniplayer : ANIPLAYER in aniplayers)
{
if(aniplayer.playMode == AniPlayMode.PlayOnce)
{
if(aniplayer.sync)
PlayOnce(aniplayer);
else
yield PlayOnce(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.PlayCycle)
{
if(aniplayer.sync)
PlayCycle(aniplayer);
else
yield PlayCycle(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.FadeIn)
{
if(aniplayer.sync)
FadeIn(aniplayer);
else
yield FadeIn(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.FadeOut)
{
if(aniplayer.sync)
FadeOut(aniplayer);
else
yield FadeOut(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.Magnify)
{
if(aniplayer.sync)
Magnify(aniplayer);
else
yield Magnify(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.Minify)
{
if(aniplayer.sync)
Minify(aniplayer);
else
yield Minify(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.Up)
{
if(aniplayer.sync)
Up(aniplayer);
else
yield Up(aniplayer);
}
else if(aniplayer.playMode == AniPlayMode.Down)
{
if(aniplayer.sync)
Down(aniplayer);
else
yield Down(aniplayer);
}
}
if(destroy)
{
Destroy(gameObject);
}
}
function PlayOnce (aniplayer : ANIPLAYER) {
var index : int;
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
index = (aniplayer.startTime * (aniplayer.aniTexture.Length / aniplayer.playTime) * aniplayer.frequency) % aniplayer.aniTexture.Length;
guiTexture.texture = aniplayer.aniTexture[index];
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}
function PlayCycle (aniplayer : ANIPLAYER) {
var index : int;
aniplayer.startTime = 0.0f;
while(true)
{
while(aniplayer.startTime <= aniplayer.playTime)
{
index = (aniplayer.startTime * (aniplayer.aniTexture.Length / aniplayer.playTime) * aniplayer.frequency) % aniplayer.aniTexture.Length;
guiTexture.texture = aniplayer.aniTexture[index];
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
aniplayer.startTime = 0.0f;
}
}
function FadeIn (aniplayer : ANIPLAYER) {
aniplayer.startTime = 0.0f;
guiTexture.color.a = 0;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.color.a = guiTexture.color.a + (Time.deltaTime / aniplayer.playTime);
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}
function FadeOut (aniplayer : ANIPLAYER) {
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.color.a = guiTexture.color.a - (Time.deltaTime / aniplayer.playTime);
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}
function Magnify (aniplayer : ANIPLAYER) {
var scale : float = guiTexture.pixelInset.width / guiTexture.pixelInset.height;
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.pixelInset.width = guiTexture.pixelInset.width + Time.deltaTime * aniplayer.offset * scale;
guiTexture.pixelInset.height = guiTexture.pixelInset.height + Time.deltaTime * aniplayer.offset;
guiTexture.pixelInset.x = guiTexture.pixelInset.x - Time.deltaTime * aniplayer.offset * scale / 2;
guiTexture.pixelInset.y = guiTexture.pixelInset.y - Time.deltaTime * aniplayer.offset / 2;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}
function Minify (aniplayer : ANIPLAYER) {
var scale : float = guiTexture.pixelInset.width / guiTexture.pixelInset.height;
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.pixelInset.width = guiTexture.pixelInset.width - Time.deltaTime * aniplayer.offset * scale;
guiTexture.pixelInset.height = guiTexture.pixelInset.height - Time.deltaTime * aniplayer.offset;
guiTexture.pixelInset.x = guiTexture.pixelInset.x + Time.deltaTime * aniplayer.offset * scale / 2;
guiTexture.pixelInset.y = guiTexture.pixelInset.y + Time.deltaTime * aniplayer.offset / 2;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}
function Up (aniplayer : ANIPLAYER) {
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.pixelInset.y = guiTexture.pixelInset.y + Time.deltaTime * aniplayer.offset;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}
function Down (aniplayer : ANIPLAYER) {
aniplayer.startTime = 0.0f;
while(aniplayer.startTime <= aniplayer.playTime)
{
aniplayer.startTime = aniplayer.startTime + Time.deltaTime;
guiTexture.pixelInset.y = guiTexture.pixelInset.y - Time.deltaTime * aniplayer.offset;
yield;
}
yield WaitForSeconds(aniplayer.waitTime);
}
標籤:
UNITY 3D
2011年7月24日 星期日
2011年7月20日 星期三
UNITY3D - 圖片淡入淡出
圖片淡入淡出其實跟GameObject逐漸透明消失的做法類似
只要改圖片本身的透明度就好了
要記得一點是顏色跟透明度的值是(x/256)的float值,不要以為直接塞1~256的值就可以...
圖片淡入
=========================================================================
function Start ()
{
guiTexture.color.a = 0; //讓圖片一開始為透明
}
function Update ()
{
if(guiTexture.color.a < 1)
{
guiTexture.color.a = guiTexture.color.a + Time.deltaTime; //讓圖片隨時間變不透明
}
}
=========================================================================
畫面淡出
=========================================================================
=========================================================================
只要改圖片本身的透明度就好了
要記得一點是顏色跟透明度的值是(x/256)的float值,不要以為直接塞1~256的值就可以...
圖片淡入
=========================================================================
function Start ()
{
guiTexture.color.a = 0; //讓圖片一開始為透明
}
function Update ()
{
if(guiTexture.color.a < 1)
{
guiTexture.color.a = guiTexture.color.a + Time.deltaTime; //讓圖片隨時間變不透明
}
}
=========================================================================
畫面淡出
=========================================================================
function Start ()
{
guiTexture.color.a = 1; //讓圖片一開始為不透明
}
function Update ()
{
if(guiTexture.color.a < 1)
{
guiTexture.color.a = guiTexture.color.a - Time.deltaTime; //讓圖片隨時間變透明
}
}
=========================================================================
標籤:
UNITY 3D
2011年7月13日 星期三
UNITY3D - 連續貼圖
U3D不支援顯示gif檔案,但是我們可以利用程式來將一連串的靜態圖片顯示得跟動態圖片無異。
首先建立一個empty object然後加入下面component script。
=======================================================================
var aTexture : Texture[]; //靜態圖片組
var framePerSecond : int; //每秒播放張數
var x : int; //x軸位置(以pixel為單位)
var y : int; //y軸位置(以pixel為單位)
private var startTime : float;
function Start () {
startTime = Time.time; //記錄開始時間,確保圖從第一張開始放。
}
function OnGUI () {
var index : int = ((Time.time - startTime) * framePerSecond) % aTexture.Length;
GUI.DrawTexture(Rect(x, y, aTexture[index].width, aTexture[index].height), aTexture[index], ScaleMode.StretchToFill, true, 0);
}
=======================================================================
加入component後將靜態圖按順序設給aTexture以及設定framePerSecond與XY軸就可以看到在設好的(x,y)上顯示靜態圖片組成的動畫了。
首先建立一個empty object然後加入下面component script。
=======================================================================
var aTexture : Texture[]; //靜態圖片組
var framePerSecond : int; //每秒播放張數
var x : int; //x軸位置(以pixel為單位)
var y : int; //y軸位置(以pixel為單位)
private var startTime : float;
function Start () {
startTime = Time.time; //記錄開始時間,確保圖從第一張開始放。
}
function OnGUI () {
var index : int = ((Time.time - startTime) * framePerSecond) % aTexture.Length;
GUI.DrawTexture(Rect(x, y, aTexture[index].width, aTexture[index].height), aTexture[index], ScaleMode.StretchToFill, true, 0);
}
=======================================================================
加入component後將靜態圖按順序設給aTexture以及設定framePerSecond與XY軸就可以看到在設好的(x,y)上顯示靜態圖片組成的動畫了。
標籤:
UNITY 3D
訂閱:
文章 (Atom)