1
This commit is contained in:
50
Unity/Scripts/Reference/AssetableReference.cs
Normal file
50
Unity/Scripts/Reference/AssetableReference.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
[System.Serializable]
|
||||
public record UnityReference : IReference
|
||||
{
|
||||
[SerializeField] internal int index;
|
||||
private AssetableReference asset;
|
||||
public string Get()
|
||||
{
|
||||
asset ??= Addressables.LoadAssetAsync<AssetableReference>(nameof(AssetableReference)).WaitForCompletion();
|
||||
return asset.Get(index);
|
||||
}
|
||||
internal void Set(int _index) => index = _index;
|
||||
}
|
||||
public class AssetableReference:ScriptableObject
|
||||
{
|
||||
[SerializeField]
|
||||
private List<string> values;
|
||||
internal string Get(int index) => values[index];
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[CustomPropertyDrawer(typeof(UnityReference))]
|
||||
public class UnityReferenceInspector :PropertyDrawer
|
||||
{
|
||||
public override VisualElement CreatePropertyGUI(SerializedProperty property)
|
||||
{
|
||||
var index = property.FindPropertyRelative(nameof(UnityReference.index)).intValue;
|
||||
return new Label(index.ToString());
|
||||
}
|
||||
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
var index = property.FindPropertyRelative(nameof(UnityReference.index)).intValue;
|
||||
//var unityReference = property.serializedObject.targetObject.GetType().GetField(property.name).GetValue(property.serializedObject) as UnityReference;
|
||||
EditorGUI.DrawRect(position,Color.gray);
|
||||
//EditorGUI.LabelField(position,new GUIContent(unityReference.index.T
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
29
Unity/Scripts/Reference/DataReference.cs
Normal file
29
Unity/Scripts/Reference/DataReference.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
[System.Serializable]
|
||||
public record DataReference : References
|
||||
{
|
||||
[SubclassSelector, SerializeReference] public References value;
|
||||
public override string Get()
|
||||
{
|
||||
return Data.Get<string>(value) ?? value;
|
||||
}
|
||||
}
|
||||
[System.Serializable]
|
||||
public record DataReference<T> : References<T>
|
||||
{
|
||||
public DataReference(string key)
|
||||
{
|
||||
this.key = key;
|
||||
}
|
||||
public readonly string key;
|
||||
public override T Get()
|
||||
{
|
||||
return Data.Get<T>(key);
|
||||
}
|
||||
}
|
||||
}
|
44
Unity/Scripts/Reference/ReferenceSO.cs
Normal file
44
Unity/Scripts/Reference/ReferenceSO.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
#endif
|
||||
namespace BITKit
|
||||
{
|
||||
[System.Serializable]
|
||||
public class ReferenceSO : ScriptableObject
|
||||
{
|
||||
[TextArea]
|
||||
public string value;
|
||||
}
|
||||
[System.Serializable]
|
||||
public record ReferenceScriptableObject : References
|
||||
{
|
||||
public ReferenceSO so;
|
||||
public override string Get() => so.value.IsNullOrEmpty() ? so.name : so.value;
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[CustomPropertyDrawer(typeof(ReferenceScriptableObject))]
|
||||
public class ReferenceScriptableObjectDrawer : BITProperty
|
||||
{
|
||||
public override VisualElement CreatePropertyGUI(SerializedProperty property)
|
||||
{
|
||||
VisualElement root = new();
|
||||
root.Add(base.CreatePropertyGUI(property));
|
||||
|
||||
Button button = new();
|
||||
root.Add(button);
|
||||
return root;
|
||||
}
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
base.OnGUI(position, property, label);
|
||||
EditorGUI.PropertyField(position, property);
|
||||
EditorGUI.LinkButton(position, "New Button");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
12
Unity/Scripts/Reference/TestAssetableReference.cs
Normal file
12
Unity/Scripts/Reference/TestAssetableReference.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public class TestAssetableReference : MonoBehaviour
|
||||
{
|
||||
[SerializeReference, SubclassSelector] public IReference reference;
|
||||
public UnityReference UnityReference;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user