Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active April 29, 2022 12:44
Show Gist options
  • Save tsubaki/26a4d54adc40b7331322e7bea749e314 to your computer and use it in GitHub Desktop.
Save tsubaki/26a4d54adc40b7331322e7bea749e314 to your computer and use it in GitHub Desktop.
IExposedPropertyTableのサンプル
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scripting;
using UnityEngine.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class ResolverTest : MonoBehaviour, IExposedPropertyTable
{
[SerializeField] SampleData bind = null;
public List<PropertyName> propNameList = new List<PropertyName> ();
public List<UnityEngine.Object> objectList = new List<UnityEngine.Object>();
void Update()
{
bind.Do (this);
}
#region IExposedPropertyTable implementation
public void SetReferenceValue (PropertyName id, Object value)
{
if (PropertyName.IsNullOrEmpty (id))
return;
var index = -1;
index = propNameList.IndexOf (id);
if (index != -1) {
propNameList [index] = id;
objectList [index] = value;
} else if (value == null) {
propNameList.Add (id);
objectList.Add (value);
}
}
public Object GetReferenceValue (PropertyName id, out bool idValid)
{
var index = -1;
index = propNameList.IndexOf (id);
idValid = index != -1;
if (idValid) {
return objectList [index];
} else {
return null;
}
}
public void ClearReferenceValue (PropertyName id)
{
var index = -1;
index = propNameList.IndexOf (id);
if (index != -1) {
propNameList.RemoveAt (index);
objectList.RemoveAt (index);
}
}
#endregion
#if UNITY_EDITOR
[CustomEditor(typeof(ResolverTest))]
class ResolverTestEditor : Editor
{
public override void OnInspectorGUI ()
{
var restest = target as ResolverTest;
using (var change = new EditorGUI.ChangeCheckScope ()) {
// バインドする動作を取得
restest.bind = (SampleData)EditorGUILayout.ObjectField (restest.bind, typeof(SampleData), false);
if (restest.bind == null)
return;
if (change.changed) {
SetExposedNames (restest);
}
}
// バインド先の情報をキャッシュし、
// バインド先と自身のオブジェクトを紐付けしたフィールドを取得、
// 一覧表示する
var bindedAssetField = new SerializedObject (restest.bind , this.target);
var iterator = bindedAssetField.GetIterator ();
bool enterChildren = true; // 最初の子のみ表示する為
while(iterator.NextVisible(enterChildren)){
enterChildren = false;
EditorGUILayout.PropertyField (iterator, true);
}
}
void SetExposedNames(ResolverTest restest)
{
var field = new SerializedObject (restest.bind );
var iterator = field.GetIterator ();
while(iterator.NextVisible(true)){
var field = new SerializedObject (restest.bind );
var iterator = field.GetIterator ();
while(iterator.NextVisible(true)){
if (iterator.displayName == "Exposed Name") {
bool result = false;
restest.GetReferenceValue (iterator.stringValue, out result);
if (result == false) {
restest.SetReferenceValue (iterator.stringValue, null);
}
}
}
}
}
#endif
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu]
public class SampleData : ScriptableObject
{
public ExposedReference<Transform> obj;
public ExposedReference<Camera> mainCamera;
[SerializeField]
float speed = 1;
public void Do (IExposedPropertyTable resolver)
{
var target = obj.Resolve (resolver);
target.Rotate (Vector3.up * speed);
//var cam = mainCamera.Resolve (resolver);
//cam.transform.Rotate (Vector3.forward * speed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment