2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Unityで最初から自動アタッチされてる状態にする 例

Last updated at Posted at 2020-02-10

InspectorでReset選択時に自動アタッチ

InfoGetter.cs
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

    /// <summary>
    /// お知らせ表示
    /// </summary>
    public class InfoGetter : MonoBehaviour
    {
        [Header("表示するオブジェクト")]
        [SerializeField]
        private GameObject prefab = null;
        [Header("表示させる場所の親")]
        [SerializeField]
        private Transform InstantiateParent = null;//Instantiateで複製したオブジェクトの親

        void Start()
        {
           CommonValues.Message.Reverse();
            for (int i = 0; i < CommonValues.Message.Count; i++)
            {
                GameObject item = GameObject.Instantiate(prefab, InstantiateParent);

                var text1 = item.FindChild("InfoText").GetComponent<Text>();
                text1.text = CommonValues.Message[i];

            }
        }

#if UNITY_EDITOR
        /// <summary>
        /// InspectorからReset選択時に自動アタッチ
        /// </summary>
        private void Reset()
        {
            //指定フォルダのprefabを全取得
            var guids_prefab = AssetDatabase.FindAssets("t:prefab", new string[] { "Assets/Prefabs" });

            for (int i = 0; i < guids_prefab.Length; i++)
            {
                //アタッチさせたいオブジェクト名を拡張子付きで書く
                prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/Info.prefab");

            }
            // ヒエラルキー上のすべてのオブジェクトを取得
            Object[] allGameObject = Resources.FindObjectsOfTypeAll(typeof(GameObject));
            // 取得したオブジェクトの名前を表示
            foreach (GameObject obj in allGameObject)
            {
                Debug.Log(obj.name);
                //アタッチさせたいオブジェクト名を書く
                if (obj.name == "InfoContent")
                {
                    InstantiateParent = obj.transform;

                }
            }
        }
    
#endif
}


これで、gitであらぶってアタッチ抜けが起きても戻せる!

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?