1
3

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 5 years have passed since last update.

(メモ)Prefabを任意の時間、位置、角度で出現させるReorderableList

Last updated at Posted at 2017-05-15

Prefabを任意の時間で出現させたい時があったので作りました。

PrefabListEmitter.cs
using System;
using System.Collections.Generic;
using UnityEngine;
# if UNITY_EDITOR
using UnityEditor;
using UnityEditorInternal;
# endif
public class PrefabListEmitter : MonoBehaviour
{
    public List<EmitPrefab> list = new List<EmitPrefab>();

    bool activate;
    float Timer;
//開始時に呼ぶ
    public void Set()
    {
        activate = true;
    }
//出現させたい間呼ぶ
    public void Emit()
    {
        if (activate)
        {
            Timer += Time.deltaTime;
            for(int i = 0;i < list.Count; i++)
            {
                if (list[i].Time < Timer)
                {
                    GameObject obj = (GameObject)Instantiate(list[i].Prefab, list[i].Pos, Quaternion.Euler(list[i].Rot));
                    obj.transform.parent = transform;
                    list.Remove(list[i]);
                }
            }
        }
    }
    [Serializable]
    public class EmitPrefab
    {
        public float Time;
        public UnityEngine.Object Prefab;
        public Vector3 Pos;
        public Vector3 Rot;
    }
}
//ReorderableListの作成
public class EmitPrefabAttribute : PropertyAttribute { }
# if UNITY_EDITOR
[CanEditMultipleObjects]
[CustomEditor(typeof(PrefabListEmitter))]
public class TutorialMessagesDrawer : Editor
{
    private ReorderableList RL;
    private SerializedProperty ListProp;
    private void OnEnable()
    {
        ListProp = serializedObject.FindProperty("list");
        RL = new ReorderableList(serializedObject, ListProp);
        RL.elementHeight = 80;
        RL.drawHeaderCallback = (rect) =>
        {
            EditorGUI.LabelField(rect, "Prefabs");
        };
        RL.drawElementCallback = (rect, index, isActive, isFocused) =>
        {
            var element = ListProp.GetArrayElementAtIndex(index);
            EditorGUI.PropertyField(rect, element);
        };
    }
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        serializedObject.Update();
        RL.DoLayoutList();
        serializedObject.ApplyModifiedProperties();
    }
}
[CustomPropertyDrawer(typeof(PrefabListEmitter.EmitPrefab))]
public class TutorialMessageAttribute : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        using (new EditorGUI.PropertyScope(position, label, property))
        {
            position.height = EditorGUIUtility.singleLineHeight;
            var ele1 = property.FindPropertyRelative("Time");
            var ele2 = property.FindPropertyRelative("Prefab");
            var ele3 = property.FindPropertyRelative("Pos");
            var ele4 = property.FindPropertyRelative("Rot");
            var ele1Rect = new Rect(position)
            {
                height = position.height
            };
            var ele2Rect = new Rect(position)
            {
                height = position.height,
                y = ele1Rect.y + EditorGUIUtility.singleLineHeight + 2
            };
            var ele3Rect = new Rect(position)
            {
                height = position.height * 2,
                y = ele2Rect.y + EditorGUIUtility.singleLineHeight + 2
            };
            var ele4Rect = new Rect(position)
            {
                height = position.height * 2,
                y = ele3Rect.y + EditorGUIUtility.singleLineHeight + 2
            };
            EditorGUI.PropertyField(ele1Rect, ele1);
            EditorGUI.PropertyField(ele2Rect, ele2);
            EditorGUI.PropertyField(ele3Rect, ele3);
            EditorGUI.PropertyField(ele4Rect, ele4);
        }
    }
}
# endif

出現させたPrefabはこのクラスをアタッチしたオブジェクトの子になります。出現させたオブジェクトが全部消えたかどうかの判定などに使ってください。

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?