Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active December 31, 2017 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/99859e4548db24cb6f1057cbf3f56cce to your computer and use it in GitHub Desktop.
Save tsubaki/99859e4548db24cb6f1057cbf3f56cce to your computer and use it in GitHub Desktop.
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)){
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 SampleAction1 : SampleData {
public ExposedReference<Transform> item;
new void Reset()
{
base.Reset ();
item.exposedName = "item";
}
public override void Do (IExposedPropertyTable resolver)
{
var target = obj.Resolve (resolver);
target.Rotate (Vector3.up * speed);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu]
public class SampleAction2 : SampleData {
public override void Do (IExposedPropertyTable resolver)
{
var target = obj.Resolve (resolver);
target.Rotate (Vector3.right * speed);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class SampleData : ScriptableObject
{
// 全SampleDataで使用する予定の設定項目
[SerializeField]
protected float speed = 1;
public ExposedReference<Transform> obj;
// オブジェクト生成時にexposeを登録しておく
public void Reset()
{
obj.exposedName = "obj";
}
public abstract void Do (IExposedPropertyTable resolver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment