我有以下可编写脚本的对象:
[CreateAssetMenu(fileName = "Assets/Gladio Games/Resources/Levels/LEVLE_TMP", menuName = "Utils/Crossword/Generate Empty Level", order = 100)]
public class Level : ScriptableObject
{
public char[,] Table { get; protected set; }
public List<Crossword> Crosswords { get; protected set; }
protected static char EMPTY_CHAR = '\0';
public Crossword GetCrosswordAtIndex(int x, int y, Direction direction)
{
//Ottieni la prima e l'unica parola che si trova sotto quell'indice
return Crosswords.First(crossword => crossword.Direction == direction && crossword.GetCrosswordIndexes().Contains(new Vector2(x, y)));
}
}这是我用来保存可脚本对象的代码。
private static void Save(Level level, string path)
{
EditorUtility.SetDirty(level);
AssetDatabase.CreateAsset(level, path + ".asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
}该对象是通过编辑器脚本成功创建的,并且保存了所有数据,但是如果我关闭并重新打开编辑器,并尝试加载字段为null。在场景中使用保存和加载脚本对象的脚本。
我做错什么了?
发布于 2022-04-19 07:54:28
签出脚本序列化规则
尤其是在你的情况下
因此,您的两个属性都没有序列化(=保存)。
作为解决办法
Crosswords- Make sure that the type `Crossword` itself is `[Serializable]`- Use a **field** instead公开列表字汇;
Table,有多种方法。
我可能会简单地使用包装类,比如。
//当然可以使这个通用表变得通用,但对于抽屉来说,现在这种方法更容易一些:可序列化的公共类表{ SerializeField私有int xDimension = 1;SerializeField私有int yDimension = 1;SerializeField私有char[] flatArray =新char1;公共表{{)}公共表(char,数组){ xDimension = array.GetLength(0);yDimension = array.GetLength(1);flatArray =新charxDimension * yDimension;(var x= 0;x< xDimension;x++) { for (var y= 0;y< yDimension;y++) { flatArrayx * yDimension +y= arrayx,y;}}公共表( int x,int y) { xDimension = x;yDimension = y;flatArray =新字符* y;}公共表(char[]数组,int x,int y) { xDimension = x;yDimension = y;flatArray =数组;} public char thisint int x,int y{ get => flatArrayx * yDimension + y;设置=> flatArrayx * yDimension +y=值;} #if UNITY_EDITOR CustomPropertyDrawer(typeof(Table))私有类TableDrawer : PropertyDrawer {私有const int k_elementSpacing = 5;公共覆盖浮标GetPropertyHeight(SerializedProperty属性,GUIContent label) { var height = 1f;if (property.isExpanded) {高度+= k_elementSpacing* 1.5f;}返回高度* EditorGUIUtility.singleLineHeight;}公共覆盖无效OnGUI(Rect位置、SerializedProperty属性、GUIContent标签){ var foldoutRect = position;foldoutRect.height = EditorGUIUtility.singleLineHeight;foldoutRect.width = EditorGUIUtility.labelWidth;property.isExpanded = EditorGUI.Foldout(foldoutRect、property.isExpanded、label);若(property.isExpanded) { var xRect = position;xRect.height = EditorGUIUtility.singleLineHeight;xRect.x += foldoutRect.width;xRect.width -= foldoutRect.width;xRect.width *= 0.5f;var yRect = xRect;yRect.x += xRect.width;var xDimension = property.FindPropertyRelative(nameof(Table.yDimension));var yDimension =property.FindPropertyRelative(nameof(Table.yDimension));var数组= property.FindPropertyRelative(nameof(Table.flatArray));使用(var changeCheck =新EditorGUI.ChangeCheckScope()) { EditorGUI.PropertyField(xRect,xDimension,GUIContent.none);EditorGUI.PropertyField(yRect,yDimension,GUIContent.none);若(changeCheck.changed) { array.arraySize = xDimension.intValue * yDimension.intValue;}位置.y += EditorGUIUtility.singleLineHeight * 1.5f;EditorGUI.indentLevel++;var elementRect =EditorGUI.IndentedRect(位置);elementRect缩进级-;var elementWidth = elementRect.width / yDimension.intValue - k_elementSpacing;var elementHeight = EditorGUIUtility.singleLineHeight;var currentPosition = elementRect;currentPosition.width = elementWidth;currentPosition.height = elementHeight;for (var x= 0;x< xDimension.intValue;x++) { for (var y= 0;y< yDimension.intValue;y++) { var元素= array.GetArrayElementAtIndex(x * yDimension.intValue + y);EditorGUI.PropertyField(currentPosition,element,GUIContent.none);CurentPosition.x += elementWidth + k_elementSpacing;} += elementHeight * 1.5f;} #endif }
然后更愿意拥有。
公共餐桌;https://stackoverflow.com/questions/71920089
复制相似问题