using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor.AddressableAssets.GUI;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.U2D;
namespace UnityEditor.AddressableAssets.Settings
{
using Object = UnityEngine.Object;
///
/// Contains editor data for the AssetReference.
///
internal static class AssetReferenceDrawerUtilities
{
internal const string noAssetString = "None (Addressable Asset)";
internal const string noAssetTypeStringformat = "None (Addressable {0})";
static internal bool ValidateAsset(AssetReference assetRefObject, List restrictions, Object obj)
{
return assetRefObject != null
&& assetRefObject.ValidateAsset(obj)
&& restrictions != null
&& restrictions.All(r => r.ValidateAsset(obj));
}
static internal bool ValidateAsset(AssetReference assetRefObject, List restrictions, IReferenceEntryData entryData)
{
return assetRefObject != null
&& assetRefObject.ValidateAsset(entryData?.AssetPath)
&& restrictions != null
&& restrictions.All(r => r.ValidateAsset(entryData));
}
static internal bool ValidateAsset(AssetReference assetRefObject, List restrictions, string path)
{
if (assetRefObject != null && assetRefObject.ValidateAsset(path))
{
foreach (var restriction in restrictions)
{
if (!restriction.ValidateAsset(path))
return false;
}
return true;
}
return false;
}
static internal bool SetObject(ref AssetReference assetRefObject, ref bool referencesSame, SerializedProperty property, Object target, FieldInfo fieldInfo, string label, out string guid)
{
guid = null;
try
{
if (assetRefObject == null)
return false;
Undo.RecordObject(property.serializedObject.targetObject, "Assign Asset Reference");
if (target == null)
{
guid = SetSingleAsset(ref assetRefObject, property, null, null);
if (property.serializedObject.targetObjects.Length > 1)
return SetMainAssets(ref referencesSame, property, null, null, fieldInfo, label);
return true;
}
Object subObject = null;
if (target.GetType() == typeof(Sprite))
{
var atlasEntries = new List();
AddressableAssetSettingsDefaultObject.Settings.GetAllAssets(atlasEntries, false, null,
e => AssetDatabase.GetMainAssetTypeAtPath(e.AssetPath) == typeof(SpriteAtlas));
var spriteName = FormatName(target.name);
foreach (var a in atlasEntries)
{
var atlas = AssetDatabase.LoadAssetAtPath(a.AssetPath);
if (atlas == null)
continue;
var s = atlas.GetSprite(spriteName);
if (s == null)
continue;
subObject = target;
target = atlas;
break;
}
}
if (subObject == null && AssetDatabase.IsSubAsset(target))
{
subObject = target;
target = AssetDatabase.LoadAssetAtPath