Newer
Older
CGTrack / Assets / Editor / Bausatz / TrackPieceEditor.cs
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

namespace Bausatz
{
    
    [CustomEditor(typeof(TrackPiece))]
    public class TrackPieceEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            var piece = (TrackPiece) target;
            var parent = piece.GetComponentInParent<Track>();
            DrawDefaultInspector();
            
            EditorGUILayout.LabelField("Length", piece.length.ToString(CultureInfo.InvariantCulture));

            if (parent == null || parent.prefabs == null) return;
            foreach (var prefab in parent.prefabs)
            {
                if (GUILayout.Button("Add " + prefab.name))
                {
                    var newChild = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
                    newChild.transform.SetPositionAndRotation(piece.end.position, piece.end.rotation);
                    newChild.transform.SetParent(parent.transform);
                    newChild.transform.SetSiblingIndex(piece.transform.GetSiblingIndex() + 1);

                    Selection.objects = new Object[] {newChild};
                }
            }
        }
    }
}