将所有ScriptableObjects从文件夹添加到Unity中的Dictionary

时间:2018-01-07 20:28:25

标签: c# unity3d

我这里没什么问题。 我正在尝试从 Assets / Resources / InteractibleObjects 获取所有ScriptableObjects(名为“ InteractibleObjects ”)并将它们放入Dictionary(名为“ allInteractibleObjects “),但是Unity将NullReferenceExeption与”将'objectToAdd'添加到'allInteractibleObjects'字典“一致。 有我的代码:

using System.Linq;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

public Dictionary<int, InteractibleObject> allInteractibleObjects;

public string[] allInteractibleObjetsNames;

void Start()
{
    allInteractibleObjetsNames = Directory.GetFiles ("Assets/Resources/InteractibleObjects/", "*.asset")
       .Select(Path.GetFileName)
       .ToArray();// Getting files names

    for (int i = 0; i < allInteractibleObjetsNames.Length; i++) 
    {
        allInteractibleObjetsNames[i] = allInteractibleObjetsNames[i].Remove (allInteractibleObjetsNames[i].Length - 6);// Removing '.asset' from names
        Object objectInFolder = Resources.Load("InteractibleObjects/" + allInteractibleObjetsNames[i]);// Finding 'InteractibleObjects' by the name from 'allInteractibleObjetsNames'
        InteractibleObject objectToAdd = (InteractibleObject)objectInFolder;// From 'Object' to 'InteractibleObject'
        allInteractibleObjects.Add (i, objectToAdd);// Adding 'objectToAdd' to the 'allInteractibleObjects' Dictionary
    }
}

我该怎么办?

0 个答案:

没有答案