banner
paofan

paofan

欢迎来到我的个人主页!我是泡饭~喵~🐱
steam_profiles
bilibili
github

unityの親子オブジェクトスクリプト

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()
    {
        
    }
}
読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。