Last active
October 21, 2024 09:08
-
-
Save hobione2k/dbc3eca03b847a6a9de6e1a5315fcc84 to your computer and use it in GitHub Desktop.
[VRC] シーン内のアバターをボタンで切り替えるUnityEditor拡張(ボタンを押したアバターだけを表示)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Collections.Generic; | |
using System.Linq; | |
using VRC.SDKBase; | |
using UnityEngine.SceneManagement; | |
#if UNITY_EDITOR | |
public class ToggleAvatar : EditorWindow | |
{ | |
[SerializeField] GameObject showingAvatar; | |
private Vector2 _scrollPosition = Vector2.zero; | |
[MenuItem("Tools/ToggleAvatar")] | |
public static void ShowWindow() | |
{ | |
GetWindow<ToggleAvatar>("Toggle Avatar"); | |
} | |
void OnGUI() | |
{ | |
_scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition); | |
var avatars = FindRootObjectsByType<VRC_AvatarDescriptor>(); | |
var toggleObjects = avatars.Select(a => a.gameObject).ToArray(); | |
if (toggleObjects.Length <= 0) | |
EditorGUILayout.LabelField("No avatars on this scene."); | |
else | |
{ | |
if (showingAvatar == null && Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<VRC_AvatarDescriptor>() != null) | |
showingAvatar = Selection.activeGameObject; // already selected avatar | |
GUIStyle selectedStyle = new GUIStyle(GUI.skin.button); | |
selectedStyle.fontStyle = FontStyle.Bold; | |
selectedStyle.alignment = TextAnchor.MiddleLeft; | |
selectedStyle.padding = new RectOffset(15, 15, 3, 3); | |
GUIStyle noneStyle = new GUIStyle(GUI.skin.button); | |
noneStyle.alignment = TextAnchor.MiddleLeft; | |
noneStyle.padding = new RectOffset(10, 10, 3, 3); | |
foreach (var obj in toggleObjects) | |
{ | |
if (GUILayout.Button(obj.name, style: showingAvatar == obj ? selectedStyle : noneStyle)) { | |
ShowOnly(obj, toggleObjects); | |
} | |
} | |
} | |
EditorGUILayout.EndScrollView(); | |
} | |
void ShowOnly(GameObject selected, IEnumerable<GameObject> objects) | |
{ | |
showingAvatar = selected; | |
foreach (var obj in objects) | |
obj.SetActive(obj == selected); | |
} | |
IEnumerable<GameObject> FindRootObjectsByType<T>() where T: Component{ | |
var currentScene = SceneManager.GetActiveScene(); | |
var rootObjects = currentScene.GetRootGameObjects(); | |
foreach(var obj in rootObjects) { | |
T component = obj.GetComponent<T>(); | |
if(component==null) continue; | |
yield return obj; | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment