(하이라키창에 있는)게임오브젝트의 gameobject, transform 접근하는 방법,
gamobject,transform의 객체를 가져오는 방법
GameObjectTest.Cs
using Sample.ObjectTest;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace Sample
{
public class GameObjectTest : MonoBehaviour
{
//필드
//2)
public Transform publicTransform;
public GameObject publicObject;
//3)
private GameObject[] tagObjects;
private GameObject tagObject;
//4)
public GameObject prefabObject;
//5)
public Transform parentObject;
private Transform[] childObjects;
// Start is called before the first frame update
void Start()
{
//1)
//this.gameObject
//this.transform
//2)
//publicTransform
//publicObject
//3)
//tagObjects = GameObject.FindGameObjectsWithTag("tagString");
//tagObject = GameObject.FindGameObjectWithTag("tagString");
//4)
//GameObject prefabGo = Instantiate(prefabObject, this.transform.position, Quaternion.identity);
//5)
//childObjects = new Transform[parentObject.childCount];
//for (int i = 0; i < childObjects.Length; i++)
//{
// childObjects[i] = parentObject.GetChild(i);
//}
//6)
//StaticClass.number = 10;
//Singleton
var objectA = Singleton.Instance;
var objectB = Singleton.Instance;
if(objectA == objectB)
{
Debug.Log(objectA);
}
//SingletonTest
SingletonTest.Instance.number = 10;
}
}
}
/*
(하이라키창에 있는)게임오브젝트의 gameobject, transform 접근하는 방법,
gamobject,transform의 객체를 가져오는 방법
1) gameobject에 스크립트 소스를 컴포넌트로 추가하여 직접(this.)로 가져온다
2) 가져오려는 오브젝트의 gameobject, transform 객체(인스턴스)를 public 한 필드로 선언한 후
유니티 에디터 툴에서 인스펙터 창으로 직접 드래그 해서 가져온다
3) Find - 유니티에서 제공하는 API를 이용해서 gameobject, transform의 객체를 반환받아 가져온다
GameObject.FindGameObjectsWithTag , GameObject.FindGameObjectWithTag
4) prefab 게임오브젝트 생성시 Instantiate 함수의 반환값으로 gameobject의 객체를 가져온다
5) 부모 자식관계를 이용해서 gameobject의 객체를 가져온다
6) static 필드: 클래스이름.필드이름,
*/
StaticClass.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Sample
{
public class StaticClass : MonoBehaviour
{
//필드
public static int number = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
}
Singleton.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sample.ObjectTest;
namespace Sample
{
namespace ObjectTest
{
public class Singleton
{
private static Singleton instance;
//속성 - 읽기전용
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
//메서드
/*public static Singleton Instance()
{
if(instance == null)
{
instance = new Singleton();
}
return instance;
}*/
}
}
}
SingletonTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Sample
{
public class SingletonTest : MonoBehaviour
{
//클래스의 인스턴스 변수를 static으로 선언
private static SingletonTest instance;
public static SingletonTest Instance
{
get
{
return instance;
}
}
//실행과 동시에 instance에 객체를 받아온다
private void Awake()
{
if (instance != null)
{
Destroy(gameObject);
return;
}
instance = this;
//gameObject는 신 종료시 파괴가 안된다
DontDestroyOnLoad(gameObject);
}
public int number = 0;
}
}
'수업일지 > 유니티_타워디펜스' 카테고리의 다른 글
24-09-09 30일차 UI (0) | 2024.09.10 |
---|---|
24-09-06 29일차 카메라제어 ,이동제어 (0) | 2024.09.06 |
24-09-04 27일차 객체접근,충돌 (1) | 2024.09.05 |
24-09-03 26일차 Rotation- 회전 (0) | 2024.09.04 |
24-09-02 25일차 타이머, 코루틴, UI-Text (0) | 2024.09.02 |