using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson3 : MonoBehaviour
{
public Lesson3 otherLesson3;
// Start is called before the first frame update
void Start()
{
#region 知識点一 重要メンバー
//1.アタッチされているGameObjectを取得
print(this.gameObject.name);
//2.アタッチされているGameObjectの位置情報を取得
//オブジェクトの位置情報を取得
print(this.transform.position);//位置
print(this.transform.eulerAngles);//角度
print(this.transform.lossyScale);//スケールサイズ
//この書き方は上と同じ効果で、アタッチされているオブジェクトの位置情報を取得する
//this.gameObject.transform
//3.スクリプトがアクティブかどうかを取得
this.enabled = false;
//他のスクリプトオブジェクトを取得 アタッチされているgameobjectとtransformの位置情報
print(otherLesson3.gameObject.name);
print(otherLesson3.transform.position);
#endregion
#region 知識点二 重要メソッド
//アタッチされているオブジェクトにマウントされている他のスクリプトを取得
//1.自分がマウントしている単一のスクリプトを取得
//スクリプト名で取得
//スクリプトを取得する方法 取得に失敗した場合は、対応するスクリプトがないことを意味し、デフォルトでnullを返す
Lesson3_Test t = this.GetComponent("Lesson3_Test") as Lesson3_Test;
print(t);
//Typeで取得
t = this.GetComponent(typeof(Lesson3_Test)) as Lesson3_Test;
print(t);
//ジェネリックで取得 ジェネリックを使用することをお勧めします。なぜなら、二重変換が不要だからです
t = this.GetComponent<Lesson3_Test>();
if( t != null )
{
print(t);
}
//シーン内の他のオブジェクトやオブジェクトにアタッチされているスクリプトを取得できれば
//そのすべての情報を取得できます
//2.自分がマウントしている複数のスクリプトを取得
Lesson3[] array = this.GetComponents<Lesson3>();
print(array.Length);
List<Lesson3> list = new List<Lesson3>();
this.GetComponents<Lesson3>(list);
print(list.Count);
//3.子オブジェクトにマウントされているスクリプトを取得(デフォルトでは自分にマウントされているスクリプトも探します)
//関数には1つのパラメータがあり、デフォルトではfalseを渡します。つまり、子オブジェクトが非アクティブの場合、そのオブジェクトに特定のスクリプトがあるかどうかは探しません
//trueを渡すと、たとえ非アクティブでも探します
//子オブジェクトにマウントされているスクリプトを取得 単一
t = this.GetComponentInChildren<Lesson3_Test>(true);
print(t);
//子オブジェクトにマウントされているスクリプトを取得 複数
Lesson3_Test[] lts = this.GetComponentsInChildren<Lesson3_Test>(true);
print(lts.Length);
List<Lesson3_Test> list2 = new List<Lesson3_Test>();
this.GetComponentsInChildren<Lesson3_Test>(true, list2);
print(list2.Count);
//4.親オブジェクトにマウントされているスクリプトを取得(デフォルトでは自分にマウントされているスクリプトも探します)
t = this.GetComponentInParent<Lesson3_Test>();
print(t);
lts = this.GetComponentsInParent<Lesson3_Test>();
print(lts.Length);
//リストもありますが、上と同じ方法なので省略します
//5.スクリプトを取得しようとする
Lesson3_Test l3t;
//単一のスクリプトを取得するためのより安全な方法が提供されており、取得できればtrueを返します
//その後、ロジック処理を行うことができます
if(this.TryGetComponent<Lesson3_Test>(out l3t))
{
//ロジック処理
}
#endregion
}
// Update is called once per frame
void Update()
{
}
}