using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
namespace TNodeCore.Runtime.Models{
///
/// Scene Object wrapper use to serialize blackboard data
///
public class SceneObjectWrapper:DataWrapper{
public bool loadedFromScene = false;
[NonSerialized]
public List PossibleSceneFields;
public List SceneFields;
public struct SceneSerializationEntry{
public string ScenePath;
public string FieldName;
public string SceneObject;
}
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();
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= '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");
}
}
}