feat: force blackboard select no scene object

main
taoria 3 years ago
parent 730d46537a
commit 0fd19497b0
  1. 8
      Resources.meta
  2. 162
      Resources/Newtonsoft.Json-for-Unity.Converters.asset
  3. 8
      Resources/Newtonsoft.Json-for-Unity.Converters.asset.meta
  4. 10
      Sample/TestExposedReference.cs
  5. 3
      Sample/TestExposedReference.cs.meta
  6. 2
      TNode/DataWrapper.cs
  7. 2
      TNode/Editor/Serialization/NodeDataWrapper.cs
  8. 167
      TNode/Models/SceneObjectWrapper.cs
  9. 9
      TNode/Runtime/SceneSerializedData.cs
  10. 3
      TNode/Runtime/SceneSerializedData.cs.meta
  11. 41
      TNode/RuntimeCache/RuntimeCache.cs
  12. 5
      TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs
  13. 21
      TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardPropertyField.cs
  14. 3
      TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardPropertyField.cs.meta
  15. 8
      TextMesh Pro.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 630fb8569cca3804a80f2fb55f7890f2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,162 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ce56e4dbb13e1644aa983b6dd170e4a7, type: 3}
m_Name: Newtonsoft.Json-for-Unity.Converters
m_EditorClassIdentifier:
useUnityContractResolver: 1
useAllOutsideConverters: 0
outsideConverters:
- enabled: 0
converterName: TNode.JsonSerialize.NodeDataConverter
settings: []
- enabled: 0
converterName: TNode.JsonSerialize.UnityObjectConverter
settings: []
- enabled: 0
converterName: TNode.JsonSerialize.Vector3Converter
settings: []
useAllUnityConverters: 1
unityConverters:
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.AI.NavMesh.NavMeshQueryFilterConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.AI.NavMesh.NavMeshTriangulationConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Camera.CullingGroupEventConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Geometry.BoundsConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Geometry.BoundsIntConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Geometry.PlaneConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Geometry.RectConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Geometry.RectIntConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Geometry.RectOffsetConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Hashing.Hash128Converter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Math.Color32Converter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Math.ColorConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Math.Matrix4x4Converter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Math.QuaternionConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Math.SphericalHarmonicsL2Converter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Math.Vector2Converter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Math.Vector2IntConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Math.Vector3Converter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Math.Vector3IntConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Math.Vector4Converter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.NativeArray.NativeArrayConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Physics.JointDriveConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Physics.JointLimitsConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Physics.SoftJointLimitConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Physics2D.ColliderDistance2DConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Physics2D.ContactFilter2DConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Random.RandomStateConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Scripting.LayerMaskConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.UnityConverters.Scripting.RangeIntConverter
settings: []
useAllJsonNetConverters: 1
jsonNetConverters:
- enabled: 1
converterName: Newtonsoft.Json.Converters.StringEnumConverter
settings: []
- enabled: 1
converterName: Newtonsoft.Json.Converters.VersionConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.BinaryConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.BsonObjectIdConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.DataSetConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.DataTableConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.DiscriminatedUnionConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.EntityKeyMemberConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.ExpandoObjectConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.IsoDateTimeConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.JavaScriptDateTimeConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.KeyValuePairConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.RegexConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.UnixDateTimeConverter
settings: []
- enabled: 0
converterName: Newtonsoft.Json.Converters.XmlNodeConverter
settings: []

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 466d3febdbd656c4f9728b1116b1564f
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,10 @@
using UnityEngine;
namespace Sample{
// Create at Asset/Test
[CreateAssetMenu(fileName = "NewData", menuName = "Test/Data", order = 1)]
public class TestExposedReference:ScriptableObject{
public ExposedReference<Camera> camera;
public ExposedReference<GameObject> go;
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fed858872d394fb8896d48178f8a7d78
timeCreated: 1657862979

@ -21,6 +21,8 @@ namespace TNode.Editor.Serialization{
Cache.Add(data,wrapper);
return wrapper;
}
public event Action<DataWrapper<TWrapper,TData>> OnValueChanged;
public void SetValue(string path, object value){

@ -60,7 +60,7 @@ namespace TNode.Editor.Serialization{
// private static readonly Dictionary<NodeData,NodeDataWrapper> Cache = new ();
// public event Action<NodeDataWrapper> OnValueChanged;
// /// <summary>
// /// Create a new wrapper or get a cached wrapper for the given data
// /// Create a new wrapper or get a infoCached wrapper for the given data
// /// </summary>
// /// <param name="data">node data,an implemented type is acceptable</param>
// /// <returns></returns>

@ -1,7 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using TNode.Editor.Serialization;
using TNode.RuntimeCache;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;
using Object = UnityEngine.Object;
namespace TNode.Models{
@ -9,23 +13,170 @@ namespace TNode.Models{
/// Scene Object wrapper use to serialize blackboard data
/// </summary>
public class SceneObjectWrapper:DataWrapper<SceneObjectWrapper,BlackboardData>{
public bool loadedFromScene =false;
public bool loadedFromScene = false;
[NonSerialized]
public List<string> PossibleSceneFields;
public List<SceneSerializationEntry> SceneFields;
public struct SceneSerializationEntry{
public string ScenePath;
public string FieldName;
public string SceneObject;
public List<string> sceneObjects = new List<string>();
public void LoadFromScene(){
}
public bool infoCached;
//Reflection may be expensive, so we cache the type
public Type Type;
//we need to cache blackboard data info
public void LoadBlackboardInfo(){
if (infoCached) return;
infoCached = true;
Type = data.GetType();
PossibleSceneFields = new List<string>();
foreach (var field in Type.GetFields()){
if (field.FieldType == typeof(Object)){
PossibleSceneFields.Add(field.Name);
}
}
}
#region Oboselete Region for serialization
//
// public string BuildThroughTransform(Object go, Transform root,int id){
// if (root.gameObject == go) return $"/{id}";
// var res = root.transform.Find(go.name);
// if (res != null){
// for (int j = 0; j < root.transform.childCount; j++){
// var child = root.transform.GetChild(j);
// var childRes = BuildThroughTransform(go,child,j);
// if(childRes=="") continue;
// return $"/{id}{childRes}";
// }
// }
// return "";
// }
// public string BuildPath(Object go,GameObject[] roots){
//
// //Search root objects
// for(int i=0;i<roots.Length;i++){
// var res = BuildThroughTransform(go,roots[i].transform,i);
// if (res != "") return res;
// }
//
// return "";
// }
// public string GetSceneObjectSerializationInfo(Object value){
//
// var type = value.GetType();
// if (value is GameObject go){
// //check if go is a scene object
// if (go.scene.IsValid()){
// //TODO costly operation
// var roots = go.scene.GetRootGameObjects();
// var path = BuildPath(go,roots);
// return path;
// }
// else{
// return "$not in scene$";
// }
// }
//
// if (value is Behaviour be){
// if (be.gameObject.scene.IsValid()){
// //TODO costly operation
// GameObject gameObject;
// var roots = (gameObject = be.gameObject).scene.GetRootGameObjects();
// var path = BuildPath(gameObject,roots);
// return path + $"/{value.GetType()}";
// }
// else{
// return "$not in scene$";
// }
// }
//
// return "";
//
// }
//
// private void SaveToSceneFields(Object value,string fieldName){
// if (value == null) return;
// var scenePath = SceneManager.GetActiveScene().path;
// SceneFields.Add(new SceneSerializationEntry(){
// ScenePath = scenePath,
// FieldName = fieldName,
// SceneObject = GetSceneObjectSerializationInfo(value)
// });
// }
//
// public void Save(){
// SceneFields.Clear();
// //Search all possible scene fields to check if they have values
// foreach (var s in PossibleSceneFields){
// var value = data.GetValue(s) as Object;
// SaveToSceneFields(value,s);
// }
// }
//
// public void Load(){
// Scene scene = SceneManager.GetActiveScene();
// if (scene.IsValid()){
// foreach (var s in SceneFields){
// GameObject currentGo = null;
// bool isBehaviour = false;
// Behaviour behaviour = null;
// if (s.ScenePath == scene.path){
// var value = data.GetValue(s.FieldName) as Object;
// if (value == null){
// var path = s.SceneObject.Split('/');
// for (var i = 0; i < path.Length; i++){
// if (currentGo == null){
// var index = int.Parse(path[i]);
// currentGo = scene.GetRootGameObjects()[index];
// }
// if (i == path.Length - 1){
// var firstChar = path[i][0];
// //check if it's a number
// if (firstChar is >= '0' and <= '9'){
// var index = int.Parse(path[i]);
// currentGo = currentGo.transform.GetChild(index).gameObject;
// data.SetValue(s.FieldName,currentGo);
// }
// else{
// isBehaviour = true;
// behaviour= currentGo.GetComponent(path[i]) as Behaviour;
// data.SetValue(s.FieldName,behaviour);
// }
// }
// else{
// var index = int.Parse(path[i]);
// currentGo = currentGo?.transform?.GetChild(index).gameObject;
// }
// }
// }
// }
// }
// }
// }
#endregion
public override BlackboardData GetData(){
if (data == null) return null;
LoadBlackboardInfo();
if (!Cache.ContainsKey(this)){
Cache.Add(data,this);
}
if (loadedFromScene==false){
loadedFromScene = true;
Load();
}
return data;
}
private void Load(){
var res = SceneManager.GetActiveScene();
GameObject.FindGameObjectsWithTag("SerializedData");
}
}
}

@ -0,0 +1,9 @@
using UnityEngine;
namespace TNode.Runtime{
public class SceneSerializedData:MonoBehaviour{
public GameObject serializedObject;
public Behaviour serializedFeature;
public bool isComponent;
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2f2dd6542406480ea3fec1d9527ffba8
timeCreated: 1657874785

@ -16,33 +16,42 @@ namespace TNode.RuntimeCache{
}
//delegate return a value from a nodedata
public delegate object GetValueDelegate(IModel nodeData);
public delegate object SetValueDelegate(IModel nodeData,object value);
public delegate void SetValueDelegate(object nodeData,object value);
public readonly Dictionary<Type, Dictionary<string,GetValueDelegate>> CachedDelegatesForGettingValue =
new ();
public readonly Dictionary<Type,Dictionary<string,SetValueDelegate>> CachedDelegatesForSettingValue =
new ();
private static readonly string[] ExcludedAssemblies = new string[]{"Microsoft", "UnityEngine","UnityEditor","mscorlib","System"};
public RuntimeCache(){
RegisterRuntimeBlackboard(typeof(EasyBlackboardData));
}
public void RegisterRuntimeBlackboard(Type type){
if (type == null) return;
if(!CachedDelegatesForGettingValue.ContainsKey(type)){
CachedDelegatesForGettingValue.Add(type, new Dictionary<string, GetValueDelegate>());
CachedDelegatesForSettingValue.Add(type,new Dictionary<string, SetValueDelegate>());
var properties = type.GetProperties();
foreach(var property in properties){
//if the property only has a setter ,skip
if(property.GetMethod == null){
continue;
}
var getValueDelegate = GetValueDelegateForProperty(property);
CachedDelegatesForGettingValue[type].Add(property.Name,getValueDelegate);
var setValueDelegate = SetValueDelegateForProperty(property);
CachedDelegatesForSettingValue[type].Add(property.Name,setValueDelegate);
}
//register the fields
var fields = type.GetFields();
foreach(var field in fields){
var getValueDelegate = GetValueDelegateForField(field);
CachedDelegatesForGettingValue[type].Add(field.Name,getValueDelegate);
var setValueDelegate = SetValueDelegateForField(field);
CachedDelegatesForSettingValue[type].Add(field.Name,setValueDelegate);
}
}
}
@ -50,15 +59,18 @@ namespace TNode.RuntimeCache{
private GetValueDelegate GetValueDelegateForField(FieldInfo field){
return field.GetValue;
}
private SetValueDelegate SetValueDelegateForField(FieldInfo field){
return field.SetValue;
}
private GetValueDelegate GetValueDelegateForProperty(PropertyInfo property){
var getValueDelegate = (GetValueDelegate)Delegate.CreateDelegate(typeof(GetValueDelegate), property.GetGetMethod());
return getValueDelegate;
}
private SetValueDelegate SetValueDelegateForProperty(PropertyInfo property){
var setValueDelegate = (SetValueDelegate)Delegate.CreateDelegate(typeof(SetValueDelegate), property.GetSetMethod());
return setValueDelegate;
}
}
public static class RuntimeExtension{
@ -67,11 +79,18 @@ namespace TNode.RuntimeCache{
var method = RuntimeCache.Instance.CachedDelegatesForGettingValue[blackboardData.GetType()][path];
return (T) method.Invoke(blackboardData);
}
public static object GetValue(this BlackboardData blackboardData, string path){
var method = RuntimeCache.Instance.CachedDelegatesForGettingValue[blackboardData.GetType()][path];
return method.Invoke(blackboardData);
}
public static void SetValue<T>(this BlackboardData blackboardData,string path,T value){
var method = RuntimeCache.Instance.CachedDelegatesForSettingValue[blackboardData.GetType()][path];
method.Invoke(blackboardData,value);
}
public static void SetValue(this BlackboardData blackboardData,string path,object value){
var method = RuntimeCache.Instance.CachedDelegatesForSettingValue[blackboardData.GetType()][path];
method.Invoke(blackboardData,value);
}
public static RuntimeCache.GetValueDelegate GetValueDelegate(this BlackboardData blackboardData,string path){
var method = RuntimeCache.Instance.CachedDelegatesForGettingValue[blackboardData.GetType()][path];
return method;

@ -32,12 +32,13 @@ namespace TNodeGraphViewImpl.Editor.GraphBlackboard{
var foldoutData = new Foldout{
text = field.Name
};
var drawer = new PropertyField(serializedObject.FindProperty("data").FindPropertyRelative(field.Name),field.Name);
var drawer = new GraphBlackboardPropertyField(serializedObject.FindProperty("data").FindPropertyRelative(field.Name),field.Name);
drawer.Bind(serializedObject);
foldoutData.Add(drawer);
visualElement.Add(propertyField);
visualElement.Add(foldoutData);
this.Add(visualElement);
Add(visualElement);
}
else{

@ -0,0 +1,21 @@
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace TNodeGraphViewImpl.Editor.GraphBlackboard{
public class GraphBlackboardPropertyField:PropertyField{
public GraphBlackboardPropertyField(SerializedProperty findPropertyRelative, string fieldName):base(findPropertyRelative, fieldName){
}
protected override void ExecuteDefaultActionAtTarget(EventBase evt)
{
base.ExecuteDefaultActionAtTarget(evt);
if (this.Q<ObjectField>() != null){
this.Q<ObjectField>().allowSceneObjects = false;
}
}
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 39e7ba9513c34ea0814f018befd8d7ad
timeCreated: 1657876733

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f54d1bd14bd3ca042bd867b519fee8cc
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save