Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity3D—可以将数据保存到非';当你玩的时候,我不会放过你_C#_Unity3d - Fatal编程技术网

C# Unity3D—可以将数据保存到非';当你玩的时候,我不会放过你

C# Unity3D—可以将数据保存到非';当你玩的时候,我不会放过你,c#,unity3d,C#,Unity3d,我正在尝试创建一个编辑器窗口,可以在游戏中创建项目。 它应该将该项目添加到一个项目列表中,而不是每次玩游戏时都清除该项目 是否可以在没有外部XML/JSON文件保存数据的情况下执行此操作 我目前在编辑器窗口脚本中有这样的功能,ItemEquipable(type,price)是一个基本类,它只有一个类型和一个价格变量: if(GUILayout.Button("Create")) ItemList.items.Add(new ItemEquipable(type, price))

我正在尝试创建一个编辑器窗口,可以在游戏中创建项目。 它应该将该项目添加到一个项目列表中,而不是每次玩游戏时都清除该项目

是否可以在没有外部XML/JSON文件保存数据的情况下执行此操作

我目前在编辑器窗口脚本中有这样的功能,ItemEquipable(type,price)是一个基本类,它只有一个类型和一个价格变量:

if(GUILayout.Button("Create"))
        ItemList.items.Add(new ItemEquipable(type, price));
ItemList类ItemEquipable扩展了基本Item类

using UnityEngine;
using System.Collections.Generic;

public class ItemList:MonoBehaviour {
    public static List<Item> items;
}
使用UnityEngine;
使用System.Collections.Generic;
公共类项目列表:MonoBehavior{
公共静态列表项;
}
我已将项目列表附加到场景中的游戏对象

我看到了,但该脚本扩展了编辑器,我正在扩展EditorWindow,所以我没有访问目标变量的权限

更新:

好的,我已经试着让ItemList类序列化了

using UnityEngine;
using System;
using System.Collections.Generic;

[Serializable]
public class ItemList:MonoBehaviour {
    public static List<Item> items;
}
使用UnityEngine;
使用制度;
使用System.Collections.Generic;
[可序列化]
公共类项目列表:MonoBehavior{
公共静态列表项;
}
把它变成一个单身汉

using UnityEngine;
using System.Collections.Generic;

public class ItemList:Singleton<ItemList> {
    public List<Item> items;

}
使用UnityEngine;
使用System.Collections.Generic;
公共类ItemList:Singleton{
公共清单项目;
}
Singleton是您的基本Singleton类/代码


但是两者都不起作用。

项目数组未标记为可序列化,因此Unity无法在启动游戏时保存它。解决方案是使用属性
[Serializable]
标记
ItemList
,或者您可以直接使用
列表。

因此,我解决问题的方法是每次创建项时将数组保存到预置中

UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/Resources/Prefabs/Items/Item Manager.prefab");
PrefabUtility.ReplacePrefab(Selection.activeGameObject, prefab);
AssetDatabase.Refresh();
我已将ItemList类拆分为多个列表:

[Serializable]
public class ItemList:MonoBehaviour {
    [SerializeField] private List<ItemEquipable> equipableItems = new     List<ItemEquipable>();
    [SerializeField] private List<ItemWeapon> weaponItems = new List<ItemWeapon>();
    [SerializeField] private List<ItemPower> powerItems = new List<ItemPower>();
    [SerializeField] private List<ItemSpecial> specialItems = new List<ItemSpecial>();

    public List<ItemEquipable> EquipableItems { get { return equipableItems; } }
    public List<ItemWeapon> WeaponItems { get { return weaponItems; } }
    public List<ItemPower> PowerItems { get { return powerItems; } }
    public List<ItemSpecial> SpecialItems { get { return specialItems; } }
}
编辑器中使用静态项变量以防止数百个变量使编辑器脚本混乱:

Item.item.Type = (Item.ItemType)EditorGUILayout.EnumPopup("Item Type", Item.item.Type);
为了设置这些变量,我刚刚对item类中的每个字段使用了getter和setter


这可能不是最有效的方法,但它很管用。

这对我来说不管用,我已使ItemList可序列化,每次单击“播放”时它仍会重置。我也试着让它成为非静态的和单态的。除了创建一个“Add”方法来检查
items
是否为空,然后对其进行实例化。对不起,我忘了说该项还必须标记为
[Serializable]
,它应该可以使用它!
Item.item.Type = (Item.ItemType)EditorGUILayout.EnumPopup("Item Type", Item.item.Type);