using UnityEngine;
using System.Collections;
public class PlayerControls03 : MonoBehaviour {
private Vector3 pos, oldpos;
public bool bMove = false;
private bool bPause = false;
void Start ()
{
}
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
Debug.Log ("Mouse Down");
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Ray2D ray2D = new Ray2D(new Vector2(ray.origin.x, ray.origin.y), new Vector2(ray.direction.x, ray.direction.y));
RaycastHit2D[] hits = Physics2D.RaycastAll(ray2D.origin, ray2D.direction);
int i = 0;
while(i < hits.Length) {
RaycastHit2D hit = hits[i];
if(hit.collider.tag == "Pause") {
if(bPause)
Time.timeScale = 1;
else
Time.timeScale = 0;
bPause = !bPause;
} else if(hit.collider.tag == "Player") {
if(!bPause) {
bMove = true;
pos = transform.position;
}
}
Debug.Log ("Touch -> " + hit.collider.tag);
i++;
}
}
else if (Input.GetMouseButton (0))
{
if(bMove)
{
oldpos = pos;
pos = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z));
transform.position = new Vector3 (transform.position.x + (pos.x - oldpos.x),
transform.position.y + (pos.y - oldpos.y),
transform.position.z);
/*
transform.localPosition = new Vector3 (transform.localPosition.x + (pos.x - oldpos.x),
transform.localPosition.y + (pos.y - oldpos.y),
transform.localPosition.z);
*/
Debug.Log("Mouse Move");
}
}
else if(Input.GetMouseButtonUp(0))
{
Debug.Log ("Mouse Up");
bMove = false;
}
}
}
using UnityEngine;
using System.Collections;
public class MusicPlayer01 : MonoBehaviour {
public AudioClip backMusics;
// Use this for initialization
void Start () {
audio.clip = backMusics;
audio.Play ();
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
public class MusicPlayer02 : MonoBehaviour {
public AudioClip[] backMusics;
// Use this for initialization
void Start () {
ChanageBackMusic (backMusics[0]);
}
// Update is called once per frame
void Update () {
}
public void ChanageBackMusic(AudioClip aud) {
if(audio.isPlaying)
audio.Stop();
audio.clip = aud;
audio.Play ();
}
}
using UnityEngine;
using System.Collections;
public class MusicPlayer03 : MonoBehaviour {
public AudioClip[] backMusics;
// Use this for initialization
void Start () {
ChanageBackMusic (backMusics[0]);
StartCoroutine ("TestMusicChange");
}
IEnumerator TestMusicChange() {
yield return new WaitForSeconds (10.0f);
ChanageBackMusic (backMusics[1]);
}
// Update is called once per frame
void Update () {
}
public void ChanageBackMusic(AudioClip aud) {
if(audio.isPlaying)
audio.Stop();
audio.clip = aud;
audio.Play ();
}
}
using UnityEngine;
using System.Collections;
public class Destoryer : MonoBehaviour {
public GameObject destoryAnim;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void DestroyGameObject()
{
Destroy (gameObject);
if(destoryAnim)
Instantiate (destoryAnim, transform.position, Quaternion.identity);
}
}
using UnityEngine;
using System.Collections;
public class EnemiesCollision : MonoBehaviour {
public GameObject destoryAnim;
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "HeroMissile") {
Debug.Log ("HeroMissile");
Destroy (other.gameObject);
if(destoryAnim)
Instantiate (destoryAnim, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
using UnityEngine;
using System.Collections;
public class ScoreShadow : MonoBehaviour
{
public GameObject guiCopy; // A copy of the score.
void Awake()
{
// Set the position to be slightly down and behind the other gui.
Vector3 behindPos = transform.position;
behindPos = new Vector3(guiCopy.transform.position.x, guiCopy.transform.position.y-0.005f, guiCopy.transform.position.z-1);
transform.position = behindPos;
}
void Update ()
{
// Set the text to equal the copy's text.
guiText.text = guiCopy.guiText.text;
}
}
guiText와 guiTexture는 다른 GameObject들과 다르게 GUI 좌표계를 사용하므로, 상대적으로 z값이 더 큰 것이 화면의 위쪽에 위치하게 된다.
(참고 : http://moguwai.tistory.com/entry/GUITexture%EC%9D%98-%EC%A2%8C%ED%91%9C)
using UnityEngine;
using System.Collections;
public class GameScenecontroller : MonoBehaviour {
private int Score = 0;
public GUIText scoreGUI;
//public GUIText scoreGUIShadow;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Escape)) {
Debug.Log ("Escape Key");
Application.Quit ();
}
}
public void AddScore(int Value) {
Score += Value;
scoreGUI.text = "Score: " + Score;
}
public int GetScore()
{
return Score;
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameScenecontroller_01 : MonoBehaviour {
private int Score = 0;
public Text scoreGUI;
//public GUIText scoreGUIShadow;
// Use this for initialization
void Start () {
// scoreGUI = GameObject.Find ("Score").GetComponent<Text> ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Escape)) {
Debug.Log ("Escape Key");
Application.Quit ();
}
}
public void AddScore(int Value) {
Score += Value;
scoreGUI.text = "Score: " + Score;
}
public int GetScore()
{
return Score;
}
}
바뀐 UI 시스템관련 소스 코드
using UnityEngine;
using System.Collections;
public class EnemiesCollision : MonoBehaviour {
public GameObject destoryAnim;
public int Score = 20;
private GameObject UICon;
private GameScenecontroller UIscript;
void Start () {
UICon = GameObject.Find ("GameSceneController");
UIscript = Con.GetComponent<GameScenecontroller> ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "HeroMissile") {
Debug.Log ("HeroMissile");
Destroy (other.gameObject);
if(destoryAnim)
Instantiate (destoryAnim, transform.position, Quaternion.identity);
Destroy(gameObject);
UIscript.AddScore(Score);
}
}
}
모든 적캐릭터에서 사용되는 만큼 UICon 변수에 UIController를 일일이 대입하지 않고 start()함수에서 처리하도록 하고 있음