コード例 #1
0
ファイル: SoundDrawer.cs プロジェクト: hagish/tektix
    public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
    {
        SerializedProperty name = prop.FindPropertyRelative("Name");
        SerializedProperty clip = prop.FindPropertyRelative("Clip");
        SerializedProperty volume = prop.FindPropertyRelative("Volume");

        if (name == null || clip == null || volume == null) return;

        Grid g = new Grid(pos, 2, 4, 5);

        EditorGUI.PropertyField(g.GetCellsArea(0, 0, 2, 1), name, GUIContent.none);

        EditorGUI.PropertyField(g.GetCellsArea(0, 1, 1, 1), clip, GUIContent.none);

        EditorGUI.Slider(g.GetCellsArea(1, 1, 1, 1), volume, 0f, 1f, GUIContent.none);

        var audioClip = clip.objectReferenceValue as AudioClip;

        if (GUI.Button(g.GetCellsArea(0, 2, 2, 1), "Play"))
        {

            if (player != null) {
                GameObject.DestroyImmediate(player);
                player = null;
            }

            if (player == null)
            {
                player = new GameObject();
                player.hideFlags = HideFlags.HideAndDontSave;
                var s = player.AddComponent<AudioSource>();
                s.clip = audioClip;
                s.volume = volume.floatValue;
                s.Play();
            }
        }

        // update volume on running clip
        if (player != null && player.audio != null && player.audio.clip == audioClip)
        {
            player.audio.volume = volume.floatValue;
        }
    }