Beispiel #1
0
        GameObject SpawnInstance(GameObject prototype, ObjectInitData initData = null, string savedUniqueID = null)
        {
            // Instantiate prototype
            GameObject instance = Instantiate(prototype);
            // Get SaveLoadable component of instance
            SaveLoadableGameObject spawnedObject = instance.GetComponent <SaveLoadableGameObject>();

            if (initData == null)
            {
                Debug.LogFormat("GameObject {0} spawning without any initialization data", prototype.name);
            }
            else
            {
                spawnedObject.Initialize(initData);
            }
            // If we are spawning an object with a saved UniqueID, set it here
            if (savedUniqueID != null)
            {
                instance.GetComponent <UniqueID>().SetUniqueIDFromSaveFile(savedUniqueID);
            }


            // WorldController should listen for this and add it to its dictionary
            cbPrefabSpawned(instance);

            return(instance);
        }
Beispiel #2
0
        // Loads a prefab resource from its string path and confirms it has a SpawnableObject type for initialization
        // TODO: Would be nice to add this to a dictionary for ease of access
        // TODO: This seems unnecessarily convoluted now. Why split these functions? Is dictionary optimization necessary?
        GameObject CreatePrototype(string prefabPath)
        {
            // Load a prefab as our prototype
            GameObject prototype = (GameObject)Resources.Load(prefabPath, typeof(GameObject));

            if (prototype == null)
            {
                Debug.LogErrorFormat("Unable to find prefab with path {0}", prefabPath);
                return(null);
            }
            // Get the prefab's SaveLoadableGameObject-inherited component (Poster, PostIt, Furniture, etc)
            SaveLoadableGameObject saveLoadableObject = prototype.GetComponent <SaveLoadableGameObject>();

            if (saveLoadableObject == null)
            {
                Debug.LogErrorFormat("Attempting to spawn an item ({0}) without any component derived from SaveLoadableGameObject", prefabPath);
                return(null);
            }

            return(prototype);
        }