我正在使用Unity Engine和C#开发游戏。我是初学者。
我创建了一个自定义类,称为“ Item”,该类来自可编写脚本的对象,这些对象是游戏中各项的基础。 Item类型的对象保存在“库存”的另一个类中,该类包含添加和删除功能,用于从库存中添加和删除Item类型的对象。我目前正在开发游戏的“保存和加载”功能,由于无法通过将项目序列化为二进制并将其放置在持久数据路径中来保存Item类型的对象,因此我创建了代码来保存NAME该项目。 我的问题是:如何仅从其名称的字符串创建和添加对象Item(我的自定义类)。(属性Item.name与Unity中对象的实际名称共享)。我已在代码中添加了注释,以期使人们更容易理解我的工作。
这是课程项:
public class Item : ScriptableObject
{
new public string name = "New Item";
public Sprite icon = null;
public bool isDefaultItem = false;
public virtual void Use()
{
Debug.Log("USING: " + name);
}
public void RemoveFromInventory()
{
Inventory.instance.Remove(this);
}
}
这是类清单,其中包含添加和删除功能,其中包含一个项目:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
public class Inventory : MonoBehaviour
{
public static Inventory instance;
private bool alreadyExists = false;
public static int space = 20;
Vector3 playerPosition;
private void Awake()
{
if (instance != null)
{
Debug.LogWarning("More than 1 inventory");
return;
}
instance = this;
}
public List<Item> items = new List<Item>();
public static List<string> index = new List<string>();
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback;
//These are the functions to add and remove an item to the inventory, Taking in an item, of type Item, from my custom class Item.
public bool Add (Item item)
{
if (!item.isDefaultItem)
{
if (items.Count >= space)
{
Debug.Log("NOT ENOUGH SPACE IN INV");
return false;
}
items.Add(item);
index.Add(item.name);
}
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
return true;
}
public void Remove(Item item)
{
items.Remove(item);
index.Remove(item.name);
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
}
这里是实际包含问题所在代码的类;我需要以某种方式实例化或创建一个项目,然后将其从项目名称添加到清单中。请参阅LoadInv()以了解需要放置的位置。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
public class InventoryAttached : MonoBehaviour
{
public string[] invArray;
string all;
//This is the Saving Seperator
char[] delimiter = { '.' };
public void SaveInv()
{
InvSaveSystem.SaveInv(this);
}
public void LoadInv()
{
//Code that gets data for the items that need to be added to the inventory
InvData data = InvSaveSystem.LoadInv(); //This is the Data from my binary formatter
all = ConvertStringArrayToString(data.invSlot);
invArray = all.Split(delimiter);
//For loop that iterates through the inventory array containing the names of the items stored in the inventory
for (int i = 0; i < invArray.Length; i++)
{
Debug.Log(invArray[i]);
//Here, I would like to use my Add function to add the items to the inventory
// It would look something like this: Inventory.instance.Add(invArray[i[]);
}
}
static string ConvertStringArrayToString(string[] array) //Converter Code used to convert to and from string arrays
{
// Concatenate all the elements into a StringBuilder.
StringBuilder builder = new StringBuilder();
foreach (string value in array)
{
builder.Append(value);
builder.Append('.');
}
return builder.ToString();
}
static string ConvertStringArrayToStringJoin(string[] array)
{
// Use string Join to concatenate the string elements.
string result = string.Join(".", array);
return result;
}
}
仅供参考,这是InvData类,其中包含My保存系统格式化为二进制并保存到HD的字符串数组。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using System.Text.RegularExpressions;
[System.Serializable]
public class InvData
{
public string[] invSlot;
public InvData(InventoryAttached inv)
{
invSlot = new string[Inventory.index.Count];
for (int i = 0; i < Inventory.index.Count; i++)
{
invSlot[i] = Inventory.index[i];
}
}
}
答案 0 :(得分:-1)
.AddComponent(Type.GetType(“ type_name”));