스크립트 부록
본 튜토리얼에 사용된 스크립트들이 모인 곳입니다.
StartMenuGUI script
아래는 StartMenuGUI 스크립트의 최종 코드입니다.
============================================
//@script ExecuteInEditMode()
var gSkin : GUISkin;
var backdrop : Texture2D;
private var isLoading = false;
function OnGUI()
{
if(gSkin)
GUI.skin = gSkin;
else
Debug.Log("StartMenuGUI : GUI Skin object missing!");
var backgroundStyle : GUIStyle = new GUIStyle();
backgroundStyle.normal.background = backdrop;
GUI.Label ( Rect( ( Screen.width - (Screen.height * 2)) * 0.75, 0, Screen.height * 2, Screen.height), "", backgroundStyle);
GUI.Label ( Rect( (Screen.width/2)-197, 50, 400, 100), "Lerpz Escapes", "mainMenuTitle");
if (GUI.Button( Rect( (Screen.width/2)-70, Screen.height -160, 140, 70), "Play"))
{
isLoading = true;
Application.LoadLevel("TheGame");
}
var isWebPlayer = (Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer);
if (!isWebPlayer)
{
if (GUI.Button( Rect( (Screen.width/2)-70, Screen.height - 80, 140, 70), "Quit")) Application.Quit();
}
if (isLoading)
{
GUI.Label ( Rect( (Screen.width/2)-110, (Screen.height / 2) - 60, 400, 70), "Loading...", "mainMenuTitle");
}
}
==============================================================================
GameOverGUI
아래는 GameOverGUI 스크립트의 최종 코드입니다.
==============================================================================
@script ExecuteInEditMode()
var background : GUIStyle;
var gameOverText : GUIStyle;
var gameOverShadow : GUIStyle;
var gameOverScale = 1.5;
var gameOverShadowScale = 1.5;
function OnGUI()
{
GUI.Label ( Rect( (Screen.width - (Screen.height * 2)) * 0.75, 0, Screen.height * 2, Screen.height), "", background);
GUI.matrix = Matrix4x4.TRS(Vector3(0, 0, 0), Quaternion.identity, Vector3.one * gameOverShadowScale);
GUI.Label ( Rect( (Screen.width / (2 * gameOverShadowScale)) - 150, (Screen.height / (2 * gameOverShadowScale)) - 40, 300, 100), "Game Over", gameOverShadow);
GUI.matrix = Matrix4x4.TRS(Vector3(0, 0, 0), Quaternion.identity, Vector3.one * gameOverScale);
GUI.Label ( Rect( (Screen.width / (2 * gameOverScale)) - 150, (Screen.height / (2 * gameOverScale)) - 40, 300, 100), "Game Over", gameOverText);
}
==============================================================================
GameOverscript
아래는 GameOverscript 스크립트의 최종 코드입니다.
==============================================================================
function LateUpdate ()
{
if (!audio.isPlaying || Input.anyKeyDown)
Application.LoadLevel("StartMenu");
}
==============================================================================
ThirdPersonStatus
아래는 ThirdPersonStatus 스크립트의 최종 코드입니다.
==============================================================================
// ThirdPersonStatus: 플레이어의 상태 머신을 관리합니다.
// 인벤토리, 체력, 목숨등을 관리합니다.
var health : int = 6;
var maxHealth : int = 6;
var lives = 4;
// 음향효과.
var struckSound: AudioClip;
var deathSound: AudioClip;
private var levelStateMachine : LevelStatus;// 레벨 완료 시퀀스를 담당하는 스크립트로의 링크.
private var remainingItems : int; // 레벨 내에 존재하는 아이템의 수. LevelStatus로 부터 가져옴.
function Awake()
{
levelStateMachine = FindObjectOfType(LevelStatus);
if (!levelStateMachine)
Debug.Log("No link to Level Status");
remainingItems = levelStateMachine.itemsNeeded;
}
// HUD스크립트에 의해 사용된 유틸리티 함수
function GetRemainingItems() : int
{
return remainingItems;
}
function ApplyDamage (damage : int)
{
if (struckSound)
AudioSource.PlayClipAtPoint(struckSound, transform.position); // 플레이어 피격에 해당하는 음향효과를 재생합니다.
health -= damage;
if (health <= 0)
{
SendMessage("Die");
}
}
function AddLife (powerUp : int)
{
lives += powerUp;
health = maxHealth;
}
function AddHealth (powerUp : int)
{
health += powerUp;
if (health>maxHealth) // 우리는 오직 6개의 조각만 HUD에 표시 할수 있습니다.
{
health=maxHealth;
}
}
function FoundItem (numFound: int)
{
remainingItems-= numFound;
if (remainingItems == 0)
{
levelStateMachine.UnlockLevelExit(); // 플레이어가 레벨에서 나올 수 있도록 합니다.
}
}
function FalloutDeath ()
{
Die();
return;
}
function Die ()
{
// 만약 가능하다면 사망 음향효과를 재생합니다.
if (deathSound)
{
AudioSource.PlayClipAtPoint(deathSound, transform.position);
}
lives--;
health = maxHealth;
if(lives < 0)
Application.LoadLevel("GameOver");
// 만약 여기 까지 왔는데 플레이어의 목숨이 남아있다면, 리스폰합니다.
respawnPosition = Respawn.currentRespawn.transform.position;
Camera.main.transform.position = respawnPosition - (transform.forward * 4) + Vector3.up; // 카메라도 리셋 시킵니다.
// 플레이어를 바로 숨기고 사망 음향 효과가 끝날 시간을 줍니다.
SendMessage("HidePlayer");
// 플레이어를 재위치 시킵니다.
//이를 하지 않으면 카메라는 FalloutDeath 박스 컬라이더위에 서있는 투명한 플레이어에 계속 포커스를 둘 것입니다.
transform.position = respawnPosition + Vector3.up;
yield WaitForSeconds(1.6); // 소리가 완료될 시간을 줍니다.
// (참고: "HidePlayer" 는 플레이어의 조종을 비활성화 시켜 주기도 합니다.)
SendMessage("ShowPlayer"); // 플레이어를 다시 보여주고...
// 파티클 효과를 보여주는 리스폰 지점을 준비합니다.
Respawn.currentRespawn.FireEffect ();
}
function LevelCompleted()
{
levelStateMachine.LevelCompleted();
}
==============================================================================
LevelStatus
아래는 LevelnStatus 스크립트의 최종 코드입니다.
=============================================================================
// LevelStatus: 마스터 스테이트 머신 스크립트
var exitGateway: GameObject;
var levelGoal: GameObject;
var unlockedSound: AudioClip;
var levelCompleteSound: AudioClip;
var mainCamera: GameObject;
var unlockedCamera: GameObject;
var levelCompletedCamera: GameObject;
// 이부분은 레벨을 완료하기 위한 정보- 플레이어가 모아야하는 아이템의 수같은 -가 있는 곳입니다.
var itemsNeeded: int = 20; // 모아야 하는 연료통의 수.
private var playerLink: GameObject;
// Awake(): 스크립트가 불러와질때 유니티에 의해 호출되어집니다.
// 우리는 이 함수를 럽츠 게임 객체로의 링크를 초기화하기위해 사용합니다.
function Awake()
{
levelGoal.GetComponent(MeshCollider).isTrigger = false;
playerLink = GameObject.Find("Player");
if (!playerLink)
Debug.Log("Could not get link to Lerpz");
levelGoal.GetComponent(MeshCollider).isTrigger = false; // 이부분은 확실히!
}
function UnlockLevelExit()
{
mainCamera.GetComponent(AudioListener).enabled = false;
unlockedCamera.active = true;
unlockedCamera.GetComponent(AudioListener).enabled = true;
exitGateway.GetComponent(AudioSource).Stop();
if (unlockedSound)
{
AudioSource.PlayClipAtPoint(unlockedSound, unlockedCamera.GetComponent(Transform).position, 2.0);
}
yield WaitForSeconds(1);
exitGateway.active = false; // ...방벽이 바로 해제되었다가...
yield WaitForSeconds(0.2); //...1/4초 동안 잠시 멈추었다가...
exitGateway.active = true; //...이제 방벽이 다시 반짝이고....
yield WaitForSeconds(0.2); //...또 한번 짧은 멈춤이 있다가...
exitGateway.active = false; //...방벽은 영원히 중단됩니다!
levelGoal.GetComponent(MeshCollider).isTrigger = true;
yield WaitForSeconds(4); //플레이어가 결과를 볼 수 있는 시간을 줍니다.
// 카메라를 교대합니다.
unlockedCamera.active = false; // NearCamera가 스크린을 독점할수 있도록 합니다.
unlockedCamera.GetComponent(AudioListener).enabled = false;
mainCamera.GetComponent(AudioListener).enabled = true;
}
function LevelCompleted()
{
mainCamera.GetComponent(AudioListener).enabled = false;
levelCompletedCamera.active = true;
levelCompletedCamera.GetComponent(AudioListener).enabled = true;
playerLink.GetComponent(ThirdPersonController).SendMessage("HidePlayer");
playerLink.transform.position+=Vector3.up*500.0; //플레이어를 500유닛 정도 움직입니다.
if (levelCompleteSound)
{
AudioSource.PlayClipAtPoint(levelCompleteSound, levelGoal.transform.position, 2.0);
}
levelGoal.animation.Play();
yield WaitForSeconds (levelGoal.animation.clip.length);
Application.LoadLevel("GameOver"); //게임오버 시퀀스를 보여줍니다
}
=============================================================================
HandleSpaceshipCollision
아래는 HandleSpaceshipCollision 스크립트의 최종 코드입니다.
=============================================================================
function OnTriggerEnter (col : Collider)
{
playerLink=col.GetComponent(ThirdPersonStatus);
if (!playerLink) // 플레이어가 아닌 경우.
{
return;
}
else
{
playerLink.LevelCompleted();
}
}
=============================================================================
본 튜토리얼 번역서는 (주)지피엠스튜디오가 운영하는 "유니티코리아" 회원님들을 위한 메뉴얼 번역 자료 입니다.
본 자료를 다른 곳에 개제 하실 때에는 아래의 번역과 출처를 명확히 밝혀 주시기 바랍니다.
본 자료는 제3자가 상업적인 용도로 사용 할 수 없음을 밝힙니다.
번역 : 유니티코리아 [U3K]게임인생
출처 : 유니티코리아(www.unity3dkorea.com
'Game Engine > Unity' 카테고리의 다른 글
Unity3 신기능 (0) | 2010.08.09 |
---|---|
Unity tutorial 모음 (0) | 2010.08.09 |
No.11 Unity 3D Platform Tutorial "Optimizing" [완결] (0) | 2010.08.09 |
No.10 Unity 3D Platform Tutorial "Audio & Finishing Touches #2" (0) | 2010.08.09 |
No.9 Unity 3D Platform Tutorial "Audio & Finishing Touches #1" (0) | 2010.08.09 |