2020-11-09 21:38:25 +11:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using UnityEngine;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
using UnityEngine.UI;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
using UnityExplorer.Helpers;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
using UnityExplorer.UI;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
using UnityExplorer.UI.Shared;
|
|
|
|
|
|
|
|
|
|
namespace UnityExplorer.Inspectors.Reflection
|
|
|
|
|
{
|
|
|
|
|
public class InteractiveValue
|
|
|
|
|
{
|
|
|
|
|
public CacheObjectBase OwnerCacheObject;
|
|
|
|
|
|
|
|
|
|
public object Value { get; set; }
|
|
|
|
|
public Type ValueType;
|
|
|
|
|
|
|
|
|
|
public string ButtonLabel => m_btnLabel ?? GetButtonLabel();
|
|
|
|
|
private string m_btnLabel;
|
|
|
|
|
|
|
|
|
|
public MethodInfo ToStringMethod => m_toStringMethod ?? GetToStringMethod();
|
|
|
|
|
private MethodInfo m_toStringMethod;
|
|
|
|
|
|
|
|
|
|
public virtual void Init()
|
|
|
|
|
{
|
|
|
|
|
UpdateValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void UpdateValue()
|
|
|
|
|
{
|
2020-11-11 20:16:43 +11:00
|
|
|
|
if (!m_text)
|
|
|
|
|
return;
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
if (OwnerCacheObject is CacheMember ownerMember && !string.IsNullOrEmpty(ownerMember.ReflectionException))
|
|
|
|
|
{
|
|
|
|
|
m_text.text = "<color=red>" + ownerMember.ReflectionException + "</color>";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
if (Value == null)
|
|
|
|
|
{
|
|
|
|
|
m_text.text = "<color=red>null</color>";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GetButtonLabel();
|
|
|
|
|
m_text.text = ButtonLabel;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
|
|
|
|
private MethodInfo GetToStringMethod()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
m_toStringMethod = ReflectionHelpers.GetActualType(Value).GetMethod("ToString", new Type[0])
|
|
|
|
|
?? typeof(object).GetMethod("ToString", new Type[0]);
|
|
|
|
|
|
|
|
|
|
// test invoke
|
|
|
|
|
m_toStringMethod.Invoke(Value, null);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
m_toStringMethod = typeof(object).GetMethod("ToString", new Type[0]);
|
|
|
|
|
}
|
|
|
|
|
return m_toStringMethod;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetButtonLabel()
|
|
|
|
|
{
|
2020-11-11 20:16:43 +11:00
|
|
|
|
if (Value == null) return "";
|
2020-11-09 21:38:25 +11:00
|
|
|
|
|
|
|
|
|
var valueType = ReflectionHelpers.GetActualType(Value);
|
|
|
|
|
|
|
|
|
|
string label;
|
|
|
|
|
|
|
|
|
|
if (valueType == typeof(TextAsset))
|
|
|
|
|
{
|
|
|
|
|
var textAsset = Value as TextAsset;
|
|
|
|
|
|
|
|
|
|
label = textAsset.text;
|
|
|
|
|
|
|
|
|
|
if (label.Length > 10)
|
|
|
|
|
{
|
|
|
|
|
label = $"{label.Substring(0, 10)}...";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
label = $"\"{label}\" {textAsset.name} (<color={SyntaxColors.Class_Instance}>UnityEngine.TextAsset</color>)";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
label = (string)ToStringMethod?.Invoke(Value, null) ?? Value.ToString();
|
|
|
|
|
|
|
|
|
|
var classColor = valueType.IsAbstract && valueType.IsSealed
|
|
|
|
|
? SyntaxColors.Class_Static
|
|
|
|
|
: SyntaxColors.Class_Instance;
|
|
|
|
|
|
|
|
|
|
string typeLabel = $"<color={classColor}>{valueType.FullName}</color>";
|
|
|
|
|
|
|
|
|
|
if (Value is UnityEngine.Object)
|
|
|
|
|
{
|
|
|
|
|
label = label.Replace($"({valueType.FullName})", $"({typeLabel})");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!label.Contains(valueType.FullName))
|
|
|
|
|
{
|
|
|
|
|
label += $" ({typeLabel})";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
label = label.Replace(valueType.FullName, typeLabel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_btnLabel = label;
|
|
|
|
|
}
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
|
|
|
|
#region UI CONSTRUCTION
|
|
|
|
|
|
|
|
|
|
internal GameObject m_UIContent;
|
|
|
|
|
internal Text m_text;
|
|
|
|
|
|
|
|
|
|
public void ConstructUI(GameObject parent)
|
|
|
|
|
{
|
|
|
|
|
// TEMPORARY
|
|
|
|
|
m_UIContent = UIFactory.CreateLabel(parent, TextAnchor.MiddleLeft);
|
|
|
|
|
var mainLayout = m_UIContent.AddComponent<LayoutElement>();
|
|
|
|
|
mainLayout.minWidth = 100;
|
|
|
|
|
mainLayout.flexibleWidth = 5000;
|
|
|
|
|
mainLayout.minHeight = 25;
|
|
|
|
|
m_text = m_UIContent.GetComponent<Text>();
|
|
|
|
|
|
|
|
|
|
if (OwnerCacheObject != null)
|
|
|
|
|
{
|
|
|
|
|
if (!OwnerCacheObject.HasEvaluated)
|
|
|
|
|
{
|
|
|
|
|
m_text.text = "Not yet evaluated";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_text.text = ButtonLabel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2020-11-09 21:38:25 +11:00
|
|
|
|
}
|
|
|
|
|
}
|