[英]Find all the Instances of a Scriptable Object and store it in a List
I have one Scriptable Object Character
and let us say n instances of that Character
. 我有一个可脚本编写的对象
Character
,让我们说出该Character
n个实例。
Character.cs
public class Character : UnityEngine.ScriptableObject
{
...
}
// Let us say that there are 100 instances of this Class
Now I create another Class 现在我创建另一个类
CharacterList.cs
public class CharacterList
{
List<Character> characterList = new List<Character>();
FindObjectsOfType<Character>();
// I also tried to write Character[] character = FindObjectsOfType<Character>() but that did not work.
// Now How do I put all the objects found in the List?
}
What I want to do is create a Class
that isn't a Monobehaviour
. 我想要做的就是创建一个
Class
,是不是一个Monobehaviour
。 Just a simple Class called CharacterList
and in there I want a List
of Characters that will contain all the instances of the Character
Scriptable Object. 只是一个称为
CharacterList
的简单类,在其中我想要一个字符List
,其中将包含Character
Scriptable Object的所有实例。 Hence, there should be n number of Characters in CharacterList
因此,CharacterList中应该有n个
CharacterList
(SO = Scriptable Object) We've 2 options here. (SO =可编写脚本的对象)我们在这里有2个选项。 Depending if these SO are serialized or instantiated during play.
取决于这些SO是在播放期间被序列化还是实例化。
Easier option is to create another monobehaviour or scriptable object and place all the instantiated object there. 最简单的选择是创建另一个单行为或可编写脚本的对象,并将所有实例化的对象放在此处。
public class CharacterList : ScriptableObject
{
public Character[] characterList;
}
The other way will require you to search for the SO in the assets folder by scripting using AssetDatabase.FindAssets and AssetDatabase.LoadAssetAtPath . 另一种方法将要求您通过使用AssetDatabase.FindAssets和AssetDatabase.LoadAssetAtPath编写脚本在资产文件夹中搜索SO。
In this case the code will look like this: 在这种情况下,代码将如下所示:
public class CharacterList
{
List<Character> characterList = new List<Character>();
void PopulateList()
{
string[] assetNames = AssetDatabase.FindAssets("Your_Filter", new[] { "Assets/YourFolder" });
characterList.Clear();
foreach (string SOName in assetNames)
{
var SOpath = AssetDatabase.GUIDToAssetPath(SOName);
var character = AssetDatabase.LoadAssetAtPath<Character>(SOpath);
characterList.Add(character);
}
}
}
How to find them. 如何找到他们。
VIA NAME 威盛名称
You can replace "Your_Filter" to retrieve your SO. 您可以替换“ Your_Filter”来检索您的SO。 In this case the SO should share come kind of commonality in their names.
在这种情况下,SO应该在名称上共享某种共性。
VIA FOLDER 威盛文件夹
You can replace new[] { "Assets/YourFolder" } with your folder. 您可以使用文件夹替换new [] {“ Assets / YourFolder”}。 In this case all the SO should be in the same folder AND this folder should contain ONLY these SO.
在这种情况下,所有SO都应位于同一文件夹中,并且该文件夹应仅包含这些SO。
In this case I would suggest to implement some kind of factory. 在这种情况下,我建议实施某种工厂。
Instead of creating the SO with the usual code: 而不是使用常规代码创建SO:
ScriptableObject.CreateInstance<YourSo>();
I would use a factory the Create the Instance and store it. 我将使用创建实例的工厂并将其存储。 So you will have something like this.
这样您将拥有类似的东西。
public static class CharacterGenerator
{
static List<Character> characterList = new List<Character>();
public static Character CreateCharacter()
{
var character = ScriptableObject.CreateInstance<YourSo>();
characterList.Add(character);
return character;
}
}
So whenever you want to create a new character you will call CharacterGenerator.CreateCharacter();
因此,无论何时要创建新字符,都将调用
CharacterGenerator.CreateCharacter();
Remember also to Clear the list at the start of the game (or when you think it is required). 还记得在游戏开始时(或在您认为必要时)清除列表。
在CharacterList.cs
类中:
object[] characters = FindObjectsOfType(typeof(Character));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.