// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using System.Collections.Generic;
using UltimateXR.Avatar;
using UnityEditor;
using UnityEngine;
namespace UltimateXR.Editor.Avatar
{
///
/// extensions available only for editor scripts.
///
public static class UxrAvatarEditorExt
{
#region Public Methods
///
/// Gets an 's prefab, based on the Guid stored.
///
/// Avatar to get the source prefab of
/// Prefab or null if it could not be found
public static GameObject GetPrefab(this UxrAvatar self)
{
return AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(self.PrefabGuid));
}
///
/// Gets an prefab's component, based on the Guid stored.
///
/// Avatar to get the source prefab of
/// Prefab or null if it could not be found
public static UxrAvatar GetAvatarPrefab(this UxrAvatar self)
{
return GetFromGuid(self.PrefabGuid);
}
///
/// Gets a prefab's GUID.
///
/// Prefab to get the GUID of
/// GUID or empty string if it could not be found
public static string GetGuid(GameObject gameObject)
{
return AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(gameObject));
}
///
/// Gets an prefab, based on an asset GUID.
///
/// Asset GUID
/// Prefab or null if it could not be found
public static UxrAvatar GetFromGuid(string avatarPrefabGuid)
{
GameObject prefab = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(avatarPrefabGuid));
return prefab != null ? prefab.GetComponent() : null;
}
///
/// Gets the avatar prefab chain. This is the source prefab followed by all parent prefabs up to the root parent
/// prefab.
///
///
/// Upwards prefab chain. If the avatar is a prefab itself, it will be the first item in the list.
///
public static IEnumerable GetPrefabChain(this UxrAvatar self)
{
UxrAvatar avatarPrefab = self.GetAvatarPrefab();
if (avatarPrefab != null)
{
yield return avatarPrefab;
}
UxrAvatar current = self.ParentAvatarPrefab;
while (current != null)
{
yield return current;
current = current.ParentAvatarPrefab;
}
}
///
/// Checks whether the avatar is or comes from the given prefab.
///
/// Avatar
/// Prefab
/// Whether the avatar is or comes from the given prefab
public static bool IsInPrefabChain(this UxrAvatar self, GameObject prefab)
{
foreach (UxrAvatar avatarPrefab in self.GetPrefabChain())
{
if (avatarPrefab != null && avatarPrefab.gameObject == prefab)
{
return true;
}
}
return false;
}
#endregion
}
}