/* * Copyright (c) Meta Platforms, Inc. and affiliates. * All rights reserved. * * Licensed under the Oculus SDK License Agreement (the "License"); * you may not use the Oculus SDK except in compliance with the License, * which is provided at the time of installation or download, or which * otherwise accompanies this software in either electronic or hard copy form. * * You may obtain a copy of the License at * * https://developer.oculus.com/licenses/oculussdk/ * * Unless required by applicable law or agreed to in writing, the Oculus SDK * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System.Collections.Generic; using UnityEngine; using UnityEditor; using System; using Object = UnityEngine.Object; namespace Oculus.Interaction.Editor { public class AssetListWindow : EditorWindow { public class AssetInfo { public readonly string AssetPath; public readonly string DisplayName; public readonly Action ClickAction; public AssetInfo(string assetPath) : this(assetPath, assetPath) { } public AssetInfo(string assetPath, string displayName, Action clickAction) : this(assetPath, displayName) { ClickAction = clickAction; } public AssetInfo(string assetPath, string displayName) { AssetPath = assetPath; DisplayName = displayName; ClickAction = () => PingObject(assetPath); } } private List _assetInfos; private Vector2 _scrollPos; private Action _headerDrawer; private Action _footerDrawer; public IReadOnlyList AssetInfos => _assetInfos; public static AssetListWindow Show( string title, IEnumerable assetPaths, bool modal = false, Action headerDrawer = null, Action footerDrawer = null) { List assetInfos = new List(); foreach (var path in assetPaths) { assetInfos.Add(new AssetInfo(path)); } return Show(title, assetInfos, modal, headerDrawer, footerDrawer); } public static AssetListWindow Show( string title, IEnumerable assetInfos, bool modal = false, Action headerDrawer = null, Action footerDrawer = null) { AssetListWindow window = GetWindow(true); window._assetInfos = new List(assetInfos); window.SetTitle(title); window.SetHeader(headerDrawer); window.SetFooter(footerDrawer); if (modal) { window.ShowModalUtility(); } else { window.ShowUtility(); } return window; } public static void CloseAll() { if (HasOpenInstances()) { AssetListWindow window = GetWindow(true); window.Close(); } } public void SetTitle(string title) { titleContent = new GUIContent(title); } public void SetHeader(Action headerDrawer) { _headerDrawer = headerDrawer; } public void SetFooter(Action footerDrawer) { _footerDrawer = footerDrawer; } private void OnGUI() { DrawHeader(); DrawContent(); DrawFooter(); } private void DrawHeader() { if (_headerDrawer == null) { return; } EditorGUILayout.BeginVertical(); _headerDrawer.Invoke(this); EditorGUILayout.EndVertical(); } private void DrawFooter() { if (_footerDrawer == null) { return; } EditorGUILayout.BeginVertical(); _footerDrawer.Invoke(this); EditorGUILayout.EndVertical(); } private void DrawContent() { EditorGUILayout.BeginVertical(); _scrollPos = EditorGUILayout.BeginScrollView(_scrollPos); foreach (var assetInfo in _assetInfos) { var rect = EditorGUILayout.BeginHorizontal(); if (GUI.Button(rect, "", GUIStyle.none)) { assetInfo.ClickAction.Invoke(); } GUIStyle style = new GUIStyle(GUI.skin.label); style.richText = true; EditorGUILayout.LabelField(assetInfo.DisplayName, style); EditorGUILayout.EndHorizontal(); } GUILayout.FlexibleSpace(); EditorGUILayout.EndScrollView(); EditorGUILayout.EndVertical(); } private static void PingObject(string assetPath) { Object obj = AssetDatabase.LoadAssetAtPath( assetPath, typeof(Object)); if (obj != null) { EditorGUIUtility.PingObject(obj); } } } }